1.0.1 • Published 9 years ago

check-defined v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

check-defined

npm Build Status

Simple Javascript utility function for checking if your complex object are defined all along the path. For example:

//instead of writing
if ((typeof a !== 'undefined') && (typeof a.b !== 'undefined') && (typeof a.b.c !== 'undefined'))

//write
if (isDefined(a, 'b.c'))

Installation

Use npm to install

npm install check-defined

Include with node.js, browserify or webpack:

var isDefined = require('check-defined');
isDefined(a, 'b.c')

Or you could add index.js on your page with a <script> tag and it will export a global isDefined method, or define the module if you are using RequireJS.

Usage

isDefined function takes one or two arguments. One argument version works just as your regular check for typeof === 'undefined'. Two parameter version checks every object of the object chain for undefined.

var a = {};
a.b = {};
isDefined(a);         // => true
isDefined(a, 'b');    // => true
isDefined(a, 'b.c');  // => false

Planned features

  • Add checks for null and NaN

License

MIT