mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 20:13:30 +01:00
feat(Collection): backport partition method
Commit: a732402c95
PR: #2511
This commit is contained in:
@@ -308,6 +308,27 @@ class Collection extends Map {
|
|||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Partitions the collection into two collections where the first collection
|
||||||
|
* contains the items that passed and the second contains the items that failed.
|
||||||
|
* @param {Function} fn Function used to test (should return a boolean)
|
||||||
|
* @param {*} [thisArg] Value to use as `this` when executing function
|
||||||
|
* @returns {Collection[]}
|
||||||
|
* @example const [big, small] = collection.partition(guild => guild.memberCount > 250);
|
||||||
|
*/
|
||||||
|
partition(fn, thisArg) {
|
||||||
|
if (typeof thisArg !== 'undefined') fn = fn.bind(thisArg);
|
||||||
|
const results = [new Collection(), new Collection()];
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this)) {
|
||||||
|
results[0].set(key, val);
|
||||||
|
} else {
|
||||||
|
results[1].set(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Identical to
|
* Identical to
|
||||||
* [Array.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map).
|
* [Array.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map).
|
||||||
|
|||||||
Reference in New Issue
Block a user