crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
50 lines (38 loc) • 1.15 kB
JavaScript
/** @license ISC License (c) copyright 2017 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */
var curry = require('../core/curry')
var isArray = require('../core/isArray')
var isDefined = require('../core/isDefined')
var isEmpty = require('../core/isEmpty')
var isInteger = require('../core/isInteger')
var isNil = require('../core/isNil')
var isString = require('../core/isString')
// hasPropPath : [ String | Integer ] -> a -> Boolean
function hasPropPath(keys, target) {
if(!isArray(keys)) {
throw new TypeError(
'hasPropPath: Array of Non-empty Strings or Integers required for first argument'
)
}
if(isNil(target)) {
return false
}
var value = target
for(var i = 0; i < keys.length; i++) {
var key = keys[i]
if(!(isString(key) && !isEmpty(key) || isInteger(key))) {
throw new TypeError(
'hasPropPath: Array of Non-empty Strings or Integers required for first argument'
)
}
if(isNil(value)) {
return false
}
value = value[key]
if(!isDefined(value)) {
return false
}
}
return true
}
module.exports = curry(hasPropPath)