Skip to content

Commit

Permalink
Add Collection.clone() (#1238)
Browse files Browse the repository at this point in the history
* Add Collection.clone()

* More efficient cloning, and concat update

* Update Collection.js

* Update Collection.js
  • Loading branch information
bdistin authored and Gawdl3y committed Mar 1, 2017
1 parent 01d8d32 commit 25bb602
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/util/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,23 @@ 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
* @returns {Collection}
* @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);
}
Expand Down

0 comments on commit 25bb602

Please sign in to comment.