|
| 1 | +/*jshint loopfunc: true */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +module.exports = function MongoQS(opts) { |
| 5 | + opts = opts || {}; |
| 6 | + |
| 7 | + this.ops = opts.ops || ['!', '^', '$', '~', '>', '<']; |
| 8 | + this.alias = opts.alias || {}; |
| 9 | + this.blacklist = opts.blacklist || {}; |
| 10 | + this.whitelist = opts.whitelist || {}; |
| 11 | + this.custom = opts.custom || {}; |
| 12 | + |
| 13 | + for (var param in this.custom) { |
| 14 | + switch (param) { |
| 15 | + case 'bbox': |
| 16 | + this.custom.bbox = this.customBBOX(this.custom[param]); |
| 17 | + break; |
| 18 | + case 'near': |
| 19 | + this.custom.near = this.customNear(this.custom[param]); |
| 20 | + break; |
| 21 | + case 'after': |
| 22 | + this.custom.after = this.customAfter(this.custom[param]); |
| 23 | + } |
| 24 | + } |
| 25 | + return this; |
| 26 | +}; |
| 27 | + |
| 28 | +module.exports.prototype.customBBOX = function(field) { |
| 29 | + return function(query, bbox) { |
| 30 | + bbox = bbox.split(','); |
| 31 | + |
| 32 | + if (bbox.length === 4) { |
| 33 | + // Optimize by unrolling the loop |
| 34 | + bbox[0] = parseFloat(bbox[0], 10); |
| 35 | + bbox[1] = parseFloat(bbox[1], 10); |
| 36 | + bbox[2] = parseFloat(bbox[2], 10); |
| 37 | + bbox[3] = parseFloat(bbox[3], 10); |
| 38 | + |
| 39 | + if (!isNaN(bbox.reduce(function(a,b){return a+b;}))) { |
| 40 | + query[field] = { |
| 41 | + $geoWithin: { |
| 42 | + $geometry: { |
| 43 | + type: 'Polygon', |
| 44 | + coordinates: [[ |
| 45 | + [bbox[0], bbox[1]], |
| 46 | + [bbox[2], bbox[1]], |
| 47 | + [bbox[2], bbox[3]], |
| 48 | + [bbox[0], bbox[3]], |
| 49 | + [bbox[0], bbox[1]], |
| 50 | + ]], |
| 51 | + }, |
| 52 | + }, |
| 53 | + }; |
| 54 | + } |
| 55 | + } |
| 56 | + }; |
| 57 | +}; |
| 58 | + |
| 59 | +module.exports.prototype.customNear = function(field) { |
| 60 | + return function(query, point) { |
| 61 | + point = point.split(','); |
| 62 | + |
| 63 | + if (point.length === 2) { |
| 64 | + point[0] = parseFloat(point[0], 10); |
| 65 | + point[1] = parseFloat(point[1], 10); |
| 66 | + |
| 67 | + if (!isNaN(point.reduce(function(a,b){return a+b;}))) { |
| 68 | + query[field] = { |
| 69 | + $near: { |
| 70 | + $geometry: { |
| 71 | + type: 'Point', |
| 72 | + coordinates: point, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }; |
| 76 | + } |
| 77 | + } |
| 78 | + }; |
| 79 | +}; |
| 80 | + |
| 81 | +module.exports.prototype.customAfter = function(field) { |
| 82 | + return function(query, date) { |
| 83 | + if (!isNaN(date)) { |
| 84 | + if ((date + '').length === 10) { |
| 85 | + date = date + '000'; |
| 86 | + } |
| 87 | + date = parseInt(date, 10); |
| 88 | + } |
| 89 | + |
| 90 | + date = new Date(date); |
| 91 | + |
| 92 | + if (date.toString() !== 'Invalid Date') { |
| 93 | + query[field] = { |
| 94 | + $gte: date.toISOString() |
| 95 | + }; |
| 96 | + } |
| 97 | + }; |
| 98 | +}; |
| 99 | + |
| 100 | +module.exports.prototype.parse = function(query) { |
| 101 | + var op, val; |
| 102 | + var res = {}; |
| 103 | + |
| 104 | + for (var key in query) { |
| 105 | + val = query[key]; |
| 106 | + |
| 107 | + if (Object.keys(this.whitelist).length && !this.whitelist[key]) { |
| 108 | + continue; |
| 109 | + } |
| 110 | + |
| 111 | + if (this.blacklist[key]) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + if (typeof val !== 'string') { |
| 116 | + continue; |
| 117 | + } |
| 118 | + |
| 119 | + if (!/^[a-zæøå0-9-_.]+$/i.test(key)) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + if (this.alias[key]) { |
| 124 | + key = this.alias[key]; |
| 125 | + } |
| 126 | + |
| 127 | + if (typeof this.custom[key] === 'function') { |
| 128 | + this.custom[key](res, val); |
| 129 | + |
| 130 | + } else if (!val) { |
| 131 | + res[key] = { $exists: true }; |
| 132 | + |
| 133 | + } else if (this.ops.indexOf(val[0]) >= 0) { |
| 134 | + op = val.charAt(0); |
| 135 | + val = val.substr(1); |
| 136 | + |
| 137 | + res[key] = (function() { |
| 138 | + switch (op) { |
| 139 | + case '!': |
| 140 | + if (val) { |
| 141 | + return { $ne: isNaN(val) ? val : parseFloat(val, 10) }; |
| 142 | + } else { |
| 143 | + return { $exists: false }; |
| 144 | + } |
| 145 | + break; |
| 146 | + case '>': |
| 147 | + return { $gt: parseFloat(val, 10) }; |
| 148 | + case '<': |
| 149 | + return { $lt: parseFloat(val, 10) }; |
| 150 | + default: |
| 151 | + val = val.replace(/[^a-zæøå0-9-_.* ]/i, ''); |
| 152 | + switch (op) { |
| 153 | + case '^': |
| 154 | + return { $regex: '^' + val, $options: 'i' }; |
| 155 | + case '$': |
| 156 | + return { $regex: val + '$', $options: 'i' }; |
| 157 | + default: |
| 158 | + return { $regex: val, $options: 'i' }; |
| 159 | + } |
| 160 | + } |
| 161 | + })(); |
| 162 | + } else { |
| 163 | + res[key] = isNaN(val) ? val : parseFloat(val, 10); |
| 164 | + } |
| 165 | + } |
| 166 | + return res; |
| 167 | +}; |
0 commit comments