-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
33 lines (29 loc) · 857 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
var deepEqual = require('deep-equal')
exports.has = function has (key, obj) {
if (typeof key === 'string') { 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 (typeof key === 'string') 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 (typeof key === 'string') { 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)
})
}