deno.land / x / rambda@v9.1.1 / source / sortWith.spec.js

sortWith.spec.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { ascend, prop } from '../rambda.js'import { sortWith } from './sortWith.js'
const albums = [ { artist : 'Rush', genre : 'Rock', score : 3, title : 'A Farewell to Kings', }, { artist : 'Dave Brubeck Quartet', genre : 'Jazz', score : 3, title : 'Timeout', }, { artist : 'Rush', genre : 'Rock', score : 5, title : 'Fly By Night', }, { artist : 'Daniel Barenboim', genre : 'Baroque', score : 3, title : 'Goldberg Variations', }, { artist : 'Glenn Gould', genre : 'Baroque', score : 3, title : 'Art of the Fugue', }, { artist : 'Leonard Bernstein', genre : 'Romantic', score : 4, title : 'New World Symphony', }, { artist : 'Don Byron', genre : 'Jazz', score : 5, title : 'Romance with the Unseen', }, { artist : 'Iron Maiden', genre : 'Metal', score : 2, title : 'Somewhere In Time', }, { artist : 'Danny Holt', genre : 'Modern', score : 1, title : 'In Times of Desparation', }, { artist : 'Various', genre : 'Broadway', score : 3, title : 'Evita', }, { artist : 'Nick Drake', genre : 'Folk', score : 1, title : 'Five Leaves Left', }, { artist : 'John Eliot Gardiner', genre : 'Classical', score : 4, title : 'The Magic Flute', },]
test('sorts by a simple property of the objects', () => { const sortedAlbums = sortWith([ ascend(prop('title')) ], albums) expect(sortedAlbums).toHaveLength(albums.length) expect(sortedAlbums[ 0 ].title).toBe('A Farewell to Kings') expect(sortedAlbums[ 11 ].title).toBe('Timeout')})
test('sorts by multiple properties of the objects', () => { const sortedAlbums = sortWith([ ascend(prop('score')), ascend(prop('title')) ], albums) expect(sortedAlbums).toHaveLength(albums.length) expect(sortedAlbums[ 0 ].title).toBe('Five Leaves Left') expect(sortedAlbums[ 1 ].title).toBe('In Times of Desparation') expect(sortedAlbums[ 11 ].title).toBe('Romance with the Unseen')})
test('sorts by 3 properties of the objects', () => { const sortedAlbums = sortWith([ ascend(prop('genre')), ascend(prop('score')), ascend(prop('title')) ], albums) expect(sortedAlbums).toHaveLength(albums.length) expect(sortedAlbums[ 0 ].title).toBe('Art of the Fugue') expect(sortedAlbums[ 1 ].title).toBe('Goldberg Variations') expect(sortedAlbums[ 11 ].title).toBe('New World Symphony')})
test('sorts by multiple properties using ascend and descend', () => { const sortedAlbums = sortWith([ ascend(prop('score')), ascend(prop('title')) ], albums) expect(sortedAlbums).toHaveLength(albums.length) expect(sortedAlbums[ 0 ].title).toBe('Five Leaves Left') expect(sortedAlbums[ 1 ].title).toBe('In Times of Desparation') expect(sortedAlbums[ 11 ].title).toBe('Romance with the Unseen')})
test('sorts only arrays not array-like object', () => { const args = (function (){ return arguments })( 'c', 'a', 'b' ) expect(sortWith([ ascend(prop('value')) ], args)).toEqual([])})
test('sorts only arrays not primitives', () => { const result =sortWith([ (a, b) => a.a === b.a ? 0 : a.a > b.a ? 1 : -1, (a, b) => a.b === b.b ? 0 : a.b > b.b ? 1 : -1, ], [ {a: 1, b: 2}, {a: 2, b: 1}, {a: 2, b: 2}, {a: 1, b: 1}, ]) const expected = [ {a: 1, b: 1}, {a: 1, b: 2}, {a: 2, b: 1}, {a: 2, b: 2}, ] expect(result).toEqual(expected)})
rambda

Version Info

Tagged at
2 months ago