check-defined

Simple utility function for checking isDefined for complex objects with long property paths

Usage no npm install needed!

<script type="module">
  import checkDefined from 'https://cdn.skypack.dev/check-defined';
</script>

README

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