This repository has been archived by the owner on Oct 24, 2018. It is now read-only.
/
gtl.coffee
233 lines (182 loc) · 5.03 KB
/
gtl.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
###
______ ______ __
/\ ___\ /\__ _\ /\ \
\ \ \__ \ \/_/\ \/ \ \ \____
\ \_____\ \ \_\ \ \_____\
\/_____/ \/_/ \/_____/
gtl.js ~ v0.1.0 ~ https://github.com/kossnocorp/gtl
Greater than less
The MIT License
Copyright (c) 2012 Sasha Koss
###
Gtl = {}
# Comparator class
class Gtl.Comparator
constructor: (names...) ->
# Greater than comparator
class Gtl.GreaterThanComparator extends Gtl.Comparator
names: ['gt', 'greaterThan']
compare: (a, b) -> a > b
# Greater than or equal to comparator
class Gtl.GreaterThanOrEqualToComparator extends Gtl.Comparator
names: ['gte', 'gteq', 'greaterThanOrEqualTo']
compare: (a, b) -> a >= b
# Less than comparator
class Gtl.LessThanComparator extends Gtl.Comparator
names: ['lt', 'lessThan']
compare: (a, b) -> a < b
# Less than or equal to comparator
class Gtl.LessThanOrEqualToComparator extends Gtl.Comparator
names: ['lte', 'lteq', 'lessThanOrEqualTo']
compare: (a, b) -> a <= b
# Only comparator
class Gtl.OnlyComparator extends Gtl.Comparator
names: ['only']
compare: (a, bs) ->
if bs.constructor == Array
bs.indexOf(a) != -1
else
a == bs
# Except comparator
class Gtl.ExceptComparator extends Gtl.Comparator
names: ['not', 'except']
compare: (a, bs) ->
not gtl.comparators.only(a, bs)
# Grep comparator
class Gtl.GrepComparator extends Gtl.Comparator
names: ['grep']
compare: (str, substr) ->
str.search(substr) != -1
# Fuzzy comparator
class Gtl.FuzzyComparator extends Gtl.Comparator
names: ['fuzzy']
compare: (str, searchStr) ->
subStr = str
for char in searchStr
if -1 != i = subStr.search(char)
subStr = subStr.slice(i + 1)
else
return false
true
# Comparators collection
class Gtl.ComparatorSet
constructor: ->
@comparators = []
add: (klass) ->
@comparators.push(new klass())
rehash: ->
for comparator in @comparators
for name in comparator.names
@[name] = comparator.compare
# Standart comparator set
class Gtl.StandartComparatorSet extends Gtl.ComparatorSet
set: [
Gtl.GreaterThanComparator
Gtl.GreaterThanOrEqualToComparator
Gtl.LessThanComparator
Gtl.LessThanOrEqualToComparator
Gtl.OnlyComparator
Gtl.ExceptComparator
Gtl.GrepComparator
Gtl.FuzzyComparator
]
constructor: ->
super
@add(klass) for klass in @set
@rehash()
# Main GTL object: set of rules, filter function
class Gtl.Filter
constructor: ->
@comparators = new Gtl.StandartComparatorSet()
filter: (array, rules, iterator) ->
result = clone(array)
unless iterator
iterator = []
if rules.or or rules.in
iterator.push(rule: 'or', iterator: rules.or || rules.in)
if rules.and
iterator.push(rule: 'and', iterator: rules.and)
if iterator.length == 0
iterator = (elm) -> elm
for name, rule of rules
if ['or', 'in', 'and'].indexOf(name) == -1
result = filter(result, @comparators[name], rule, iterator)
result
curry: (curriedRules, curriedIterator) ->
(array, userRules, userIterator = curriedIterator) =>
if userRules and userRules.constructor == Function
rules = curriedRules
iterator = userRules
else
rules = merge(curriedRules, userRules)
iterator = userIterator
@filter(array, rules, iterator)
# Define main object
gtl = new Gtl.Filter()
###
Internal: clone array
###
clone = (array) -> array.slice()
###
Internal: merge objects
###
merge = (a = {}, b = {}) ->
result = {}
copyPropsToResult = (obj) ->
for own key, value of obj
result[key] = value
copyPropsToResult(a)
copyPropsToResult(b)
result
###
Internal: get element by path
###
getByPath = (obj, path) ->
if path.constructor == String
getByPath(obj, path.split('.'))
else
if path.length == 1
obj[path]
else
getByPath(obj[path[0]], path.slice(1))
###
Internal: is satisfied to iterator rule
###
isSatisfiedToIteratorRule = (rule, results) ->
switch rule
when 'or'
results.indexOf(true) != -1
when 'and'
results.indexOf(false) == -1
###
Internal: filter array by rule and return copy
###
filter = (array, comparator, rule, iterator) ->
result = []
for elm in array
satisfied = false
if iterator.constructor == Function
satisfied = true if comparator(iterator(elm), rule)
else
satisfied = true
# Each iterator rule (or, and)
for iteratorRule in iterator
do ->
results = []
compare = (iterator) ->
results.push \
comparator(getByPath(elm, iterator), rule)
switch iteratorRule.iterator.constructor
when String
compare(iteratorRule.iterator)
when Array
compare(i) for i in iteratorRule.iterator
unless isSatisfiedToIteratorRule(iteratorRule.rule, results)
satisfied = false
result.push(elm) if satisfied
result
# Export gtl to global scope
if window?
window.gtl = gtl
else
module.exports = gtl