Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
import set from 'lodash/set'
import { isNumberish, toEmpty } from 'exotic'
import { recurse } from 'chain'

/**
 * @desc wraps recursor to simplify usage
 *       skips array indexes
 *       skips root value
 *       slices/dereferences path
 *
 * @param {*} x data to recurse
 * @return {Function} recursor
 */
const recurser = x => {
  const recursion = recurse(x)
  recursion.forEachImportant = function(fn) {
    return recursion.forEach((value, key, traverser) => {
      if (!key) return
      if (isNumberish(key)) return
      fn(value, traverser.path.slice(0), traverser)
    })
  }
  return recursion
}

/**
 * @desc takes any data, returns, with all nested data turned to empty types
 * @param {*} x any data
 * @return {*} ALL empty data
 */
const toEmptyRecursive = x => {
  const Empty = toEmpty(x)
  recurser(x).forEachImportant((value, path) => {
    set(Empty, path, value)
  })
  return Empty
}

export { recurser, toEmptyRecursive }
export default toEmptyRecursive