Repository URL to install this package:
|
Version:
2.0.1 ▾
|
import { hasOwnProp } from '../attributes/properties'
import { AnyObj } from '../../../typings'
/**
* Object into a Map
* @since 5.0.0-beta.6
* @memberOf cast
*
* @name objToMap
* @alias objectToMap
*
* @param {*} obj cast to Map
* @return {Map} Map(x)
*
* Object.keys(obj).forEach(key => map.set(key, obj[key]))
* @TODO use `forOwn`
* @TODO can just use obj.hasOwnProp again?
*
* {@link https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/entries#Converting_an_Object_to_a_Map mozilla-obj-to-map}
* @see {@link mozilla-obj-to-map}
*
* @example
*
* const obj = {eh: 0}
* const map = objToMap(obj)
*
* map.has('eh')
* //=> true
*
* map.get('eh')
* //=> 0
*
* map.size
* //=> 1
*
*/
const objToMap = <Obj = AnyObj>(obj: Obj): Map<keyof Obj, Obj[keyof Obj]> => {
const map = new Map()
// eslint-disable-next-line
for (const prop in obj) hasOwnProp(obj, prop) && map.set(prop, obj[prop])
return map
}
/**
* @TODO
* const map = pipe(entries, newMap)
* const map = newMap(Object.entries(obj))
*/
export { objToMap }
export default objToMap