Repository URL to install this package:
Version:
6.0.0-beta.8 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @todo swap with uxui-modules @@perf
* @see https://github.com/isaacs/json-stringify-safe
*/
const isNil = (x) => !x;
const MAX_SPLICE_SIZE = 10000;
function toObjNotationString(obj, replacer, spaces, cycleReplacer) {
return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
}
exports.stringify = toObjNotationString;
function serializer(replacer, cycleReplacer) {
const stack = [];
const keys = [];
if (isNil(cycleReplacer)) {
// tslint:disable-next-line
cycleReplacer = function (key, value) {
return stack[0] === value
? '[Circular ~]'
: '[Circular ~.' + keys.slice(0, stack.indexOf(value)).join('.') + ']';
};
}
return function (key, value) {
if (stack.length > 0) {
let thisPos = stack.indexOf(this);
~thisPos ? stack.splice(thisPos + 1) : stack.push(this);
~thisPos ? keys.splice(thisPos, MAX_SPLICE_SIZE, key) : keys.push(key);
if (~stack.indexOf(value)) {
value = cycleReplacer.call(this, key, value);
}
}
else {
stack.push(value);
}
return isNil(replacer) ? value : replacer.call(this, key, value);
};
}
exports.default = toObjNotationString;
//# sourceMappingURL=stringify.js.map