From 594836b1bf2d6c091becc6cc10ba4d3949e6bd44 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Thu, 8 Sep 2016 02:26:28 -0400 Subject: [PATCH] Rewrite Collection.map/filter, and add some/every --- src/util/Collection.js | 59 +++++++++++++++++++++++++++++++++++------- 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/src/util/Collection.js b/src/util/Collection.js index 1098d454e..838770dc9 100644 --- a/src/util/Collection.js +++ b/src/util/Collection.js @@ -111,25 +111,64 @@ class Collection extends Map { * Identical to * [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter), * but returns a Collection instead of an Array. - * @param {function} callback Function used to filter (should return a boolean) - * @param {Object} [thisArg] Value to set as this when filtering + * @param {function} fn Function used to test (should return a boolean) + * @param {Object} [thisArg] Value to use as `this` when executing function * @returns {Collection} */ - filter(...args) { - const newArray = this.array().filter(...args); + filter(fn, thisArg) { + if (thisArg) fn = fn.bind(thisArg); 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; } /** - * Functionally identical shortcut to `collection.array().map(...)`. - * @param {function} callback Function that produces an element of the new Array, taking three arguments - * @param {*} [thisArg] Optional. Value to use as this when executing callback. + * Identical to + * [Array.map()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map). + * @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} */ - map(...args) { - return this.array().map(...args); + map(fn, thisArg) { + 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; } }