/
nowUtil.js
75 lines (65 loc) · 1.63 KB
/
nowUtil.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
var util = {
hasProperty: function (obj, prop) {
return Object.prototype.hasOwnProperty.call(Object(obj), prop);
},
noop: function () {
},
clone: function (obj) {
var output = {};
output.__proto__ = obj;
return output;
},
generateRandomString: function () {
return Math.random().toString().substr(2);
},
getAllChildFqns: function (parentObj, parentFqn) {
var fqns = [];
function getAllChildFqnsHelper(parentObj, parentFqn) {
for (var prop in parentObj) {
if (util.hasProperty(parentObj, prop)) {
fqns.push(parentFqn+'.'+prop);
if (parentObj[prop] && typeof parentObj[prop] === 'object') {
getAllChildFqnsHelper(parentObj[prop], parentFqn+'.'+prop);
}
}
}
}
getAllChildFqnsHelper(parentObj, parentFqn);
return fqns;
},
getVarFromFqn: function (fqn, scope) {
var path = fqn.split('.');
path.shift();
var currVar = scope;
var prop;
while (path.length > 0) {
prop = path.shift();
if (util.hasProperty(currVar, prop)) {
currVar = currVar[prop];
} else {
return false;
}
}
return currVar;
},
flatten: function (val, fqn) {
var vals = {};
var fqns = util.getAllChildFqns(val, '');
var m;
for (var i = 0, ii = fqns.length; i < ii; i++) {
m = util.getVarFromFqn(fqns[i], val);
if (!Array.isArray(m)) {
vals[fqn + fqns[i]] = m;
}
}
return vals;
},
getValOrFqn: function (val, fqn) {
if (typeof val === 'function') {
return {fqn: fqn};
} else {
return val;
}
}
};
exports.nowUtil = util;