This repository has been archived by the owner on Oct 24, 2018. It is now read-only.
/
gtl_spec.coffee
150 lines (112 loc) · 5.76 KB
/
gtl_spec.coffee
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
gtl = require('../src/gtl.coffee')
chai = require('chai')
sinon = require('sinon')
sinonChai = require('sinon-chai')
chai.should()
chai.use(sinonChai)
describe 'Greater than less', ->
describe 'gtl.filter function', ->
describe 'greater/less rules', ->
it 'should filter array with greaterThan rule', ->
gtl.filter([1, 2, 3, 4, 5], greaterThan: 3).should.eql [4, 5]
gtl.filter([1, 2, 3, 4, 5], gt: 3).should.eql [4, 5]
it 'should filter array of strings with greaterThan rule', ->
gtl.filter(['a', 'b', 'c', 'd', 'e'], greaterThan: 'c').should.eql ['d', 'e']
it 'should filter array with greaterThanOrEqualTo rule', ->
gtl.filter([1, 2, 3, 4, 5], greaterThanOrEqualTo: 3).should.eql [3, 4, 5]
gtl.filter([1, 2, 3, 4, 5], gte: 3).should.eql [3, 4, 5]
gtl.filter([1, 2, 3, 4, 5], gteq: 3).should.eql [3, 4, 5]
it 'should filter array of strings with greaterThanOrEqualTo rule', ->
gtl.filter(['a', 'b', 'c', 'd', 'e'], greaterThanOrEqualTo: 'c').should.eql ['c', 'd', 'e']
it 'should filter array with lessThan rule', ->
gtl.filter([1, 2, 3, 4, 5], lessThan: 3).should.eql [1, 2]
gtl.filter([1, 2, 3, 4, 5], lt: 3).should.eql [1, 2]
it 'should filter array of strings with lessThan rule', ->
gtl.filter(['a', 'b', 'c', 'd', 'e'], lessThan: 'c').should.eql ['a', 'b']
it 'should filter array with lessThanOrEqualTo rule', ->
gtl.filter([1, 2, 3, 4, 5], lessThanOrEqualTo: 3).should.eql [1, 2, 3]
gtl.filter([1, 2, 3, 4, 5], lte: 3).should.eql [1, 2, 3]
gtl.filter([1, 2, 3, 4, 5], lteq: 3).should.eql [1, 2, 3]
it 'should filter array of strings with lessThanOrEqualTo rule', ->
gtl.filter(['a', 'b', 'c', 'd', 'e'], lessThanOrEqualTo: 'c').should.eql ['a', 'b', 'c']
it 'should filter array with only rule', ->
gtl.filter([1, 2, 3, 4, 5], only: 2).should.eql [2]
gtl.filter([1, 2, 3, 4, 5], only: [1, 2]).should.eql [1, 2]
it 'should filter array of strings with only rule', ->
gtl.filter(['a', 'b', 'c', 'd', 'e'], only: 'c').should.eql ['c']
describe 'inclusion and exclusion rules', ->
it 'should filter array with except rule', ->
gtl.filter([1, 2, 3, 4, 5], except: 3).should.eql [1, 2, 4, 5]
gtl.filter([1, 2, 3, 4, 5], not: 3).should.eql [1, 2, 4, 5]
gtl.filter([1, 2, 3, 4, 5], except: [1, 2]).should.eql [3, 4, 5]
it 'should filter array of strings with except rule', ->
gtl.filter(['a', 'b', 'c', 'd', 'e'], except: ['a', 'c']).should.eql ['b', 'd', 'e']
describe 'grep rule', ->
it 'should filter array by grepping strings', ->
gtl.filter(
['but break', 'my heart', 'for I must', 'hold my tongue']
grep: 'my'
).should.eql ['my heart', 'hold my tongue']
it 'should filter array by grepping RegExp', ->
gtl.filter(
['but break', 'my heart', 'for I must', 'hold my tongue']
grep: /m./
).should.eql ['my heart', 'for I must', 'hold my tongue']
describe 'fuzzy', ->
it 'should filter array by fuzzy search in strings', ->
gtl.filter(
['but break', 'my heart', 'for I must', 'hold my tongue']
fuzzy: 'ut'
).should.eql ['but break', 'for I must']
describe 'multiply', ->
it 'should filter by multiply rules', ->
gtl.filter([1, 2, 3, 4, 5], gt: 2, lte: 4).should.eql [3, 4]
describe 'filter arrays of objects through iterator', ->
it 'should use iterator passed as third argument', ->
gtl.filter(
[{ num : 1 }, { num : 2 }, { num : 3 }, { num : 4 }, { num : 5 }]
gte: 4
(obj) -> obj.num
).should.eql [{ num : 4 }, { num : 5 }]
describe 'add custom comparator', ->
it 'should works with custom rule added to gtl.rules', ->
gtl.rules.odd = (a) -> a % 2 == 1
gtl.filter([1, 2, 3, 4, 5], odd: true).should.eql [1, 3, 5]
gtl.rules.even = (a) -> a % 2 != 1
gtl.filter([1, 2, 3, 4, 5], even: true).should.eql [2, 4]
describe 'iterator rules', ->
it 'should filter array by specified field', ->
gtl.filter(
[{ body: { text: 'but break' } }, { body: { text: 'my heart' } }, { body: { text: 'for I must' } }, { body: { text: 'hold my tongue' } }]
grep: 'my'
in: 'body.text'
).should.eql [{ body: { text: 'my heart' } }, { body: { text: 'hold my tongue' } }]
it 'should filter array by specified fields', ->
gtl.filter(
[{ one: 1, two: 5 }, { one: 4, two: 4 }, { one: 4, two: -2 }, { one: 5, two: 7 }]
gt: 4
or: ['one', 'two']
).should.eql [{ one: 1, two: 5 }, { one: 5, two: 7 }]
it 'should filter array by specified fields where all elements satisfy the condition', ->
gtl.filter(
[{ one: 1, two: 5 }, { one: 4, two: 4 }, { one: 4, two: -2 }, { one: 5, two: 7 }]
gte: 4
and: ['one', 'two']
).should.eql [{ one: 4, two: 4 }, { one: 5, two: 7 }]
it 'should filter array by specified fields, combine or and and iterator rules', ->
gtl.filter(
[{ one: 1, two: 5, three: 4 }, { one: 4, two: 4, three: 9 }, { one: 4, two: -2, three: 3 }, { one: 5, two: 7, three: 1 }]
gte: 4
or: ['one', 'two']
and: 'three'
).should.eql [{ one: 1, two: 5, three: 4 }, { one: 4, two: 4, three: 9 }]
describe 'gtl.curry', ->
it 'should curry filter options', ->
findWilly = gtl.curry(fuzzy: 'willy')
findWilly(
['storm we are ill', 'is we ill yo', 'trololo will']
).should.eql ['is we ill yo']
describe 'gtl.clone', ->
it 'should clone gtl object', ->
cloned = gtl.clone()
cloned.rules.should.not.eq gtl.rules