Simple and natural nested property accessor. For node and browser.
Switch branches/tags
Nothing to show
Clone or download
Latest commit 76d30cf Aug 31, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci config change Feb 11, 2018
img logo v5 Jan 24, 2018
src readme usage Feb 8, 2018
.babelrc added babel Feb 15, 2018
.eslintrc new project structure, rollup configs Feb 5, 2018
.gitignore test witch coverage Feb 7, 2018
.npmignore packaga json fix and npmignore file Feb 7, 2018
LICENSE Initial commit Jan 24, 2018
README.md dependencies badge Feb 9, 2018
package-lock.json lock update Aug 31, 2018
package.json npm audit fix and es build Aug 31, 2018
rollup.config.iife.js new project structure, rollup configs Feb 5, 2018
rollup.config.js npm audit fix and es build Aug 31, 2018
yarn.lock lock update Aug 31, 2018

README.md

portfolio_view

CircleCI Coverage Status BCH compliance License: MIT dependencies Status

NPM

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:

<script src="dist/guardy-iife.min.js"></script>

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.

import { guardy } from "guardy";

var nestedProp = guardy(myObj).I.can.safely.access.any.property;

With defined fallback value

import { guardyFb } from "guardy";

var nestedProp = guardyFb(myObj, "myFallBackStringOrAnyType").I.can.safely.access.any.property.__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 = ({ NestedReducerState }) => (
    {
       var nestedStateObject = guardy(NestedReducerState).a.nested.object;
       var nestedStateString = guardyWithFallback(NestedReducerState, "defaultString").a.nested.string.__value__;

       return {
           nestedStateObject,
           nestedStateString
       }
    }
);

Compatibility

Licensing

The code in this project is licensed under MIT license.