From 9f918199d2146ef06fb4d2fffc46c7975db8b441 Mon Sep 17 00:00:00 2001 From: 1Computer1 Date: Fri, 4 May 2018 03:08:32 -0400 Subject: [PATCH] feat: add Collection#tap (#2507) * Add Collection#inspect * No u * Rename to tap * Rename variable * Do it here too --- src/util/Collection.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/util/Collection.js b/src/util/Collection.js index 6fdffc943..531f84d28 100644 --- a/src/util/Collection.js +++ b/src/util/Collection.js @@ -364,6 +364,24 @@ class Collection extends Map { return accumulator; } + /** + * Identical to + * [Map.forEach()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach), + * but returns the collection instead of undefined. + * @param {Function} fn Function to execute for each element + * @param {*} [thisArg] Value to use as `this` when executing function + * @returns {Collection} + * @example + * collection + * .tap(user => console.log(user.username)) + * .filter(user => user.bot) + * .tap(user => console.log(user.username)); + */ + tap(fn, thisArg) { + this.forEach(fn, thisArg); + return this; + } + /** * Creates an identical shallow copy of this collection. * @returns {Collection}