From 6baf43dc24396b28a7017fa727266d574cf736e1 Mon Sep 17 00:00:00 2001 From: Schuyler Cebulskie Date: Sat, 15 Oct 2016 01:20:31 -0400 Subject: [PATCH] Add Collection.filterArray --- src/util/Collection.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/util/Collection.js b/src/util/Collection.js index b70cec32f..53299023a 100644 --- a/src/util/Collection.js +++ b/src/util/Collection.js @@ -205,11 +205,27 @@ class Collection extends Map { */ filter(fn, thisArg) { if (thisArg) fn = fn.bind(thisArg); - const collection = new Collection(); + const results = new Collection(); for (const [key, val] of this) { - if (fn(val, key, this)) collection.set(key, val); + if (fn(val, key, this)) results.set(key, val); } - return collection; + return results; + } + + /** + * Identical to + * [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter). + * @param {function} fn Function used to test (should return a boolean) + * @param {Object} [thisArg] Value to use as `this` when executing function + * @returns {Collection} + */ + filterArray(fn, thisArg) { + if (thisArg) fn = fn.bind(thisArg); + const results = []; + for (const [key, val] of this) { + if (fn(val, key, this)) results.push(val); + } + return results; } /**