-
Notifications
You must be signed in to change notification settings - Fork 0
/
seek.js
26 lines (22 loc) · 817 Bytes
/
seek.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
module.exports = configureSeek
var escapeRegexp = require("./escape");
var circular = /\[Circular \((.+)\)\]/
function configureSeek(separator, separatorArrayLeft, separatorArrayRight) {
separator = separator || '.'
separatorArrayLeft = separatorArrayLeft || '['
separatorArrayRight = separatorArrayRight || ']'
var nestedRe = new RegExp("(" + escapeRegexp(separator) + "|" + escapeRegexp(separatorArrayLeft) + ")")
var scrub = new RegExp(escapeRegexp(separatorArrayRight), "g")
return function seek(obj, path) {
path = path.replace(scrub, "")
var pathBits = path.split(nestedRe)
var len = pathBits.length
var layer = obj
for (var i = 0; i < len; i += 2) {
if (layer == null) return undefined
var key = pathBits[i]
layer = layer[key]
}
return layer
}
}