@@ -754,14 +754,6 @@ describe('sort() tests', () => {
754
754
expect ( [ ...coll . values ( ) ] ) . toStrictEqual ( [ 1 , 2 , 3 ] ) ;
755
755
} ) ;
756
756
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
-
765
757
describe ( 'defaultSort' , ( ) => {
766
758
test ( 'stays the same if it is already sorted' , ( ) => {
767
759
const coll = createTestCollection ( ) ;
@@ -855,6 +847,48 @@ describe('union() tests', () => {
855
847
} ) ;
856
848
} ) ;
857
849
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
+
858
892
describe ( 'random thisArg tests' , ( ) => {
859
893
const coll = createCollectionFrom ( [ 'a' , 3 ] , [ 'b' , 2 ] , [ 'c' , 1 ] ) as Collection < string , unknown > ;
860
894
0 commit comments