From 25bb602d5a2bfeb249f917d854ff2fc78dbef78a Mon Sep 17 00:00:00 2001 From: bdistin Date: Wed, 1 Mar 2017 03:41:36 -0600 Subject: [PATCH] Add Collection.clone() (#1238) * Add Collection.clone() * More efficient cloning, and concat update * Update Collection.js * Update Collection.js --- src/util/Collection.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/util/Collection.js b/src/util/Collection.js index 13ab31cc3..36d13fdd3 100644 --- a/src/util/Collection.js +++ b/src/util/Collection.js @@ -319,6 +319,15 @@ class Collection extends Map { return accumulator; } + /** + * Creates an identical shallow copy of this collection. + * @returns {Collection} + * @example const newColl = someColl.clone(); + */ + clone() { + return new this.constructor(this); + } + /** * Combines this collection with others into a new collection. None of the source collections are modified. * @param {...Collection} collections Collections to merge @@ -326,8 +335,7 @@ class Collection extends Map { * @example const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl); */ concat(...collections) { - const newColl = new this.constructor(); - for (const [key, val] of this) newColl.set(key, val); + const newColl = this.clone(); for (const coll of collections) { for (const [key, val] of coll) newColl.set(key, val); }