@btmills/default-map

An extension of ES2015's Map that provides a default value when accessing a key that has not been explicitly set.

Usage no npm install needed!

<script type="module">
  import btmillsDefaultMap from 'https://cdn.skypack.dev/@btmills/default-map';
</script>

README

@btmills/default-map

An extension of ES2015's Map that provides a default value when accessing a key that has not been explicitly set.

import DefaultMap from '@btmills/default-map';

const counter = new DefaultMap<string, number>(() => 0);
for (const word of input) {
    counter.set(word, counter.get(word) + 1);
}