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 / reduce.ts

import { AnyObj, ObjWith } from '../_typings'
import ArrayFrom from '../util/from'

/**
 * @desc Map -> Object
 * @since 4.0.0
 *
 * @param {Map} map map to reduce, calls entries, turns into an array, then object
 * @return {Object} reduced object
 *
 * @see ArrayFrom
 *
 * @example
 *
 *    var emptyMap = new Map()
 *    reduce(emptyMap)
 *    //=> {}
 *
 * @example
 *
 *    var map = new Map()
 *    map.set('eh', 1)
 *    reduce(map)
 *    //=> {eh: 1}
 *
 */
export default <Key extends string, Value = any>(
  map: Map<Key, Value>
): ObjWith<Key, Value> => {
  let reduced = {}

  // only need to do this if we actually have values in our Map
  if (map.size !== 0) {
    reduced = ArrayFrom(map.entries()).reduce((acc: AnyObj, [key, value]) => {
      acc[key] = value
      return acc
    }, reduced)
  }

  return reduced
}