feat(Collection): backport partition method

Commit: a732402c95
PR: #2511
This commit is contained in:
SpaceEEC
2018-05-25 16:49:10 +02:00
parent f921261f3f
commit f456f4c3c0

View File

@@ -308,6 +308,27 @@ class Collection extends Map {
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
* [Array.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map).