Simple and natural nested property accessor.
Protect yourself from "Cannot read property 'x' of undefined" runtime exception.
Installing
Package is available in npm. It's in umd format so it will work correctly with all popular bundlers(webpack) and also nodejs.
npm install guardy --save
Using yarn
yarn add guardy --save
For direct usage without bundler, use iife format and import file directly:
Usage
Wrap object with guardy and then access with no stress any property at any nested level. If not defined it will fallback to empty object {} instead of throwing "Cannot read property 'x' of undefined" runtime exception.
; var nestedProp = Icansafelyaccessanyproperty;
With defined fallback value
; var nestedProp = Icansafelyaccessanyproperty__value__;
Guardy can be successfully used together with redux when trying to access not yet initialized state properties by providing safe fallback values.
const mapStateToProps = var nestedStateObject = anestedobject; var nestedStateString = anestedstring__value__; return nestedStateObject nestedStateString ;
Compatibility
- nodejs >= 6.4.0
- browser with Proxy compatibility, see https://caniuse.com/#search=Proxy
Licensing
The code in this project is licensed under MIT license.