feat: align some methods with the Change By Copy proposal (#9207)

BREAKING CHANGE: The `sorted` method has been renamed to `toSorted`
This commit is contained in:
Almeida
2023-11-07 18:26:48 +00:00
committed by GitHub
parent 3b8df63a5a
commit 65966ae6ec
2 changed files with 51 additions and 9 deletions

View File

@@ -906,6 +906,14 @@ export class Collection<K, V> extends Map<K, V> {
return coll;
}
/**
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed | Array.toReversed()}
* but returns a Collection instead of an Array.
*/
public toReversed() {
return new this.constructor[Symbol.species](this).reverse();
}
/**
* The sorted method sorts the items of a collection and returns it.
* The sort is not necessarily stable in Node 10 or older.
@@ -919,7 +927,7 @@ export class Collection<K, V> extends Map<K, V> {
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
* ```
*/
public sorted(compareFunction: Comparator<K, V> = Collection.defaultSort) {
public toSorted(compareFunction: Comparator<K, V> = Collection.defaultSort) {
return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk));
}