map-object-facade
Use map as object (e.g. map.a instead of map.get('a'))
Features
- Get and set values using the object dot notation.
- Supports Object calls like Object.keys(map) and delete map[key].
- Supports readonly (sealed) mode and noAdditions (frozen) mode.
- Provides a single function for maps (mapAsObject).
- Transparent: existing Map functionality is not changed.
Usage examples
let map = ;mapa = 1;map'b' = 2;map; mapa === map;mapsize === 3Object === map === 'a' 'b' 'c' delete mapbmapsize === 2mapb === map === undefinedObject === map === 'a' 'c' let readMap = ;
Readonly or noAdditions
Object.seal and Object.freeze cannot be set independently on a proxy (which is used in the implementation of objectFacade). Instead, options can be given in the mapAsObject(..) call.
Current supported options are:
- noAdditions (alias: seal) that allows changing values, but no new keys (default: false)
- readonly (alias: freeze) that allows no changes whatsoever (default: false)
- throwOnIgnoredSet will throw if set is ignored due to noAdditions / readonly (default: false -- meaning sets will be silently ignored if not allowed).
For example:
mapAsObject(map, {noAdditions:true, throwOnIgnoredSet:true})
'seal' and 'freeze' are used as alias because they are known names (though not very descriptive).
Changelog
Install
$ npm install map-object-facade
License
MIT (C) Nicolas de Jong