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/router / dist / deps / stringMagic.js
Size: Mime:
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const query_string_1 = __importDefault(require("query-string"));
const exotic_1 = require("exotic");
const chain_able_lego_1 = require("chain-able-lego");
const MATCH_NON_ALPHANUMERIC = /\w+/gim;
const stripNonAlphaNumeric = (x) => {
    if (exotic_1.isString(x)) {
        return x.replace(MATCH_NON_ALPHANUMERIC, '');
    }
    else {
        return '';
    }
};
exports.stripNonAlphaNumeric = stripNonAlphaNumeric;
// @todo 1 for component 1 for uri
// (uri is for absolute url, component is for parts of url)
function isURIEncoded(x = '') {
    if (exotic_1.isString(x) === false) {
        return false;
    }
    const decoded = decodeURIComponent(x);
    if (decoded === x) {
        return false;
    }
    else {
        return true;
    }
}
exports.isURIEncoded = isURIEncoded;
/**
 * @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 = exotic_1.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 = Object.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
                chain_able_lego_1.stringify(to[key])
            : // only serialize objects
                exotic_1.isObj(to[key])
                    ? fromObjToStringifiedWithoutQuotes(to[key], false)
                    : to[key];
        return IS_ARRAY === true || exotic_1.isNil(value) || exotic_1.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;
    }
}
// @note - this slices first and last off
// fromObjToGoodLookingParams
function goodLookingStringify(to) {
    if (exotic_1.isObj(to) === false) {
        return query_string_1.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)
}
exports.goodLookingStringify = goodLookingStringify;
/**
 * @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;
//# sourceMappingURL=stringMagic.js.map