ensure-key

Safe member access with initial value assignment.

Usage no npm install needed!

<script type="module">
  import ensureKey from 'https://cdn.skypack.dev/ensure-key';
</script>

README

Ensure-Key

Lets you access members of an object safely (will be added if missing and initialized to a value you provide).

How to use:

import './Ensure'; 

const registry = {}; 

registry.ensure('abc', {}).ensure('items', []).push(123); 

console.log(registry);  // --> { abc: { items: [ 123 ] } }

The ensure method is added to the Object prototype and is thus available on every object. It is hidden from enumeration (to not cause interferences with environments).