A small safe accessor that prevents 'Cannot read property of undefined' errors.
JavaScript
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.npmignore
README.md
index.js
package.json
tests.js

README.md

sget

A small safe accessor that prevents 'Cannot read property of undefined' errors.


How-to-get:

  • npm install safe-get -save

How-to-use

const _ = require ('safe-get');
const myObject = {
foo: {
   bar: {
       baz: ['sget','protects','me']
       }
   }
}
let result = _(myObject,'foo.bar.baz[1]'); // result === 'protects'
result = _(myObject,'foo.missingProperty.this.could.go.on.forever'); // result = undefined - no Exception hooray!

More-info

See this blog post on wakecoder.com for more detail. Also, see tests.js for more examples.