Code coverage report for lib/obj.js

Statements: 100% (13 / 13)      Branches: 100% (8 / 8)      Functions: 100% (2 / 2)      Lines: 100% (13 / 13)      Ignored: none     

All files » lib/ » obj.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39              1   7                   1   15 15   15     15 22 22 10       15     1 1
/**
 * Check whether the specified nested properties exist in an object or not.
 *
 * @param {String} dsv: dot-separated nested properties name
 * @param {Object} obj: the object to check against whether the nested properties exist
 * @return {Boolean} true if nested properties value exist, false otherwise
 */
function exist(dsv, obj) {
 
  return value(dsv, obj) !== undefined;  
}
 
/**
 * Retrieve the value of nested properties within an object.
 *
 * @param {String} dsv: dot-separated nested properties name
 * @param {Object} obj: the object to find the nested properties from
 * @return {?} value of the nested properties
 */
function value(dsv, obj) {
 
  dsv = dsv || '';
  obj = obj || {};
 
  var props = dsv.split('.'),
    _value;
 
  for (var i = 0, ln = props.length; i < ln; i += 1) {
    _value = (_value) ? _value[props[i]] : obj[props[i]];
    if (_value === undefined) {
      break;
    }
  }
 
  return _value;
}
 
exports.exist = exist;
exports.value = value;