Has Deep

Deep property validation that doesn't suck.

 

https://github.com/ben-bradley/has-deep
https://npmjs.org/package/has-deep

The Problem

// test.js

'use strict';

var a = {
    b: {
        c: {
            d: 'a defined value'
        }
    }
};

var test = function (a) {
    if (a.b.c.d.e.f)
        return true;
    else
        return false;
};

console.log(test(a));

The Problem

$ node ./test.js

// boom!

/Users/M53565/development/sandbox/hasdeep/test.js:12
    if (a.b.c.d.e.f)
                 ^
TypeError: Cannot read property 'f' of undefined
    at test (/Users/M53565/development/sandbox/hasdeep/test.js:12:18)
    at Object.<anonymous> (/Users/M53565/development/sandbox/hasdeep/test.js:18:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

The Workaround

// test.js

'use strict';

var a = {
    b: {
        c: {
            d: 'a defined value'
        }
    }
};

var test = function (a) {
    if (a && a.b && a.b.c && a.b.c.d && a.b.c.d.e && a.b.c.d.e.f)
        return true;
    else
        return false;
};

console.log(test(a));

The Workaround

$ node ./test.js
false

A Better Way

// test.js

'use strict';

var has = require('has-deep');

var a = {
    b: {
        c: {
            d: 'a defined value'
        }
    }
};

var test = function (a) {
    if (has(a, 'b.c.d.e.f'))
        return true;
    else
        return false;
};

console.log(test(a));

A Better Way

$ node ./test.js
false

So What?

// convert this xml to json

<xmlroot>
    <objectpropertygroup>
        <objectproperty>
            <property>A defined value</property>
        </objectproperty>
    </objectpropertygroup>
</xmlroot>

So What?

// xmlPuller.js

'use strict';

var request = require('request'),
  xml2json = require('xml2json');

var getJson = function(callback) {
    request.get('http://example.com/webservice.xml', function(err, res, xml) {
        var json = xml2json.toJson(xml);
        callback(err, json);
    });
}

getJson(function(err, json) {
    if (err)
        throw err;
    if (
        json &&
        json.xmlroot &&
        json.xmlroot[0] &&
        json.xmlroot[0].objectpropertygroup &&
        json.xmlroot[0].objectpropertygroup[0] &&
        json.xmlroot[0].objectpropertygroup[0].objectproperty &&
        json.xmlroot[0].objectpropertygroup[0].objectproperty[0] &&
        json.xmlroot[0].objectpropertygroup[0].objectproperty[0].property
       )
        console.log(json.xmlroot[0].objectpropertygroup[0].objectproperty[0].property);
});

So What?

// xmlPuller.js

'use strict';

var request = require('request'),
  xml2json = require('xml2json'),
  has = require('has-deep');

var getJson = function(callback) {
    request.get('http://example.com/webservice.xml', function(err, res, xml) {
        var json = xml2json.toJson(xml);
        callback(err, json);
    });
}

getJson(function(err, json) {
    if (err)
        throw err;
    var property = has(json, 'xmlroot[0].objectpropertygroup[0].objectproperty[0].property');
    if (property)
        console.log(property);
});

Has Deep

By ben-bradley

Has Deep

A walk-through of the has-deep module

  • 1,472