-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.js
42 lines (35 loc) · 861 Bytes
/
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
var deepEqual = require('deep-equal')
exports.has = function has (key, obj) {
if('string' === typeof key)
return Object.hasOwnProperty.call(obj, key)
for(var i in key) {
if(Object.hasOwnProperty.call(obj, key[i]))
obj = obj[key[i]]
else
return false
}
return true
}
exports.get = function get (key, obj) {
if('string' === typeof key) return obj[key]
for(var i in key) {
obj = obj[key[i]]
if(!obj) return obj
}
return obj
}
exports.set = function set(key, value, obj) {
if('string' === typeof key)
obj[key] = value
else {
for(var i = 0 ; i < key.length - 1; i++) {
obj = (obj[key[i]] = obj[key[i]] || {})
}
obj[key[key.length -1]] = value
}
}
exports.findByPath = function (indexes, path) {
return indexes.find(function (index) {
return deepEqual(index.value, path)
})
}