@toolpad/utils
Version:
Shared utilities used by Toolpad packages.
48 lines (46 loc) • 1.44 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getCircularReplacer = getCircularReplacer;
exports.replaceRecursive = replaceRecursive;
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value#circular_references
function getCircularReplacer() {
const ancestors = [];
return function replacer(key, value) {
if (typeof value !== 'object' || value === null) {
return value;
}
// `this` is the object that value is contained in,
// i.e., its direct parent.
while (ancestors.length > 0 && ancestors.at(-1) !== this) {
ancestors.pop();
}
if (ancestors.includes(value)) {
return '[Circular]';
}
ancestors.push(value);
return value;
};
}
function replaceRecursiveImpl(obj, replacer) {
if (Array.isArray(obj)) {
return obj.map((item, i) => {
const newItem = replacer.call(obj, i, item);
return replaceRecursiveImpl(newItem, replacer);
});
}
if (obj && typeof obj === 'object') {
return Object.fromEntries(Object.entries(obj).map(([key, value]) => {
const newValue = replacer.call(obj, key, value);
return [key, replaceRecursiveImpl(newValue, replacer)];
}));
}
return obj;
}
// Replaces nested properties using the same semantics as JSON.stringify
function replaceRecursive(obj, replacer) {
return replaceRecursiveImpl({
'': obj
}, replacer)[''];
}
;