forked from Turistforeningen/node-mongo-querystring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
329 lines (271 loc) · 7.67 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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
'use strict';
module.exports = function MongoQS(options) {
const opts = options || {};
this.ops = opts.ops || ['!', '^', '$', '~', '>', '<', '$in'];
this.alias = opts.alias || {};
this.blacklist = opts.blacklist || {};
this.whitelist = opts.whitelist || {};
this.custom = opts.custom || {};
// String Value Parsing
opts.string = opts.string || {};
this.string = opts.string || {};
this.string.toBoolean = (typeof opts.string.toBoolean === 'boolean') ? opts.string.toBoolean : true;
this.string.toNumber = (typeof opts.string.toNumber === 'boolean') ? opts.string.toNumber : true;
this.keyRegex = opts.keyRegex || /^[a-zæøå0-9-_.]+$/i;
this.valRegex = opts.valRegex || /[^a-zæøå0-9-_.* ]/i;
this.arrRegex = opts.arrRegex || /^[a-zæøå0-9-_.]+(\[])?$/i;
if (this.custom.bbox) {
this.custom.bbox = this.customBBOX(this.custom.bbox);
}
if (this.custom.near) {
this.custom.near = this.customNear(this.custom.near);
}
if (this.custom.after) {
this.custom.after = this.customAfter(this.custom.after);
}
if (this.custom.before) {
this.custom.before = this.customBefore(this.custom.before);
}
if (this.custom.between) {
this.custom.between = this.customBetween(this.custom.between);
}
return this;
};
module.exports.prototype.customBBOX = field => (query, bbox) => {
const bboxArr = bbox.split(',');
if (bboxArr.length === 4) {
// Optimize by unrolling the loop
bboxArr[0] = parseFloat(bboxArr[0], 10);
bboxArr[1] = parseFloat(bboxArr[1], 10);
bboxArr[2] = parseFloat(bboxArr[2], 10);
bboxArr[3] = parseFloat(bboxArr[3], 10);
if (!isNaN(bboxArr.reduce((a, b) => a + b))) {
query[field] = {
$geoWithin: {
$geometry: {
type: 'Polygon',
coordinates: [[
[bboxArr[0], bboxArr[1]],
[bboxArr[2], bboxArr[1]],
[bboxArr[2], bboxArr[3]],
[bboxArr[0], bboxArr[3]],
[bboxArr[0], bboxArr[1]],
]],
},
},
};
}
}
};
module.exports.prototype.customNear = field => (query, point) => {
const pointArr = point.split(',').map(p => parseFloat(p, 10));
if (pointArr.length >= 2) {
if (!isNaN(pointArr.reduce((a, b) => a + b))) {
const max = pointArr[2];
const min = pointArr[3];
query[field] = {
$near: {
$geometry: {
type: 'Point',
coordinates: pointArr.splice(0, 2),
},
},
};
if (!isNaN(max)) {
query[field].$near.$maxDistance = max;
if (!isNaN(min)) {
query[field].$near.$minDistance = min;
}
}
}
}
};
function parseDate(value) {
let date = value;
if (!isNaN(date)) {
if (`${date}`.length === 10) {
date = `${date}000`;
}
date = parseInt(date, 10);
}
date = new Date(date);
return date;
}
module.exports.prototype.customAfter = field => (query, value) => {
const date = parseDate(value);
if (date.toString() !== 'Invalid Date') {
query[field] = {
$gte: date.toISOString(),
};
}
};
module.exports.prototype.customBefore = field => (query, value) => {
const date = parseDate(value);
if (date.toString() !== 'Invalid Date') {
query[field] = {
$lt: date.toISOString(),
};
}
};
module.exports.prototype.customBetween = field => (query, value) => {
const dates = value.split('|');
const afterValue = dates[0];
const beforeValue = dates[1];
const after = parseDate(afterValue);
const before = parseDate(beforeValue);
if (after.toString() !== 'Invalid Date' && before.toString() !== 'Invalid Date') {
query[field] = {
$gte: after.toISOString(),
$lt: before.toISOString(),
};
}
};
module.exports.prototype.parseString = function parseString(string, array) {
let op = string[0] || '';
const eq = string[1] === '=';
let org = string.substr(eq ? 2 : 1) || '';
const val = this.parseStringVal(org);
const ret = { op, org, value: val };
switch (op) {
case '!':
if (array) {
ret.field = '$nin';
} else if (org === '') {
ret.field = '$exists';
ret.value = false;
} else {
ret.field = '$ne';
}
break;
case '>':
ret.field = eq ? '$gte' : '$gt';
break;
case '<':
ret.field = eq ? '$lte' : '$lt';
break;
case '^':
case '$':
case '~':
ret.field = '$regex';
ret.options = 'i';
ret.value = org.replace(this.valReqex, '');
switch (op) {
case '^':
ret.value = `^${val}`;
break;
case '$':
ret.value = `${val}$`;
break;
default:
break;
}
break;
default:
ret.org = org = op + org;
ret.op = op = '';
ret.value = this.parseStringVal(org);
if (array) {
ret.field = '$in';
} else if (org === '') {
ret.field = '$exists';
ret.value = true;
} else {
ret.field = '$eq';
}
}
ret.parsed = {};
ret.parsed[ret.field] = ret.value;
if (ret.options) {
ret.parsed.$options = ret.options;
}
return ret;
};
module.exports.prototype.parseStringVal = function parseStringVal(string) {
if (this.string.toBoolean && string.toLowerCase() === 'true') {
return true;
} else if (this.string.toBoolean && string.toLowerCase() === 'false') {
return false;
} else if (this.string.toNumber && !isNaN(parseInt(string, 10)) &&
((+string - +string) + 1) >= 0) {
return parseFloat(string, 10);
}
return string;
};
module.exports.prototype.parse = function parse(query) {
const res = {};
Object.keys(query).forEach((k) => {
let key = k;
const val = query[key];
// normalize array keys
if (val instanceof Array) {
key = key.replace(/\[]$/, '');
}
// whitelist
if (Object.keys(this.whitelist).length && !this.whitelist[key]) {
return;
}
// blacklist
if (this.blacklist[key]) {
return;
}
// alias
if (this.alias[key]) {
key = this.alias[key];
}
// string key
if (typeof val === 'string' && !this.keyRegex.test(key)) {
return;
// array key
} else if (val instanceof Array && !this.arrRegex.test(key)) {
return;
}
// custom functions
if (typeof this.custom[key] === 'function') {
this.custom[key].apply(null, [res, val]);
return;
}
// array key
if (val instanceof Array) {
if (this.ops.indexOf('$in') >= 0 && val.length > 0) {
res[key] = {};
for (let i = 0; i < val.length; i += 1) {
if (this.ops.indexOf(val[i][0]) >= 0) {
const parsed = this.parseString(val[i], true);
switch (parsed.field) {
case '$in':
case '$nin':
res[key][parsed.field] = res[key][parsed.field] || [];
res[key][parsed.field].push(parsed.value);
break;
case '$regex':
res[key].$regex = parsed.value;
res[key].$options = parsed.options;
break;
default:
res[key][parsed.field] = parsed.value;
}
} else {
res[key].$in = res[key].$in || [];
res[key].$in.push(this.parseStringVal(val[i]));
}
}
}
return;
}
// value must be a string
if (typeof val !== 'string') {
return;
}
// field exists query
if (!val) {
res[key] = { $exists: true };
// query operators
} else if (this.ops.indexOf(val[0]) >= 0) {
res[key] = this.parseString(val).parsed;
// equal operator (no operator)
} else {
res[key] = this.parseStringVal(val);
}
});
return res;
};