fix(Variables): Fix handling of circular object references

This commit is contained in:
Mariusz Nowak 2020-10-05 13:54:36 +02:00 committed by Mariusz Nowak
parent fc34140f4e
commit fd451caf90

View File

@ -241,7 +241,7 @@ class Variables {
* Generate an array of objects noting the terminal properties of the given root object and their
* paths
* @param root The object to generate a terminal property path/value set for
* @param current The current part of the given root that terminal properties are being sought
* @param initCurrent The current part of the given root that terminal properties are being sought
* within
* @param [context] An array containing the path to the current object root (intended for internal
* use)
@ -249,32 +249,30 @@ class Variables {
* @returns {TerminalProperty[]} The terminal properties of the given root object, with the path
* and value of each
*/
getProperties(root, atRoot, current, cntxt, rslts) {
let context = cntxt;
if (!context) {
context = [];
}
let results = rslts;
if (!results) {
results = [];
}
const addContext = (value, key) =>
this.getProperties(root, false, value, context.concat(key), results);
if (Array.isArray(current)) {
current.map(addContext);
} else if (
_.isObject(current) &&
!_.isDate(current) &&
!_.isRegExp(current) &&
typeof current !== 'function'
) {
if (atRoot || current !== root) {
_.mapValues(current, addContext);
getProperties(root, initAtRoot, initCurrent, initContext, initResults) {
const processedMap = new WeakMap();
return (function self(atRoot, current, context, results) {
if (processedMap.has(current)) return processedMap.get(current);
if (!context) context = [];
if (!results) results = [];
if (_.isObject(current)) processedMap.set(current, results);
const addContext = (value, key) => self(false, value, context.concat(key), results);
if (Array.isArray(current)) {
current.map(addContext);
} else if (
_.isObject(current) &&
!_.isDate(current) &&
!_.isRegExp(current) &&
typeof current !== 'function'
) {
if (atRoot || current !== root) {
_.mapValues(current, addContext);
}
} else {
results.push({ path: context, value: current });
}
} else {
results.push({ path: context, value: current });
}
return results;
return results;
})(initAtRoot, initCurrent, initContext, initResults);
}
/**