Rewrite Collection.map/filter, and add some/every

This commit is contained in:
Schuyler Cebulskie
2016-09-08 02:26:28 -04:00
parent 978cf9d87f
commit 594836b1bf

View File

@@ -111,25 +111,64 @@ class Collection extends Map {
* Identical to * Identical to
* [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter), * [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter),
* but returns a Collection instead of an Array. * but returns a Collection instead of an Array.
* @param {function} callback Function used to filter (should return a boolean) * @param {function} fn Function used to test (should return a boolean)
* @param {Object} [thisArg] Value to set as this when filtering * @param {Object} [thisArg] Value to use as `this` when executing function
* @returns {Collection} * @returns {Collection}
*/ */
filter(...args) { filter(fn, thisArg) {
const newArray = this.array().filter(...args); if (thisArg) fn = fn.bind(thisArg);
const collection = new Collection(); const collection = new Collection();
for (const item of newArray) collection.set(item.id, item); for (const [key, val] of this) {
if (fn(val, key, this)) collection.set(key, val);
}
return collection; return collection;
} }
/** /**
* Functionally identical shortcut to `collection.array().map(...)`. * Identical to
* @param {function} callback Function that produces an element of the new Array, taking three arguments * [Array.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map).
* @param {*} [thisArg] Optional. Value to use as this when executing callback. * @param {function} fn Function that produces an element of the new array, taking three arguments
* @param {*} [thisArg] Value to use as `this` when executing function
* @returns {array} * @returns {array}
*/ */
map(...args) { map(fn, thisArg) {
return this.array().map(...args); if (thisArg) fn = fn.bind(thisArg);
const arr = new Array(this.size);
let i = 0;
for (const [key, val] of this) {
arr[i++] = fn(val, key, this);
}
return arr;
}
/**
* Identical to
* [Array.some()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some).
* @param {function} fn Function used to test (should return a boolean)
* @param {Object} [thisArg] Value to use as `this` when executing function
* @returns {Collection}
*/
some(fn, thisArg) {
if (thisArg) fn = fn.bind(thisArg);
for (const [key, val] of this) {
if (fn(val, key, this)) return true;
}
return false;
}
/**
* Identical to
* [Array.every()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every).
* @param {function} fn Function used to test (should return a boolean)
* @param {Object} [thisArg] Value to use as `this` when executing function
* @returns {Collection}
*/
every(fn, thisArg) {
if (thisArg) fn = fn.bind(thisArg);
for (const [key, val] of this) {
if (!fn(val, key, this)) return false;
}
return true;
} }
} }