|
| 1 | +/** @module common */ /** */ |
| 2 | + |
| 3 | +import {isString, isArray, isDefined, isNull, isPromise, isInjectable, isObject} from "./predicates"; |
| 4 | +import {TransitionRejection} from "../transition/rejectFactory"; |
| 5 | +import {IInjectable, identity} from "./common"; |
| 6 | +import {pattern, is, not, val, invoke} from "./hof"; |
| 7 | +import {Transition} from "../transition/transition"; |
| 8 | +import {Resolvable} from "../resolve/resolvable"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Returns a string shortened to a maximum length |
| 12 | + * |
| 13 | + * If the string is already less than the `max` length, return the string. |
| 14 | + * Else return the string, shortened to `max - 3` and append three dots ("..."). |
| 15 | + * |
| 16 | + * @param max the maximum length of the string to return |
| 17 | + * @param str the input string |
| 18 | + */ |
| 19 | +export function maxLength(max: number, str: string) { |
| 20 | + if (str.length <= max) return str; |
| 21 | + return str.substr(0, max - 3) + "..."; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Returns a string, with spaces added to the end, up to a desired str length |
| 26 | + * |
| 27 | + * If the string is already longer than the desired length, return the string. |
| 28 | + * Else returns the string, with extra spaces on the end, such that it reaches `length` characters. |
| 29 | + * |
| 30 | + * @param length the desired length of the string to return |
| 31 | + * @param str the input string |
| 32 | + */ |
| 33 | +export function padString(length: number, str: string) { |
| 34 | + while (str.length < length) str += " "; |
| 35 | + return str; |
| 36 | +} |
| 37 | + |
| 38 | +export const kebobString = (camelCase: string) => camelCase.replace(/([A-Z])/g, $1 => "-"+$1.toLowerCase()); |
| 39 | + |
| 40 | +function _toJson(obj) { |
| 41 | + return JSON.stringify(obj); |
| 42 | +} |
| 43 | + |
| 44 | +function _fromJson(json) { |
| 45 | + return isString(json) ? JSON.parse(json) : json; |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +function promiseToString(p) { |
| 50 | + if (is(TransitionRejection)(p.reason)) return p.reason.toString(); |
| 51 | + return `Promise(${JSON.stringify(p)})`; |
| 52 | +} |
| 53 | + |
| 54 | +export function functionToString(fn) { |
| 55 | + let fnStr = fnToString(fn); |
| 56 | + let namedFunctionMatch = fnStr.match(/^(function [^ ]+\([^)]*\))/); |
| 57 | + return namedFunctionMatch ? namedFunctionMatch[1] : fnStr; |
| 58 | +} |
| 59 | + |
| 60 | +export function fnToString(fn: IInjectable) { |
| 61 | + let _fn = isArray(fn) ? fn.slice(-1)[0] : fn; |
| 62 | + return _fn && _fn.toString() || "undefined"; |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +let stringifyPattern = pattern([ |
| 67 | + [not(isDefined), val("undefined")], |
| 68 | + [isNull, val("null")], |
| 69 | + [isPromise, promiseToString], |
| 70 | + [is(Transition), invoke("toString")], |
| 71 | + [is(Resolvable), invoke("toString")], |
| 72 | + [isInjectable, functionToString], |
| 73 | + [val(true), identity] |
| 74 | +]); |
| 75 | + |
| 76 | +export function stringify(o) { |
| 77 | + var seen = []; |
| 78 | + |
| 79 | + function format(val) { |
| 80 | + if (isObject(val)) { |
| 81 | + if (seen.indexOf(val) !== -1) return '[circular ref]'; |
| 82 | + seen.push(val); |
| 83 | + } |
| 84 | + return stringifyPattern(val); |
| 85 | + } |
| 86 | + |
| 87 | + return JSON.stringify(o, (key, val) => format(val)).replace(/\\"/g, '"'); |
| 88 | +} |
| 89 | + |
0 commit comments