Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

skava / chain-able-deps   js

Repository URL to install this package:

Version: 6.0.4 

/ src / reduce / clean.ts

/**
 * @file @@todo @@deps
 */
import { AnyObj } from '../_typings'
import { isReal, isEmpty } from '../is'
import ObjectKeys from '../util/keys'
import filterWhere from '../loop/filter/filterWhere'
import reduceToObj from './toObj'

// const [isNotReal, isNotEmpty] = [isReal, isEmpty].map(not)
// const isNotEmptyOrNotReal = or(isNotReal, isNotEmpty)
const mapNotEmpty = filterWhere('_', x => isReal(x) && !isEmpty(x))

/**
 * @desc goes through the maps,
 *       and the map values,
 *       reduces them to array
 *       then to an object using the reduced values
 *
 * @memberOf reduce
 * @since 4.0.0 <- moved as a dep function
 * @since 0.4.0
 *
 * @param {Object} obj object to clean, usually .entries()
 * @return {Object} reduced object, without `notReal` values
 *
 * @TODO seems to be overkill with reducing mapping just copy & ignore or delete?
 *
 * @see reduce
 * @see isObjWithKeys
 * @see isNotEmptyArray
 * @see isReal
 * @see http://underscorejs.org/#reduce
 *
 * @example
 *
 *   const map = new ChainedMap()
 *
 *   map
 *    .set('emptyArr', [])
 *    .set('arr', [1])
 *    .set('nill', null)
 *    .set('emptyObj', {})
 *    .set('obj', {keys: true})
 *
 *   clean(map.entries())
 *   //=> {arr: [1], obj: {keys: true}}
 *
 */
export default function clean(obj: AnyObj) {
  const mapped = mapNotEmpty(obj)
  const keys = ObjectKeys(mapped)
  const iterator = (reduced, key) => (reduced[key] = mapped[key])

  return reduceToObj(keys, iterator)
}