Skip to content

Commit 65966ae

Browse files
authoredNov 7, 2023
feat: align some methods with the Change By Copy proposal (#9207)
BREAKING CHANGE: The `sorted` method has been renamed to `toSorted`
1 parent 3b8df63 commit 65966ae

File tree

2 files changed

+51
-9
lines changed

2 files changed

+51
-9
lines changed
 

‎packages/collection/__tests__/collection.test.ts

+42-8
Original file line numberDiff line numberDiff line change
@@ -754,14 +754,6 @@ describe('sort() tests', () => {
754754
expect([...coll.values()]).toStrictEqual([1, 2, 3]);
755755
});
756756

757-
test('sort a collection', () => {
758-
const coll = createCollectionFrom(['a', 3], ['b', 2], ['c', 1]);
759-
expect([...coll.values()]).toStrictEqual([3, 2, 1]);
760-
const sorted = coll.sorted((a, b) => a - b);
761-
expect([...coll.values()]).toStrictEqual([3, 2, 1]);
762-
expect([...sorted.values()]).toStrictEqual([1, 2, 3]);
763-
});
764-
765757
describe('defaultSort', () => {
766758
test('stays the same if it is already sorted', () => {
767759
const coll = createTestCollection();
@@ -855,6 +847,48 @@ describe('union() tests', () => {
855847
});
856848
});
857849

850+
describe('toReversed() tests', () => {
851+
test('reverses a collection', () => {
852+
const coll = createTestCollection();
853+
const reversed = coll.toReversed();
854+
expect([...reversed.entries()]).toStrictEqual([
855+
['c', 3],
856+
['b', 2],
857+
['a', 1],
858+
]);
859+
});
860+
861+
test('does not the modify original collection', () => {
862+
const coll = createTestCollection();
863+
const originalEntries = [...coll.entries()];
864+
const reversed = coll.toReversed();
865+
866+
expect(reversed).not.toBe(coll);
867+
expect([...coll.entries()]).toStrictEqual(originalEntries);
868+
});
869+
});
870+
871+
describe('toSorted() tests', () => {
872+
test('sorts a collection', () => {
873+
const coll = createCollectionFrom(['a', 3], ['b', 2], ['c', 1]);
874+
const sorted = coll.toSorted((a, b) => a - b);
875+
expect([...sorted.entries()]).toStrictEqual([
876+
['c', 1],
877+
['b', 2],
878+
['a', 3],
879+
]);
880+
});
881+
882+
test('does not modify the original collection', () => {
883+
const coll = createCollectionFrom(['a', 3], ['b', 2], ['c', 1]);
884+
const originalEntries = [...coll.entries()];
885+
const sorted = coll.toSorted();
886+
887+
expect(sorted).not.toBe(coll);
888+
expect([...coll.entries()]).toStrictEqual(originalEntries);
889+
});
890+
});
891+
858892
describe('random thisArg tests', () => {
859893
const coll = createCollectionFrom(['a', 3], ['b', 2], ['c', 1]) as Collection<string, unknown>;
860894

‎packages/collection/src/collection.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,14 @@ export class Collection<K, V> extends Map<K, V> {
906906
return coll;
907907
}
908908

909+
/**
910+
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed | Array.toReversed()}
911+
* but returns a Collection instead of an Array.
912+
*/
913+
public toReversed() {
914+
return new this.constructor[Symbol.species](this).reverse();
915+
}
916+
909917
/**
910918
* The sorted method sorts the items of a collection and returns it.
911919
* The sort is not necessarily stable in Node 10 or older.
@@ -919,7 +927,7 @@ export class Collection<K, V> extends Map<K, V> {
919927
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
920928
* ```
921929
*/
922-
public sorted(compareFunction: Comparator<K, V> = Collection.defaultSort) {
930+
public toSorted(compareFunction: Comparator<K, V> = Collection.defaultSort) {
923931
return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk));
924932
}
925933

0 commit comments

Comments
 (0)
Please sign in to comment.