hash-fetch

Utility to access keys of a hash and get an error if its undefined

Usage no npm install needed!

<script type="module">
  import hashFetch from 'https://cdn.skypack.dev/hash-fetch';
</script>

README

Adds Ruby style fetch to hashes in JavaScript.

Usage:

let hash = {first: 'Mister', last: 'Singh'};
hash = addFetch(nameHash);

Normal Access

hash['first'] // works and gives 'Mister'
hash['i-dont-exist'] // does not throw an error and gives undefined        

Access via fetch

hash.fetch('first') // works and gives 'Mister'
hash.fetch('i-dont-exist') // throws an error        

How to prevent modification of the original hash

let hash = {first: 'Mister', last: 'Singh'};
let newHash = addFetch(Object.assign({}, nameHash));
console.log(hash.fetch); //undefined
console.log(newHash.fetch); // Function