Slides
Has DeepThis module is designed to simplify the painful process of validating the deep properties of an object.
About
var a = b: c: d: e: true ;
In JavaScript, if you try to find a property on an undefined
, the script will throw an error.
This sucks.
// this throws your appvar x = abcdefg;
The most common way to solve the problem is to have repetitious blocks of if-then code to slowly validate the property path.
This also sucks, but it works.
var x = a && ab && abc && abcd && abcde && abcdef && abcdefg ? abcdefg : 'winning';console; // => winning
I'm writing this module to help make it suck less.
var has = ; var x = ? abcdefg : 'winning';console; // => winning
Yeah, the result is the same, but look at how much less validation there is!
has(object, path)
object
- Object This is the source object that you want to validate.path
- String This is a period-delimited string that is the property path you want to validate.- Returns the value at the end of the
path
orundefined
if the path doesn't exist.
Example
var has = ; var a = b: c: d: e: 'winning!' f: g: false g: true ; console; // => undefinedconsole; // => undefinedconsole; // => "winning!"console; // => { e: "winning!" }console; // => falseconsole; // => true
Install
npm install has-deep
-or-
npm install ben-bradley/has-deep
Test
npm test
-or-
mocha -R spec
Versions
- 1.0.1 - Catch when first property is bracketed
- 1.0.0 - Able to handle dotted properties
- 0.0.6 - Using
Object.prototype.toString.call(object) === '[object Object]'
for whentoString()
is overwritten =( - 0.0.4 - Providing a non-Object variabl for evaluation will return
undefined
instead of Throwing - 0.0.3 - Refactored lib to be more functional, modified deep-array valitaion to detect falsey values, added comments
- 0.0.2 - Added deep-array validation
- 0.0.1 - Inital commit