@wasmuth/path
Version:
Practical, functional utilities that fallback on native implementations as much as possible
18 lines (15 loc) • 483 B
JavaScript
import check from 'check-arg-types'
import partial from '@wasmuth/partial'
export default function path (paths, obj) {
if (arguments.length < 2) return partial(path, arguments)
check(arguments, [['array', 'string'], ['array', 'object', 'window', 'global']])
if (check.prototype.toType(paths) === 'string') paths = paths.split('.')
let val = obj
for (let x = 0; x < paths.length; x++) {
if (val == null) {
return
}
val = val[paths[x]]
}
return val
}