Skip to content

Commit

Permalink
fix(Util): flatten ignoring certain fields (v13) (#8936)
Browse files Browse the repository at this point in the history
Fixes #8929
  • Loading branch information
almeidx committed Dec 16, 2022
1 parent ca662b4 commit 4ec3355
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/util/Util.js
Expand Up @@ -41,15 +41,20 @@ class Util extends null {
const element = obj[prop];
const elemIsObj = isObject(element);
const valueOf = elemIsObj && typeof element.valueOf === 'function' ? element.valueOf() : null;
const hasToJSON = elemIsObj && typeof element.toJSON === 'function';

// If it's a Collection, make the array of keys
if (element instanceof Collection) out[newProp] = Array.from(element.keys());
// If the valueOf is a Collection, use its array of keys
else if (valueOf instanceof Collection) out[newProp] = Array.from(valueOf.keys());
// If it's an array, flatten each element
else if (Array.isArray(element)) out[newProp] = element.map(e => Util.flatten(e));
// If it's an array, call toJSON function on each element if present, otherwise flatten each element
else if (Array.isArray(element)) out[newProp] = element.map(e => e.toJSON?.() ?? Util.flatten(e));
// If it's an object with a primitive `valueOf`, use that value
else if (typeof valueOf !== 'object') out[newProp] = valueOf;
// If it's an object with a toJSON function, use the return value of it
else if (hasToJSON) out[newProp] = element.toJSON();
// If element is an object, use the flattened version of it
else if (typeof element === 'object') out[newProp] = Util.flatten(element);
// If it's a primitive
else if (!elemIsObj) out[newProp] = element;
}
Expand Down

0 comments on commit 4ec3355

Please sign in to comment.