forked from ramda/ramda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsortWith.js
81 lines (71 loc) · 3.02 KB
/
sortWith.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
var R = require('../source/index.js');
var eq = require('./shared/eq.js');
var albums = [
{title: 'A Farewell to Kings', artist: 'Rush', genre: 'Rock', score: 3},
{title: 'Timeout', artist: 'Dave Brubeck Quartet', genre: 'Jazz', score: 3},
{title: 'Fly By Night', artist: 'Rush', genre: 'Rock', score: 5},
{title: 'Goldberg Variations', artist: 'Daniel Barenboim', genre: 'Baroque', score: 3},
{title: 'Art of the Fugue', artist: 'Glenn Gould', genre: 'Baroque', score: 3},
{title: 'New World Symphony', artist: 'Leonard Bernstein', genre: 'Romantic', score: 4},
{title: 'Romance with the Unseen', artist: 'Don Byron', genre: 'Jazz', score: 5},
{title: 'Somewhere In Time', artist: 'Iron Maiden', genre: 'Metal', score: 2},
{title: 'In Times of Desparation', artist: 'Danny Holt', genre: 'Modern', score: 1},
{title: 'Evita', artist: 'Various', genre: 'Broadway', score: 3},
{title: 'Five Leaves Left', artist: 'Nick Drake', genre: 'Folk', score: 1},
{title: 'The Magic Flute', artist: 'John Eliot Gardiner', genre: 'Classical', score: 4}
];
describe('sortWith', function() {
it('sorts by a simple property of the objects', function() {
var sortedAlbums = R.sortWith([
R.ascend(R.prop('title'))
], albums);
eq(sortedAlbums.length, albums.length);
eq(sortedAlbums[0].title, 'A Farewell to Kings');
eq(sortedAlbums[11].title, 'Timeout');
});
it('sorts by multiple properties of the objects', function() {
var sortedAlbums = R.sortWith([
R.ascend(R.prop('score')),
R.ascend(R.prop('title'))
], albums);
eq(sortedAlbums.length, albums.length);
eq(sortedAlbums[0].title, 'Five Leaves Left');
eq(sortedAlbums[1].title, 'In Times of Desparation');
eq(sortedAlbums[11].title, 'Romance with the Unseen');
});
it('sorts by 3 properties of the objects', function() {
var sortedAlbums = R.sortWith([
R.ascend(R.prop('genre')),
R.ascend(R.prop('score')),
R.ascend(R.prop('title'))
], albums);
eq(sortedAlbums.length, albums.length);
eq(sortedAlbums[0].title, 'Art of the Fugue');
eq(sortedAlbums[1].title, 'Goldberg Variations');
eq(sortedAlbums[11].title, 'New World Symphony');
});
it('sorts by multiple properties using ascend and descend', function() {
var sortedAlbums = R.sortWith([
R.descend(R.prop('score')),
R.ascend(R.prop('title'))
], albums);
eq(sortedAlbums.length, albums.length);
eq(sortedAlbums[0].title, 'Fly By Night');
eq(sortedAlbums[1].title, 'Romance with the Unseen');
eq(sortedAlbums[11].title, 'In Times of Desparation');
});
it('preserves object identity', function() {
var a = {value: 'a'};
var b = {value: 'b'};
var result = R.sortWith([R.ascend(R.prop('value'))], [b, a]);
eq(result[0], a);
eq(result[1], b);
});
it('sorts array-like object', function() {
var args = (function() { return arguments; }('c', 'a', 'b'));
var result = R.sortWith([R.ascend(R.identity)], args);
eq(result[0], 'a');
eq(result[1], 'b');
eq(result[2], 'c');
});
});