-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathutils.js
More file actions
54 lines (49 loc) · 1.42 KB
/
utils.js
File metadata and controls
54 lines (49 loc) · 1.42 KB
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
/**
* Created by iddo on 7/21/17.
*/
/**
* Created by iddo on 6/28/17.
*/
/**
* Remove special selectors (LIMIT, ORDERBY etc...) from query arguments
* @param queryArgs
* @returns {*}
*/
function removeSpecialArgs(queryArgs, _specialKeys) {
let queryArgs = Object.assign({}, _queryArgs);
specialKeys.forEach((key) => {
delete queryArgs[key];
});
return queryArgs;
}
/**
* Returns true if val is a number / string with only a number
* @param val
* @returns {boolean}
*/
function isNumberParseable(val) {
return (!isNaN(parseFloat(val)) && `${parseFloat(val)}` === val) || (typeof val == 'number');
}
/**
* Return a string. If value is a string, it'll be quoted. If it's a number or can be parsed as one - it won't.
* @param value
* @returns {string}
*/
function quoteAsNeeded(value) {
return (typeof value == 'number') ? `${value}` :
(isNumberParseable(value)) ? `${parseFloat(value)}` :
(typeof value == 'string') ? `'${value.replace(/'/g, "''")}'`
: `'${value}'`;
}
/**
* Return an objects values as an array (pollyfill for ES2017's Object.values)
* @param obj
* @returns {Array}
*/
function objValues(obj) {
return Object.keys(obj).map(key => obj[key]);
}
module.exports.removeSpecialArgs = removeSpecialArgs;
module.exports.isNumberParseable = isNumberParseable;
module.exports.quoteAsNeeded = quoteAsNeeded;
module.exports.objValues = objValues;