Maps
Conversions and transforms for the native Map — to and from plain objects, merge, invert, map values. Map is the right structure for non-string keys, guaranteed insertion order and frequent adds and deletes, but it has none of the spread-and-merge ergonomics objects enjoy, which is the gap these fill. Every helper returns a new Map; later maps win on key collisions in mergeMaps, matching object spread.
Example
import { mapToObject, mergeMaps, objectToMap } from '@rtorcato/js-common/maps'
// Defaults plus overrides, then back to a plain object for JSON.
const merged = mergeMaps(
objectToMap({ theme: 'light', lang: 'en' }),
objectToMap({ theme: 'dark' })
)
mapToObject(merged) // { theme: 'dark', lang: 'en' } — later maps win
Import
import { invertMap, mapToObject, mapValues } from '@rtorcato/js-common/maps'
Exports
| Name | Summary |
|---|---|
invertMap | Inverts a Map (swaps keys and values). |
mapToObject | Converts a Map to an object. |
mapValues | Maps the values of a Map using a function. |
mergeMaps | Merges two or more maps. |
objectToMap | Converts an object to a Map. |