Add Collection.clone() (#1238)

* Add Collection.clone()

* More efficient cloning, and concat update

* Update Collection.js

* Update Collection.js
This commit is contained in:
bdistin
2017-03-01 03:41:36 -06:00
committed by Schuyler Cebulskie
parent 01d8d32ea9
commit 25bb602d5a

View File

@@ -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);
}