JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3054
  • Score
    100M100P100Q111103F
  • License ISC

A tiny (1-line) safe accessor that prevents 'Cannot read property of undefined' errors

Package Exports

  • safe-get

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (safe-get) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

sget

A tiny (1-line) safe accessor that prevents 'Cannot read property of undefined' errors.


How-to-get:

  • npm install safe-get -save
  • copy the following into your code somewhere
    • let _ = function (instance, path) { return path.split('.').reduce((p, c) => p ? p[c] : undefined, instance); }

How-to-use

//If you're not using node.js, ignore the following and take the copy-paste approach above
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.