-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
214 lines (195 loc) · 6.31 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
var qs = require('qs');
var helpers = require('./lib/helpers');
function processQuerystringItem(fieldName, value) {
var result;
if(require("util").isArray(value)) {
var processedValues = [];
for(var i = 0; i < value.length; i++) {
var subValue = helpers.replace_operator_values(value[i], fieldName);
if(subValue.value) {
result = {};
if(subValue.rename) {
result[subValue.rename] = subValue.value;
} else {
result[fieldName] = subValue.value;
}
processedValues.push(result);
}
}
return processedValues;
} else {
value = helpers.replace_operator_values(value, fieldName);
if(value.value !== null) {
result = {};
if(value.rename) {
result[value.rename] = value.value;
} else {
result[fieldName] = value.value;
}
return [result];
}
}
return [];
}
function processSortQuerystringItem(value) {
var extractSort = function (val) {
var spl = val.split("__");
if(spl.length !== 2) {
return null;
}
var key = spl[0];
var order = spl[1];
if(!isNaN(order)) {
order = order * 1;
if(order < 0) {
order = -1;
} else if(order > 0) {
order = 1;
} else {
return null;
}
} else {
order = order.toLowerCase();
if(order === "asc") {
order = 1;
} else if(order == "desc") {
order = -1
} else {
return null;
}
}
return [key, order];
}
var result;
if(require("util").isArray(value)) {
var processedValues = [];
for(var i = 0; i < value.length; i++) {
result = extractSort(value[i]);
if(result) {
processedValues.push(result);
}
}
if(processedValues.length) {
return processedValues;
}
} else {
result = extractSort(value);
if(result) {
return [result];
}
}
return null;
}
function flatten(request) {
var flattened = {};
var traverse = function (obj, path) {
var currentPath = '';
if(obj) {
for(var key in obj) {
if(obj.hasOwnProperty(key)) {
currentPath = path ? path + "." + key : key;
if(typeof obj[key] === 'object' && !require("util").isArray(obj[key])){
traverse(obj[key], currentPath);
}else{
flattened[currentPath] = obj[key];
}
}
}
}
};
traverse(request, "")
return flattened;
}
module.exports = {
extendOperators: function (operators) {
helpers.extendOperators(operators);
},
filter: function (request, options) {
var filterQuery = {};
options = options || {};
var prefix = options.prefix;
if(options.operators) {
helpers.extendOperators(options.operators);
}
var querystring, qsParts
if(typeof request === 'string') {
querystring = request;
qsParts = qs.parse(querystring);
} else if(typeof request === 'object' && request) {
if(request.url && request.url.query) {
querystring = request.url.query;
qsParts = qs.parse(querystring);
} else {
qsParts = flatten(request);
}
} else {
throw new Error('The first parameter should be an object, a request object, or a querystring');
}
var conditions = [];
var condition;
for(var key in qsParts) {
if(qsParts.hasOwnProperty(key)) {
// If there's a prefix defined, only pass through keys that start with the prefix
if(options.prefix) {
if(!key.indexOf(options.prefix)) {
conditions = conditions.concat(processQuerystringItem(key.substr(prefix.length), qsParts[key]));
}
} else {
conditions = conditions.concat(processQuerystringItem(key, qsParts[key]));
}
}
}
// Now build the final object
switch(conditions.length) {
case 1:
if(options.forceand){
filterQuery.$and = conditions;
}else{
filterQuery = conditions[0];
}
break;
case 0:
break;
default:
filterQuery.$and = conditions;
}
return filterQuery;
}, sort: function (request, options) {
var sortQuery = [];
options = options || {};
var prefix = options.prefix;
var querystring;
if(typeof request === 'string') {
querystring = request;
} else if(typeof request === 'object' && request.url && request.url.query) {
querystring = request.url.query;
} else {
throw new Error('The first parameter should be a request object, or a querystring');
}
var qsParts = qs.parse(querystring);
var sortItems = [];
var sortItem;
for(var key in qsParts) {
if(qsParts.hasOwnProperty(key)) {
// If there's a prefix defined, only pass through keys that start with the prefix
if(options.prefix) {
if(!key.indexOf(options.prefix)) {
sortItem = processSortQuerystringItem(qsParts[key]);
if(sortItem) {
sortItems = sortItems.concat(sortItem);
}
}
} else {
sortItem = processSortQuerystringItem(qsParts[key]);
if(sortItem) {
sortItems = sortItems.concat(sortItem);
}
}
}
}
if(sortItems.length) {
return sortItems;
}
return null;
}
};