-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (115 loc) · 3.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
const comparatorObject = {
gt: '>',
gte: '>=',
lt: '<',
lte: '<=',
df: '!=',
eq: '=',
lk: 'LIKE'
}
const queryParserFunctions = {
/**
* Parse all query params, clean them and return the result
* @param {Object} queryParams All URL params
*/
parse : function (queryParams) {
return this.splitInArray(queryParams)
},
/**
* Group all params in an array of object
* @param {Object} queryParams All URL params
*/
splitInArray : function (queryParams) {
let paramsArray = []
for (let i = 0; i < Object.keys(queryParams).length; i++) {
let key = Object.keys(queryParams)[i].toString()
let values = (Array.isArray(queryParams[key]) === true) ? queryParams[key] : [queryParams[key]]
for (let j = 0; j < values.length; j++) {
let value = values[j].toString()
let searched = {
key: key,
value: null,
comparator: '='
}
if (!this.containsNumberOrLetter(key)) {
continue
}
if (value.includes(':')) {
let splittedValue = value.split(':')
if (comparatorObject[splittedValue[0]] !== undefined) {
searched.comparator = comparatorObject[splittedValue[0]]
searched.value = (comparatorObject[splittedValue[0]] !== 'LIKE') ? this.parseToIntIfNumber(splittedValue[1]) : `%${splittedValue[1]}%`
}
} else if (value.includes(',')) {
let splittedValue = value.split(',')
searched.value = []
for (let k = 0; k < splittedValue.length; k++) {
searched.value.push(this.parseToIntIfNumber(splittedValue[k]))
}
} else {
searched.value = this.parseToIntIfNumber(value)
}
paramsArray.push(searched)
}
}
return paramsArray
},
/**
* Check if number is int or float and return the parsed value, else just return the value
* @param {String} value String to parse
*/
parseToIntIfNumber : function (value) {
if (this.containsOnlyNumber(value)) {
const count = (value.match(/\./g) || []).length;
if (count >= 2) {
return value
}
if (value.includes('.')) {
return parseFloat(value)
}
return parseInt(value)
}
return value
},
/**
* Check a string is only compose of number or letter
* @param {String} value String to check
*/
containsNumberOrLetter : function (value) {
if (value == null) {
return false
}
if (value.length === 0) {
return false
}
for (let i = 0; i < value.length; i++) {
let code = value[i].charCodeAt(0)
// Allow [-_a-zA-Z0-9] in key
if (code !== 45 && code !== 95 && (code < 48 || (code > 57 && code < 65) || (code > 90 && code < 97) || code > 122)) {
return false
}
}
return true
},
/**
* Check a string contains only number
* @param {String} value String to check
*/
containsOnlyNumber : function (value) {
if (value == null) {
return false
}
if (value.length === 0) {
return false
}
for (let i = 0; i < value.length; i++) {
let code = value[i].charCodeAt(0)
// Allow point for floating number
if (code !== 46 && (code < 48 || code > 57)) {
return false
}
}
return true
}
}
module.exports = queryParserFunctions