Repository URL to install this package:
|
Version:
1.1.17 ▾
|
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