Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@skava/modules / ___dist / router / src / deps / stringMagic.js
Size: Mime:
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.isURIEncoded = isURIEncoded;
exports.goodLookingStringify = goodLookingStringify;
exports.stripNonAlphaNumeric = exports.parseJSON = void 0;

var _queryString = _interopRequireDefault(require("query-string"));

var _exotic = require("exotic");

var _chainAbleBoost = require("chain-able-boost");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const MATCH_NON_ALPHANUMERIC = /\w+/gim;

const stripNonAlphaNumeric = x => {
  if ((0, _exotic.isString)(x)) {
    return x.replace(MATCH_NON_ALPHANUMERIC, '');
  } else {
    return '';
  }
}; // @todo 1 for component 1 for uri
// (uri is for absolute url, component is for parts of url)


exports.stripNonAlphaNumeric = stripNonAlphaNumeric;

function isURIEncoded(str) {
  if ((0, _exotic.isString)(str) === false) {
    return false;
  }

  const decoded = decodeURIComponent(str);

  if (decoded === str) {
    return false;
  } else {
    return true;
  }
}
/**
 * @todo one of the final todos is `{}` around nested objects
 */


function fromObjToStringifiedWithoutQuotes(to, first = true) {
  // const stringified = stringify(to)
  // const withoutQuotes = stringified.replace(/\"([^(\")"]+)\":/g, '$1:')
  // recursive tail call object serialization according to JSON standard
  // ...but without quotes around the keys
  const IS_ARRAY = (0, _exotic.isArray)(to);
  const separator = first === true ? '=' : ':'; // top level entries should be & for routing, nested is for parsing

  const symbol = IS_ARRAY || first === false ? ',' : '&';
  const result = (0, _chainAbleBoost.keys)(to).map(key => {
    // prevent going too deep
    const value = first === false ? // @todo if not primitive at not top value...
    // @example if this is bool don't want stringy bool
    (0, _chainAbleBoost.stringify)(to[key]) : // only serialize objects
    (0, _exotic.isObj)(to[key]) ? fromObjToStringifiedWithoutQuotes(to[key], false) : to[key];
    return IS_ARRAY === true || (0, _exotic.isNil)(value) || (0, _exotic.isNil)(to[key]) ? value : `${key}${separator}${value}`;
  }).filter(value => value !== undefined).join(symbol);

  if (IS_ARRAY === true) {
    return `[${result}]`;
  } else if (first === false) {
    return `{${result}}`;
  } else {
    return result;
  }
}

function fromObjToSearchParamsString(to) {
  const params = new URLSearchParams(window.location.search); // const IS_ARRAY = isArray(to)

  Object.keys(to).forEach(key => {
    const value = to[key];
    params.set(key, value);
  });
  return params;
} // @note - this slices first and last off
// fromObjToGoodLookingParams


function goodLookingStringify(to) {
  if ((0, _exotic.isObj)(to) === false) {
    return _queryString.default.stringify(to);
  } // const stringified = stringify(to).replace(/\\/gmi, '')


  const stringified = fromObjToStringifiedWithoutQuotes(to); // [eh] => eh
  // {eh: []} => eh: []
  // ...
  // @note - this ends up making it insane
  // const prettier = stringified.slice(1, stringified.length - 1)
  // console.log('___goodLookingStringify___', prettier)
  // return prettier

  return stringified; // return qs.stringify(prettier)
  // return fromObjToSearchParamsString(to)
  // return fromObjToStringifiedWithoutQuotes(to)
}
/**
 * @todo @perf @dedupe
 * @description how many times have I/anyone written this
 */


const parseJSON = x => {
  try {
    const parsed = JSON.parse(x);
    return parsed;
  } catch (syntaxException) {
    return x;
  }
};

exports.parseJSON = parseJSON;