-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
296 lines (262 loc) · 8.13 KB
/
index.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/**
* znFilterMatcher
*
* Ported code from the Zengine frontend
*
* Determines whether the given record matches the given filter
*
* Limitations vs. API filtering:
* - Does not support subfiltering across forms
* - Does not support dynamic filter conditions ('logged-in-user')
*
*
* Copyright (c) WizeHive - http://www.wizehive.com
*
*/
var znFilterMatcher = (function() {
var BigNumber = require('bignumber.js');
var ruleFunctionMap = {
'': 'ruleEquals',
'not': 'ruleDoesNotEqual',
'min': 'ruleMinimum',
'max': 'ruleMaximum',
'contains': 'ruleContains',
'not-contains': 'ruleDoesNotContain',
'starts-with': 'ruleStartsWith',
'ends-with': 'ruleEndsWith'
};
/**
* Parse a numeric value from a record field input.
* Strips '$' and ',' characters. If the value is not a valid number
* after stripping characters, 0 is returned
*
* @param input
* @returns {Number}
* @author Paul W. Smith <paul@wizehive.com>
* @since 0.5.76
*/
function parseNumber(input) {
var formatted = String(input || '')
.replace('$', '')
.replace(',', '');
return formatted;
}
// Matchers for each rule "prefix" type
var matchers = {
ruleEquals: function(recordValue, ruleValue) {
if (Array.isArray(recordValue)) {
return false;
}
return String(ruleValue).toLowerCase() == String(recordValue).toLowerCase();
},
ruleDoesNotEqual: function(recordValue, ruleValue) {
if (Array.isArray(recordValue)) {
return false;
}
return String(ruleValue).toLowerCase() != String(recordValue).toLowerCase();
},
ruleMinimum: function(recordValue, ruleValue) {
if (Array.isArray(recordValue)) {
return false;
}
if (recordValue === '') {
return false;
}
// Do numeric comparison if filter rule value is numeric. For consistency with API,
// record values are somewhat forgiving, while filter rules must be a valid number
if (ruleValue && !isNaN(Number(ruleValue))) {
recordValue = parseNumber(recordValue);
try {
ruleValue = new BigNumber(ruleValue);
recordValue = new BigNumber(recordValue);
} catch (err) {
return false;
}
return recordValue.greaterThanOrEqualTo(ruleValue);
}
// Loose comparison; don't use 'localeCompare' because we only want to compare
// numbers or well-formatted dates, which work with normal string comparison
return (recordValue >= ruleValue);
},
ruleMaximum: function(recordValue, ruleValue) {
if (Array.isArray(recordValue)) {
return false;
}
if (recordValue === '') {
return false;
}
// Do numeric comparison if filter rule value is numeric. For consistency with API,
// record values are somewhat forgiving, while filter rules must be a valid number
if (ruleValue && !isNaN(Number(ruleValue))) {
recordValue = parseNumber(recordValue);
try {
ruleValue = new BigNumber(ruleValue);
recordValue = new BigNumber(recordValue);
} catch (err) {
return false;
}
return recordValue.lessThanOrEqualTo(ruleValue);
}
// Loose comparison; don't use 'localeCompare' because we only want to compare
// numbers or well-formatted dates, which work with normal string comparison
return (recordValue <= ruleValue);
},
ruleContains: function(recordValue, ruleValue) {
if (Array.isArray(recordValue)) {
// Normalize Value as Array
if (!Array.isArray(ruleValue)) {
ruleValue = [ruleValue];
}
// Loop Array of Values
for (var index = 0; index < ruleValue.length; index++) {
// Value Not Found in Record
if (recordValue.indexOf(ruleValue[index]) === -1) {
return false;
}
}
return true;
}
else {
return String(recordValue).indexOf(String(ruleValue)) !== -1;
}
},
ruleDoesNotContain: function(recordValue, ruleValue) {
return !this.ruleContains(recordValue, ruleValue);
},
ruleStartsWith: function(recordValue, ruleValue) {
if (Array.isArray(recordValue)) {
return false;
}
return String(recordValue).startsWith(String(ruleValue));
},
ruleEndsWith: function(recordValue, ruleValue) {
if (Array.isArray(recordValue)) {
return false;
}
return String(recordValue).endsWith(String(ruleValue));
}
};
/**
* Determine whether the given record matches the given filter
*
* @param {Object} filter
* @param {Object} record
* @returns {Boolean}
* @author Paul W. Smith <paul@wizehive.com>
* @since 0.5.75
*/
function recordMatchesFilter(record, filter, options) {
options = typeof options !== 'undefined' ? options : {};
var currentOperator = Object.keys(filter)[0];
if (filter[currentOperator].length === 0) {
// Empty filter / no rules - considered a "match all"
return true;
}
for (var i in filter[currentOperator]) {
var match = recordMatchesRule(record, filter[currentOperator][i], options);
if (currentOperator == 'or' && match) {
return true;
}
if (currentOperator == 'and' && !match) {
return false;
}
}
// "and" - no misses by this point, return true
// "or" - no matches by this point, return false
return (currentOperator == 'and');
}
/**
* Determine whether the given record matches the given filter rule
*
* @param {Object} filter rule
* @param {Object} record
* @returns {Boolean}
* @author Paul W. Smith <paul@wizehive.com>
* @since 0.5.75
*/
function recordMatchesRule(record, rule, options) {
options = typeof options !== 'undefined' ? options : {};
var operators = ["and", "or"];
if (operators.indexOf(Object.keys(rule)[0]) !== -1) {
// Rule contains "and"/"or" key - nested filter
return recordMatchesFilter(record, rule);
}
if (rule.filter !== undefined) {
if (options.subfiltering) {
var subRecord = record[rule.attribute];
if (!recordMatchesFilter(subRecord, rule.filter, options)) {
return false;
}
return true;
} else {
throw new Error("Subfilter matching is not supported");
}
}
if (typeof rule.value === 'string' && rule.value.split('|').indexOf('logged-in-user') !== -1) {
throw new Error ("Dynamic filter conditions are not supported");
}
// From here, we know we have a normal rule with "prefix", "attribute", and "value" properties.
var recordValue = _getRecordValue(record, rule);
var ruleValues = _getRuleValues(rule);
// Run actual match logic based on rule prefix
var matchFunction = ruleFunctionMap[rule.prefix];
for (var i in ruleValues) {
if (matchers[matchFunction](recordValue, ruleValues[i])) {
return true;
}
}
// All ruleValues failed to match
return false;
}
/**
* Helper - parse rule values from filter rule
* If rule contains piped values, splits them into an array; otherwise
* yields an array-wrapped version of the single rule value for consistency
*
* @param {Object} filter rule
* @returns {Array} set of parsed rule values
* @author Paul W. Smith <paul@wizehive.com>
* @since 0.5.75
*/
function _getRuleValues(rule) {
if (typeof rule.value == 'string' && rule.value.indexOf('|') !== -1) {
return rule.value.split('|');
}
if (rule.value === 'null' || rule.value === null) {
return [''];
}
return [rule.value];
}
/**
* Helper - get the needed record value for comparison against this rule
*
* @param {Object} record
* @param {Object} filter rule
* @returns {String|Boolean}
* @author Paul W. Smith <paul@wizehive.com>
* @since 0.5.75
*/
function _getRecordValue(record, rule) {
var attributePieces = rule.attribute.split(".");
// Parse current record value of this rule's attribute, including dotted names (e.g. "folder.id")
var recordValue = record;
attributePieces.forEach(function(attributePiece) {
recordValue = recordValue && recordValue[attributePiece];
});
// Parse subobject properties to use for check - e.g. field123.value for upload, field456.id for linked/member
if (recordValue instanceof Object) {
if (recordValue.value !== undefined) {
recordValue = recordValue.value;
} else if (recordValue.id !== undefined) {
recordValue = recordValue.id;
}
} else if (recordValue === null || recordValue === undefined) {
return '';
}
return recordValue;
}
return {
recordMatchesFilter: recordMatchesFilter
};
}());
module.exports = znFilterMatcher;