UNPKG

crocks

Version:

A collection of well known Algebraic Datatypes for your utter enjoyment.

60 lines (44 loc) 1.41 kB
/** @license ISC License (c) copyright 2018 original and current authors */ /** @author Ian Hofmann-Hicks (evil) */ var curry = require('../core/curry') var isArray = require('../core/isArray') var isEmpty = require('../core/isEmpty') var isInteger = require('../core/isInteger') var isObject = require('../core/isObject') var isString = require('../core/isString') var array = require('../core/array') var object = require('../core/object') var pathError = 'unsetPath: Non-empty Array of non-empty Strings and/or Positive Integers required for first argument' // unsetPath :: [ String | Integer ] -> a -> a function unsetPath(path, obj) { if(!isArray(path) || isEmpty(path)) { throw new TypeError(pathError) } if(!(isObject(obj) || isArray(obj))) { return obj } var key = path[0] if(!(isString(key) && !isEmpty(key) || isInteger(key) && key >= 0)) { throw new TypeError(pathError) } if(path.length === 1) { if(isArray(obj) && isInteger(key)) { return array.unset(key, obj) } if(isObject(obj) && isString(key)) { return object.unset(key, obj) } return obj } var next = obj[key] if(!(isObject(next) || isArray(next))) { return obj } if(isArray(obj)) { return array.set(key, unsetPath(path.slice(1), next), obj) } return object.set(key, unsetPath(path.slice(1), next), obj) } module.exports = curry(unsetPath)