hash-fetch
v1.0.3
Published
Utility to access keys of a hash and get an error if its undefined
Downloads
7
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