-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
executable file
·168 lines (150 loc) · 5.48 KB
/
test.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
'use strict';
require('mocha');
require('should');
var arrSort = require('./');
describe('error tips', function() {
var arr = [{foo: 'y'}, {foo: 'z'}, {foo: 'x'}];
it('should throw an error when invalid type of array are passed', function() {
(function() {
arrSort({});
}).should.throw('array param MUST BE ARRAY');
});
it('should throw an error when invalid type of comparisonArgs are passed', function() {
(function() {
arrSort(arr, 2);
}).should.throw('comparisonArgs param MUST BE ARRAY');
});
it('should throw an error when array elements are not an object', function() {
(function() {
arrSort([2, 'string'], [{attr:'foo'}]);
}).should.throw('the item of array param MUST BE OBJECT');
});
it('should throw an error when comparisonArgs elements are not an object', function() {
(function() {
arrSort(arr, [2, 'string']);
}).should.throw('the item of comparisonArgs param MUST BE OBJECT');
});
});
describe('empty array', function() {
it('should return an empty array when null or undefined is passed', function() {
arrSort().should.eql([]);
arrSort(undefined).should.eql([]);
arrSort(null).should.eql([]);
})
});
describe('original array', function() {
var arr = [{foo: 'y'}, {foo: 'z'}, {foo: 'x'}];
it('should return an original array when comparisonArgs is null or undefined', function() {
arrSort(arr).should.eql([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}]);
arrSort(arr, undefined).should.eql([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}]);
arrSort(arr, null).should.eql([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}]);
})
});
describe('basic sort', function() {
it('should sort an array by the given object property', function() {
var out = arrSort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}],[{attr:'foo'}]);
out.should.eql([{foo: 'x'}, {foo: 'y'}, {foo: 'z'}]);
});
it('should sort an array in reverse order by the given object property', function() {
var out = arrSort([{foo: 'y'}, {foo: 'z'}, {foo: 'x'}],[{attr:'foo', asc: false}]);
out.should.eql([{foo: 'z'}, {foo: 'y'}, {foo: 'x'}]);
});
});
describe('advanced sort', function() {
it('should sort by multiple properties', function() {
var array = [
{ foo: 'bbb', num: 4, flag: 2 },
{ foo: 'aaa', num: 3, flag: 1 },
{ foo: 'ccc', num: -6, flag: 2 },
{ foo: 'ccc', num: 8, flag: 2 },
{ foo: 'bbb', num: 2, flag: 4 },
{ foo: 'aaa', num: -3, flag: 4 }
];
var result = arrSort(array,
[{
attr: 'flag',
asc: true
},
{
attr: 'foo',
asc: false
},
{
attr: 'num',
asc: true
}]
);
result.should.eql([
{ foo: 'aaa', num: 3, flag: 1},
{ foo: 'ccc', num: -6, flag: 2},
{ foo: 'ccc', num: 8, flag: 2},
{ foo: 'bbb', num: 4, flag: 2},
{ foo: 'bbb', num: 2, flag: 4},
{ foo: 'aaa', num: -3, flag: 4}
]);
});
it('should sort by nested properties', function() {
var array = [
{ locals: { foo: 'bbb', num: 4 }, flag: 2},
{ locals: { foo: 'aaa', num: 3 }, flag: 1},
{ locals: { foo: 'ccc', num: -6 }, flag: 2},
{ locals: { foo: 'ccc', num: 8 }, flag: 2},
{ locals: { foo: 'bbb', num: 2 }, flag: 4},
{ locals: { foo: 'aaa', num: -3 }, flag: 4},
];
var result = arrSort(array,
[{
attr: 'flag',
asc: true
},
{
attr: 'locals.foo',
asc: false
},
{
attr: 'locals.num',
asc: true
}]
);
result.should.eql([
{ locals: { foo: 'aaa', num: 3 }, flag: 1},
{ locals: { foo: 'ccc', num: -6 }, flag: 2},
{ locals: { foo: 'ccc', num: 8 }, flag: 2},
{ locals: { foo: 'bbb', num: 4 }, flag: 2},
{ locals: { foo: 'bbb', num: 2 }, flag: 4},
{ locals: { foo: 'aaa', num: -3 }, flag: 4}
]);
});
it('should sort by custom function', function() {
var array = [
{ locals: { foo: 'bbb', num: 4 }, flag: -2},
{ locals: { foo: 'aaa', num: 3 }, flag: 1},
{ locals: { foo: 'ccc', num: -6 }, flag: 2},
{ locals: { foo: 'ccc', num: 8 }, flag: 2},
{ locals: { foo: 'bbb', num: 2 }, flag: 4},
{ locals: { foo: 'aaa', num: -3 }, flag: 4},
];
var result = arrSort(array,
[{
attr: 'flag',
asc: function(a,b){return (Math.abs(a) - Math.abs(b))}
},
{
attr: 'locals.foo',
asc: false
},
{
attr: 'locals.num',
asc: true
}]
);
result.should.eql([
{ locals: { foo: 'aaa', num: 3 }, flag: 1},
{ locals: { foo: 'ccc', num: -6 }, flag: 2},
{ locals: { foo: 'ccc', num: 8 }, flag: 2},
{ locals: { foo: 'bbb', num: 4 }, flag: -2},
{ locals: { foo: 'bbb', num: 2 }, flag: 4},
{ locals: { foo: 'aaa', num: -3 }, flag: 4}
]);
});
});