CollectionMap

1.0.2 • Public • Published

License

npm badge

CollectionMap

A simple extension of Map that auto-initializes new values with empty collections and auto-removes empty collections.

const {SetMap} = require('CollectionMap')
 
const map = SetMap()
// Getting by a non-existent key will return an empty initial value (a set by default)
map.has('foo') // -> false
map.get('foo') // -> Set {}
// This is useful for adding to a collection without needing to initialize it
map.set('bar', map.get('bar').concat([1, 2])) // -> CollectionMap { 'foo' => Set { 1, 2 } }
// Setting an empty value will automatically remove the entry
map.set('bar', new Set()) // -> CollectionMap {}
map.has('bar') // -> false
 
// Versions for maps and arrays are included
const {MapMap, ArrayMap} = require('CollectionMap')
 
// Creating a CollectionMap for custom types is simple
class CustomSet extends Set {}
// Provide the constructor for initializing values and the property name for testing for empty values
new CollectionMap(CustomSet, 'size').get('baz') // -> CustomSet {}
// Or alternatively:
const CustomSetMap = CollectionMap.bind(CustomSet, 'size')

Test

npm install
npm test

Package Sidebar

Install

npm i CollectionMap

Weekly Downloads

2

Version

1.0.2

License

MIT

Last publish

Collaborators

  • slikts