-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
35 lines (29 loc) · 817 Bytes
/
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
function trimValues(input) {
if (input && input.constructor === Object) {
for (let prop in input) {
if (input[prop] && input[prop].constructor !== String) {
return trimValues(input[prop]);
}
if (input[prop] && input[prop].constructor === String && input[prop].length) {
input[prop] = input[prop].trim();
}
}
} else if (input && input.constructor === Array && input.length) {
input.forEach(item => trimValues(item));
} else if (input && input.constructor === String && input.length) {
input = input.trim();
}
}
function expressTrimmer(req, res, next) {
if (req.body) {
trimValues(req.body);
}
if (req.params) {
trimValues(req.params);
}
if (req.query) {
trimValues(req.query);
}
next();
}
module.exports = expressTrimmer;