Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Util): flatten ignoring certain fields #7773

Merged
merged 4 commits into from
Apr 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/discord.js/src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,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