UNPKG

@uirouter/core

Version:

UI-Router Core: Framework agnostic, State-based routing for JavaScript Single Page Apps

149 lines 5.86 kB
"use strict"; /** * Functions that manipulate strings * * Although these functions are exported, they are subject to change without notice. * * @packageDocumentation */ Object.defineProperty(exports, "__esModule", { value: true }); exports.joinNeighborsR = exports.splitOnDelim = exports.trimHashVal = exports.splitEqual = exports.splitQuery = exports.splitHash = exports.stripLastPathElement = exports.hostRegex = exports.beforeAfterSubstr = exports.stringify = exports.fnToString = exports.functionToString = exports.kebobString = exports.padString = exports.maxLength = void 0; var predicates_1 = require("./predicates"); var rejectFactory_1 = require("../transition/rejectFactory"); var common_1 = require("./common"); var hof_1 = require("./hof"); /** * Returns a string shortened to a maximum length * * If the string is already less than the `max` length, return the string. * Else return the string, shortened to `max - 3` and append three dots ("..."). * * @param max the maximum length of the string to return * @param str the input string */ function maxLength(max, str) { if (str.length <= max) return str; return str.substr(0, max - 3) + '...'; } exports.maxLength = maxLength; /** * Returns a string, with spaces added to the end, up to a desired str length * * If the string is already longer than the desired length, return the string. * Else returns the string, with extra spaces on the end, such that it reaches `length` characters. * * @param length the desired length of the string to return * @param str the input string */ function padString(length, str) { while (str.length < length) str += ' '; return str; } exports.padString = padString; function kebobString(camelCase) { return camelCase .replace(/^([A-Z])/, function ($1) { return $1.toLowerCase(); }) // replace first char .replace(/([A-Z])/g, function ($1) { return '-' + $1.toLowerCase(); }); // replace rest } exports.kebobString = kebobString; function functionToString(fn) { var fnStr = fnToString(fn); var namedFunctionMatch = fnStr.match(/^(function [^ ]+\([^)]*\))/); var toStr = namedFunctionMatch ? namedFunctionMatch[1] : fnStr; var fnName = fn['name'] || ''; if (fnName && toStr.match(/function \(/)) { return 'function ' + fnName + toStr.substr(9); } return toStr; } exports.functionToString = functionToString; function fnToString(fn) { var _fn = predicates_1.isArray(fn) ? fn.slice(-1)[0] : fn; return (_fn && _fn.toString()) || 'undefined'; } exports.fnToString = fnToString; function stringify(o) { var seen = []; var isRejection = rejectFactory_1.Rejection.isRejectionPromise; var hasToString = function (obj) { return predicates_1.isObject(obj) && !predicates_1.isArray(obj) && obj.constructor !== Object && predicates_1.isFunction(obj.toString); }; var stringifyPattern = hof_1.pattern([ [predicates_1.isUndefined, hof_1.val('undefined')], [predicates_1.isNull, hof_1.val('null')], [predicates_1.isPromise, hof_1.val('[Promise]')], [isRejection, function (x) { return x._transitionRejection.toString(); }], [hasToString, function (x) { return x.toString(); }], [predicates_1.isInjectable, functionToString], [hof_1.val(true), common_1.identity], ]); function format(value) { if (predicates_1.isObject(value)) { if (seen.indexOf(value) !== -1) return '[circular ref]'; seen.push(value); } return stringifyPattern(value); } if (predicates_1.isUndefined(o)) { // Workaround for IE & Edge Spec incompatibility where replacer function would not be called when JSON.stringify // is given `undefined` as value. To work around that, we simply detect `undefined` and bail out early by // manually stringifying it. return format(o); } return JSON.stringify(o, function (key, value) { return format(value); }).replace(/\\"/g, '"'); } exports.stringify = stringify; /** Returns a function that splits a string on a character or substring */ exports.beforeAfterSubstr = function (char) { return function (str) { if (!str) return ['', '']; var idx = str.indexOf(char); if (idx === -1) return [str, '']; return [str.substr(0, idx), str.substr(idx + 1)]; }; }; exports.hostRegex = new RegExp('^(?:[a-z]+:)?//[^/]+/'); exports.stripLastPathElement = function (str) { return str.replace(/\/[^/]*$/, ''); }; exports.splitHash = exports.beforeAfterSubstr('#'); exports.splitQuery = exports.beforeAfterSubstr('?'); exports.splitEqual = exports.beforeAfterSubstr('='); exports.trimHashVal = function (str) { return (str ? str.replace(/^#/, '') : ''); }; /** * Splits on a delimiter, but returns the delimiters in the array * * #### Example: * ```js * var splitOnSlashes = splitOnDelim('/'); * splitOnSlashes("/foo"); // ["/", "foo"] * splitOnSlashes("/foo/"); // ["/", "foo", "/"] * ``` */ function splitOnDelim(delim) { var re = new RegExp('(' + delim + ')', 'g'); return function (str) { return str.split(re).filter(common_1.identity); }; } exports.splitOnDelim = splitOnDelim; /** * Reduce fn that joins neighboring strings * * Given an array of strings, returns a new array * where all neighboring strings have been joined. * * #### Example: * ```js * let arr = ["foo", "bar", 1, "baz", "", "qux" ]; * arr.reduce(joinNeighborsR, []) // ["foobar", 1, "bazqux" ] * ``` */ function joinNeighborsR(acc, x) { if (predicates_1.isString(common_1.tail(acc)) && predicates_1.isString(x)) return acc.slice(0, -1).concat(common_1.tail(acc) + x); return common_1.pushR(acc, x); } exports.joinNeighborsR = joinNeighborsR; //# sourceMappingURL=strings.js.map