75 lines
No EOL
2.8 KiB
JavaScript
75 lines
No EOL
2.8 KiB
JavaScript
"use strict";
|
|
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.stringify = void 0;
|
|
/**
|
|
* Encapsulates JSON.stringify function to detect and handle different types of errors (eg. Circular Structure).
|
|
*
|
|
* @remarks
|
|
* Circular Structure:
|
|
* - It detects when the provided value has circular references and replaces them with [Circular *.{path to the value being referenced}].
|
|
*
|
|
* _replacer internal function:
|
|
* - Have similar functionality as the JSON.stringify internal toJSON function, but with the difference that only affects this stringify functionality.
|
|
*
|
|
* @example
|
|
* // Circular Structure:
|
|
* {
|
|
* "item": {
|
|
* "name": "parent",
|
|
* "parent": null,
|
|
* "child": {
|
|
* "name": "child",
|
|
* "parent": "[Circular *.item]" // => obj.item.child.parent = obj.item
|
|
* }
|
|
* }
|
|
* }
|
|
*
|
|
* @param value — A JavaScript value, usually an object or array, to be converted.
|
|
* @param replacer — A function that transforms the results.
|
|
* @param space — Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
|
|
* @returns {string} The converted JavaScript value to a JavaScript Object Notation (JSON) string.
|
|
*/
|
|
function stringify(value, replacer, space) {
|
|
if (!value) {
|
|
return '';
|
|
}
|
|
try {
|
|
return JSON.stringify(value, stringifyReplacer(replacer), space);
|
|
}
|
|
catch (error) {
|
|
if (!(error === null || error === void 0 ? void 0 : error.message.includes('circular structure'))) {
|
|
throw error;
|
|
}
|
|
const seen = new WeakMap();
|
|
return JSON.stringify(value, function stringifyCircularReplacer(key, val) {
|
|
var _a;
|
|
if (val === null || val === undefined || typeof val !== 'object') {
|
|
return val;
|
|
}
|
|
if (key) {
|
|
const path = seen.get(val);
|
|
if (path) {
|
|
return `[Circular *.${path.join('.')}]`;
|
|
}
|
|
const parent = (_a = seen.get(this)) !== null && _a !== void 0 ? _a : [];
|
|
seen.set(val, [...parent, key]);
|
|
}
|
|
const value = stringifyReplacer(replacer)(key, val);
|
|
return value;
|
|
}, space);
|
|
}
|
|
}
|
|
exports.stringify = stringify;
|
|
function stringifyReplacer(replacer) {
|
|
return function stringifyReplacerInternal(key, val) {
|
|
const replacerValue = replacer ? replacer.call(this, key, val) : val;
|
|
if (replacerValue === null || replacerValue === undefined || typeof replacerValue !== 'object') {
|
|
return replacerValue;
|
|
}
|
|
const result = replacerValue._replacer ? replacerValue._replacer(key) : replacerValue;
|
|
return result;
|
|
};
|
|
}
|
|
//# sourceMappingURL=stringify.js.map
|