rambdax
Version:
Extended version of Rambda - a lightweight, faster alternative to Ramda
23 lines (18 loc) • 532 B
JavaScript
export function path(pathInput, obj){
if (arguments.length === 1) return _obj => path(pathInput, _obj)
if (obj === null || obj === undefined){
return undefined
}
let willReturn = obj
let counter = 0
const pathArrValue =
typeof pathInput === 'string' ? pathInput.split('.') : pathInput
while (counter < pathArrValue.length){
if (willReturn === null || willReturn === undefined){
return undefined
}
willReturn = willReturn[ pathArrValue[ counter ] ]
counter++
}
return willReturn
}