crocks
Version:
A collection of well known Algebraic Datatypes for your utter enjoyment.
50 lines (38 loc) • 1.34 kB
JavaScript
/** @license ISC License (c) copyright 2019 original and current authors */
/** @author Dale Francis (dalefrancis88) */
var curry = require('../core/curry')
var isDefined = require('../core/isDefined')
var isEmpty = require('../core/isEmpty')
var isFoldable = require('../core/isFoldable')
var isInteger = require('../core/isInteger')
var isNil = require('../core/isNil')
var isString = require('../core/isString')
// err :: String
var err =
'hasProps: First argument must be a Foldable of Non-empty Strings or Integers'
// isKeyValid :: a -> Boolean
var isKeyValid = function (key) { return isString(key) && !isEmpty(key) || isInteger(key); }
// hasKey :: a -> (String | Integer) -> Boolean
var hasKey = function (obj) { return function (key) {
if(!isKeyValid(key)) {
throw new TypeError(err)
}
return isDefined(obj[key])
}; }
// every :: (a -> Boolean) -> ((Null | Boolean), a) -> Boolean
var every = function (fn) { return function (acc, x) { return (acc === null ? true : acc) && fn(x); }; }
// hasProps :: Foldable f => f (String | Integer) -> a -> Boolean
function hasProps(keys, x) {
if(!isFoldable(keys)) {
throw new TypeError(err)
}
if(isNil(x)) {
return false
}
var result = keys.reduce(
every(hasKey(x)),
null
)
return result === null || result
}
module.exports = curry(hasProps)