-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
60 lines (53 loc) · 1.18 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
55
56
57
58
59
60
var deepEqual = require('deep-equal')
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
}
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
}
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
}
}
function findByPath (indexes, path) {
return indexes.find((index) => {
return deepEqual(index.value, path)
})
}
function isFunction (variable) {
return typeof variable === 'function'
}
function validator (msg) {
if (typeof msg !== 'object') return null
if (typeof msg.value !== 'object') return null
if (typeof msg.value.timestamp !== 'number') return null
if (typeof msg.value.type !== 'string') return null
return msg
}
module.exports = {
has,
get,
set,
findByPath,
isFunction,
validator
}