-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.js
62 lines (50 loc) · 1.61 KB
/
util.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
import templist from 'templist';
const isObject = ob => (typeof ob === 'object' && ob !== null && !(Array.isArray(ob)));
const isString = ob => (typeof ob === 'string' && ob.length > 0);
const isNumber = ob => (typeof ob === 'number' && ob !== isNaN && ob > 0);
const notExists = ob => (typeof ob === 'undefined' || ob === null);
const exists = ob => !(notExists(ob));
const stringify = (input, pretty) => {
if (typeof input === 'string') return input;
if (typeof input === 'object') {
try {
return pretty
? JSON.stringify(input, undefined, isString(pretty) ? pretty : ' ')
: JSON.stringify(input);
} catch (err) {
return String(input);
}
}
return String(input);
};
const getString = function getString(input, ...args) {
return (typeof input === 'function') ? input(args) : stringify(input);
};
const resolveUrl = function resolveUrl(urlA, urlB) {
let ua = stringify(urlA);
let ub = stringify(urlB);
if (ua.endsWith('/')) ua = ua.slice(0, -1);
if (!(ub.startsWith('/')) && !(ub.startsWith('http'))) ub = `/${ub}`;
return ua + ub;
};
const parseObj = function parseObj(obj) {
if (typeof obj === 'string') {
try {
return JSON.parse(obj);
} catch (erm) {
return obj;
}
}
return obj;
};
const cloneObj = function cloneObj(obj) {
return parseObj(stringify(obj));
};
const assign = templist.utils.assign;
const objwalk = templist.utils.objwalk;
const isAlphaNum = templist.utils.isAlphaNum;
export {
isObject, isString, isNumber, notExists, exists,
stringify, getString, objwalk, assign, templist,
resolveUrl, parseObj, cloneObj, isAlphaNum,
};