-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (73 loc) · 2.07 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
module.exports = ExpressQSParser;
var basicConfig = {
storage: 'parsedQuery'
};
/** ExpressQSParser Middleware
* @param {literal object} config middleware configuration
*
* config: {
* params: {
* filters: /([\w-_]+)(\>|\<|\=|\!=)([\w_-]+)/g
* },
* storage: 'query'
* }
*/
function ExpressQSParser(config) {
var storageName = '', parametersNames, params;
params = config.params || {};
storageName = config.storage || basicConfig.storage;
parametersNames = Object.keys(params);
initParams();
return middleware;
function initParams() {
for(var i = parametersNames.length; i>=0; i--)
if(!(params[parametersNames[i]] instanceof RegExp)) {
delete params[parametersNames[i]];
parametersNames.splice(i, 1);
}
else {
params[parametersNames[i]] = {
exp:params[parametersNames[i]],
isGlobal: params[parametersNames[i]].global
};
if(params[parametersNames[i]].isGlobal)
params[parametersNames[i]].nGExp = new RegExp(params[parametersNames[i]].exp.toString().slice(1,-2));
}
}
function applyExpression(field, value) {
var result = value, temp;
if(!!~parametersNames.indexOf(field)) {
value = value || '';
result = value.match(params[field].exp);
if(params[field].isGlobal && result instanceof Array) {
for(var i = 0; i<result.length; i++) {
result[i] = result[i].match(params[field].nGExp);
result[i] = sanitizeRegExpMatch(result[i]);
}
}
else
result = sanitizeRegExpMatch(result);
}
return result;
}
function sanitizeRegExpMatch(match) {
if(match instanceof Array) {
var temp = [];
for(var j = 1; j<match.length; j++)
temp.push(match[j]);
match = temp;
}
return match;
}
function middleware(request, response, next) {
var queryParams = request.query,
paramNames = Object.keys(queryParams);
var storage = request[storageName];
if(!storage || !(storage instanceof Object))
storage = request[storageName] = {};
for(var i =0; i<paramNames.length; i++) {
storage[paramNames[i]] = applyExpression(paramNames[i], queryParams[paramNames[i]]);
}
next();
}
}