Skip to content

javascript utility function to test if object property nesting

Notifications You must be signed in to change notification settings

atul-github/isdef

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

isdefined

This is simple javascript utiity to check if object property is defined. This is to save lengthy check and to avoid error.

Usage

var assert=require('assert');
var isdef = require('ifdefined');

var x={};
x.y={};
x.y.z={};
x.y.z.a=1;

assert (isdef(x, "y.z.a") !== undefined, "Test Failed!!")
assert (isdef(x, "y.z.a.m") === undefined, "Test Failed!!")

In above example, to protect code for undesirable exception, you would write code like

if ( x && x.y && x.y.z && x.y.z.a ){
  // use x.y.z.a 
}

Instead, you can write simply

if ( isdef(x, 'y.z.a') ){
  // safely use x.y.z.a
}

About

javascript utility function to test if object property nesting

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published