feat: align some methods with the Set Methods proposal (#8890)

feat(collection): align/add methods with/from Set Methods proposal

BREAKING CHANGE: The `intersect` method has been renamed to `intersection`
BREAKING CHANGE: The `difference` method has been renamed to `symmetricDifference`
BREAKING CHANGE: The `subtract` method has been renamed to `difference`
This commit is contained in:
Almeida
2023-11-07 18:04:51 +00:00
committed by GitHub
parent 054eaec7d7
commit 3b8df63a5a
2 changed files with 119 additions and 59 deletions

View File

@@ -751,48 +751,71 @@ export class Collection<K, V> extends Map<K, V> {
}
/**
* The intersect method returns a new structure containing items where the keys and values are present in both original structures.
* The intersection method returns a new collection containing the items where the key is present in both collections.
*
* @param other - The other Collection to filter against
* @example
* ```ts
* const col1 = new Collection([['a', 1], ['b', 2]]);
* const col2 = new Collection([['a', 1], ['c', 3]]);
* const intersection = col1.intersection(col2);
* console.log(col1.intersection(col2));
* // => Collection { 'a' => 1 }
* ```
*/
public intersect<T>(other: ReadonlyCollection<K, T>): Collection<K, T> {
const coll = new this.constructor[Symbol.species]<K, T>();
for (const [key, value] of other) {
if (this.has(key) && Object.is(value, this.get(key))) {
coll.set(key, value);
}
}
return coll;
}
/**
* The subtract method returns a new structure containing items where the keys and values of the original structure are not present in the other.
*
* @param other - The other Collection to filter against
*/
public subtract<T>(other: ReadonlyCollection<K, T>): Collection<K, V> {
const coll = new this.constructor[Symbol.species]<K, V>();
for (const [key, value] of this) {
if (!other.has(key) || !Object.is(value, other.get(key))) {
coll.set(key, value);
}
}
return coll;
}
/**
* The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.
*
* @param other - The other Collection to filter against
*/
public difference<T>(other: ReadonlyCollection<K, T>): Collection<K, T | V> {
public intersection<T>(other: ReadonlyCollection<K, T>): Collection<K, T | V> {
const coll = new this.constructor[Symbol.species]<K, T | V>();
for (const [key, value] of other) {
if (!this.has(key)) coll.set(key, value);
for (const [key, value] of this) {
if (other.has(key)) coll.set(key, value);
}
return coll;
}
/**
* Returns a new collection containing the items where the key is present in either of the collections.
*
* @remarks
*
* If the collections have any items with the same key, the value from the first collection will be used.
* @param other - The other Collection to filter against
* @example
* ```ts
* const col1 = new Collection([['a', 1], ['b', 2]]);
* const col2 = new Collection([['a', 1], ['b', 3], ['c', 3]]);
* const union = col1.union(col2);
* console.log(union);
* // => Collection { 'a' => 1, 'b' => 2, 'c' => 3 }
* ```
*/
public union<T>(other: ReadonlyCollection<K, T>): Collection<K, T | V> {
const coll = new this.constructor[Symbol.species]<K, T | V>(this);
for (const [key, value] of other) {
if (!coll.has(key)) coll.set(key, value);
}
return coll;
}
/**
* Returns a new collection containing the items where the key is present in this collection but not the other.
*
* @param other - The other Collection to filter against
* @example
* ```ts
* const col1 = new Collection([['a', 1], ['b', 2]]);
* const col2 = new Collection([['a', 1], ['c', 3]]);
* console.log(col1.difference(col2));
* // => Collection { 'b' => 2 }
* console.log(col2.difference(col1));
* // => Collection { 'c' => 3 }
* ```
*/
public difference<T>(other: ReadonlyCollection<K, T>): Collection<K, V> {
const coll = new this.constructor[Symbol.species]<K, V>();
for (const [key, value] of this) {
if (!other.has(key)) coll.set(key, value);
}
@@ -800,6 +823,33 @@ export class Collection<K, V> extends Map<K, V> {
return coll;
}
/**
* Returns a new collection containing only the items where the keys are present in either collection, but not both.
*
* @param other - The other Collection to filter against
* @example
* ```ts
* const col1 = new Collection([['a', 1], ['b', 2]]);
* const col2 = new Collection([['a', 1], ['c', 3]]);
* const symmetricDifference = col1.symmetricDifference(col2);
* console.log(col1.symmetricDifference(col2));
* // => Collection { 'b' => 2, 'c' => 3 }
* ```
*/
public symmetricDifference<T>(other: ReadonlyCollection<K, T>): Collection<K, T | V> {
const coll = new this.constructor[Symbol.species]<K, T | V>();
for (const [key, value] of this) {
if (!other.has(key)) coll.set(key, value);
}
for (const [key, value] of other) {
if (!this.has(key)) coll.set(key, value);
}
return coll;
}
/**
* Merges two Collections together into a new Collection.
*