all files / src/utils/ get.js

100% Statements 17/17
100% Branches 8/8
100% Functions 1/1
100% Lines 17/17
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          27× 27× 27× 27× 27×   27× 11×     16× 25× 25×         13×                
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = get;
/* eslint-disable no-cond-assign */
function get(obj, prop) {
  var container = obj;
  var propName = prop;
  var parts = prop.split('.');
  var last = parts.pop();
  var falseyReturn = last === 'length' ? 0 : false;
 
  if (!container) {
    return falseyReturn;
  }
 
  while (propName = parts.shift()) {
    container = container[propName];
    if (container == null) {
      return falseyReturn;
    }
  }
 
  // we don't want to return undefined
  if (container[last] == null) {
    return falseyReturn;
  }
 
  return container[last];
}
 
/* we use `get` a lot to check for objects' contents,
 * mout's `get` throws an error if we give it an undefined object
 * this addresses that use case */