/
util.js
54 lines (47 loc) · 1.2 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
"use strict";
var keys = Object.keys;
exports.keys = keys;
// Returns array of values for the given object.
var values = function(object) {
return keys(object).map(function(key) {
return object[key]
});
};
exports.values = values;
// Returns [key, value] pairs for the given object.
var pairs = function(object) {
return keys(object).map(function(key) {
return [key, object[key]]
});
};
exports.pairs = pairs;
// Queries an object for the field nested with in it.
var query = function(object, path) {
return path.reduce(function(object, entry) {
return object && object[entry]
}, object);
};
exports.query = query;
var isObject = function(x) {
return x && typeof(x) === "object"
}
var findPath = function(object, key) {
var path = void(0);
if (object && typeof(object) === "object") {
var names = keys(object);
if (names.indexOf(key) >= 0) {
path = [];
} else {
var index = 0;
var count = names.length;
while (index < count && !path){
var head = names[index];
var tail = findPath(object[head], key);
path = tail ? [head].concat(tail) : tail;
index = index + 1
}
}
}
return path;
};
exports.findPath = findPath;