-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
69 lines (58 loc) · 1.9 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
var expect = require('chai').expect
, az = require('./')
describe('az', function() {
it('should sort by value', function() {
var a = [{ val: 2 }, { val: 3 }, { val: 1 }, { val: 2 }]
expect(a.sort(az('val'))).to.be.eql([{ val: 1 }, { val: 2 }, { val: 2 }, { val: 3 }])
})
it('should sort property-less items too', function() {
var a = [{}, { val: 2 }, {}, { val: 3 }, {}, { val: 1 }, {}, { val: 2 }]
expect(a.sort(az('val'))).to.be.eql([{}, {}, {}, {}, { val: 1 }, { val: 2 }, { val: 2 }, { val: 3 }])
})
it('should sort by string property', function() {
var a = [{}, { val: 'b' }, {}, { val: 'c' }, {}, { val: 'a' }, {}, { val: 'b' }]
expect(a.sort(az('val'))).to.be.eql([{}, {}, {}, {}, { val: 'a' }, { val: 'b' }, { val: 'b' }, { val: 'c' }])
})
it('should accept fn as key', function() {
var a = [
{ base: 'USD', quote: 'GBP' }
, { base: 'CAD', quote: 'GBP' }
, { base: 'USD', quote: 'CHF' }
, { base: 'CAD', quote: 'CHF' }
, { base: 'USD', quote: 'EUR' }
, { base: 'CAD', quote: 'EUR' }
]
expect(a.sort(az(ccy))).to.be.eql([
{ base: 'CAD', quote: 'CHF' }
, { base: 'CAD', quote: 'EUR' }
, { base: 'CAD', quote: 'GBP' }
, { base: 'USD', quote: 'CHF' }
, { base: 'USD', quote: 'EUR' }
, { base: 'USD', quote: 'GBP' }
])
function ccy(d) {
return d.base + d.quote
}
})
it('should accept multiple keys', function() {
var a = [
{ name: 'B', ip: '1' }
, { name: 'C', ip: 'Y' }
, { name: 'C', ip: 'X' }
, { name: 'A', ip: '1' }
, { name: 'A', ip: '2' }
, { name: 'B', ip: '2' }
]
expect(a.sort(az('name', 'ip'))).to.be.eql([
{ name: 'A', ip: '1' }
, { name: 'A', ip: '2' }
, { name: 'B', ip: '1' }
, { name: 'B', ip: '2' }
, { name: 'C', ip: 'X' }
, { name: 'C', ip: 'Y' }
])
function ccy(d) {
return d.base + d.quote
}
})
})