prolly-put

A put method for Array, Map, Set, WeakMap, and WeakSet.


Keywords
put, set, weakmap, weakset, map, collections, array
License
ISC
Install
npm install prolly-put@1.0.2

Documentation

prolly-put

Build Status Coverage Status

A put method for Array, Map, Set, WeakMap, and WeakSet.

What

Once upon a time, there was a simple way to retrieve or create some value once.

const value = obj[key] || (obj[key] = create());

During those times, TC39 members didn't believe that was a nice to have pattern, representable via new .set or .add methods for both Maps and Sets.

// how me and few others were hoping to have
const value = map.get(key) || map.set(key, create());

// what ES5 offered instead
const value = map.get(key) ||
              map.set(key, create()).get(key);

Accordingly, what I've always missed with both arrays, maps, or sets, is a way to retrieve a value once or create and set it once if absent.

const value = array.put(create());
// same for WeakMaps
const value = map.get(key) || map.put(key, create());
// same for WeakSets
const value = set.has(value) || set.put(value);

There is also a never ambiguous and natural pattern for those cases where a map value could be falsy.

const value = map.has(key) ?
                map.get(key) :
                map.put(key, create());

Covering all my needs since ES5.

This prollyfill would like to bring in the put method for Array, Maps, Sets, WeakMaps and WeakSets too.