Skip to content

Commit

Permalink
fix(Variables): Fix handling of circular object references
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Oct 5, 2020
1 parent fc34140 commit fd451ca
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions lib/classes/Variables.js
Expand Up @@ -241,40 +241,38 @@ 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)
* @param [results] An array of current results (intended for internal use)
* @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);
}

/**
Expand Down

0 comments on commit fd451ca

Please sign in to comment.