-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
92 lines (81 loc) · 2.01 KB
/
utils.js
File metadata and controls
92 lines (81 loc) · 2.01 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
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
const emptyObject = {};
function getArrayFor(x, method) {
if (x) {
if (Array.isArray(x) || typeof x[method] == 'function') return x;
// detecting array like
if ('item' in x && 'length' in x) return Array.from(x);
}
}
function entries(x) {
let a = getArrayFor(x, 'entries');
return a ? a.entries() : Object.entries(x || emptyObject);
}
function keys(x) {
let a = getArrayFor(x, 'keys');
return a ? a.keys() : Object.keys(x || emptyObject);
}
function values(x) {
let a = getArrayFor(x, 'values');
return a ? a.values() : Object.values(x || emptyObject);
}
let privateSymbol = Symbol('private');
function typeStr(val) {
return Object.prototype.toString.call(val).slice(8, -1).toLowerCase();
}
function extend(/*dst,src1, src2, src3*/) {
let p;
let t;
let src;
let dst;
let len = arguments.length;
let i;
// this allows easy clonig, you may use it foo = extend(bar) to do that
if (len == 1) {
t = typeStr(arguments[0]);
if (t == 'array') {
dst = [];
}
if (t == 'object') {
dst = {};
}
i = 0;
} else {
dst = arguments[0];
i = 1;
}
for (; i < len; i++) {
src = arguments[i];
// just pass non extendables through
t = typeStr(src);
if (!(t == 'array' || t == 'object')) {
dst = src;
} else {
for (p in src) {
t = typeStr(src[p]);
dst[p] = t == 'array' || t == 'object' ? extend(dst[p] ? dst[p] : t == 'array' ? [] : {}, src[p]) : src[p];
}
}
}
return dst;
}
function empty(x) {
var hasOwnProperty = Object.prototype.hasOwnProperty;
// undefined, '', null, 0, empty array
if (!x || x.length === 0) {
return true;
}
var type = typeStr(x);
if (type == 'array') return;
// empty {}
if (type == 'object') {
for (var n in x) {
if (hasOwnProperty.call(x, n)) {
return false;
}
}
return true;
}
// convert to string and match single non-space character
return !(x + '').match(/\S/);
}
export { privateSymbol, entries, keys, values, extend, typeStr, empty };