CollectionMap
A simple extension of Map
that auto-initializes new values with empty collections and auto-removes empty collections.
const SetMap = const map = // Getting by a non-existent key will return an empty initial value (a set by default)map // -> falsemap // -> Set {}// This is useful for adding to a collection without needing to initialize itmap // -> CollectionMap { 'foo' => Set { 1, 2 } }// Setting an empty value will automatically remove the entrymap // -> CollectionMap {}map // -> false // Versions for maps and arrays are includedconst MapMap ArrayMap = // Creating a CollectionMap for custom types is simple {}// Provide the constructor for initializing values and the property name for testing for empty valuesCustomSet 'size' // -> CustomSet {}// Or alternatively:const CustomSetMap = CollectionMap
Test
npm install
npm test