All files / obj get.js

100% Statements 29/29
100% Branches 16/16
100% Functions 1/1
100% Lines 27/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 592x 2x                                             30x 27x 26x 26x 26x   26x 41x 41x 41x 14x 13x   14x 27x 1x 1x 26x 25x   1x 1x     26x 26x 5x 1x     26x     2x  
var $type = require('./type');
var $keyPathSplit = require('../str/keyPathSplit');
 
/**
 * 从对象路径取值(简易版)
 * @method obj/get
 * @see [lodash.get](https://lodash.com/docs/4.17.15#get)
 * @param {Object|Array} obj 要取值的对象或者数组
 * @param {String} xpath 要取值的路径
 * @param {Any} [defaultValue] 值为 undefined 则取此默认值
 * @returns {Any} 取得的值
 * @example
 * var $get = require('@spore-ui/kit/packages/obj/get');
 * var obj = {a: {b: {c: 1}}};
 * console.info($get(obj, 'a.b.c'); // 1
 * console.info($get(obj, 'e'); // undefined
 * console.info($get(obj, 'e', 3); // 3
 * var arr = [1, {a: [2, 3]}];
 * console.info($get(arr, '[1].a[1]'); // 3
 */
 
// 引用 lodash/get 会引入超过10kb 代码,用这个方法来精简 sdk 体积
 
function get(obj, xpath, def) {
  if (!obj) return undefined;
  if (typeof xpath !== 'string') return undefined;
  var arrXpath = $keyPathSplit(xpath);
  var point = obj;
  var len = arrXpath.length;
  var index;
  for (index = 0; index < len; index += 1) {
    var key = arrXpath[index];
    var otype = $type(point);
    if (otype === 'array') {
      if (/^\d+$/.test(key)) {
        key = parseInt(key, 10);
      }
      point = point[key];
    } else if (point === null) {
      point = undefined;
      break;
    } else if (typeof point === 'object') {
      point = point[key];
    } else {
      point = undefined;
      break;
    }
  }
  var value = point;
  if ($type(value) === 'undefined') {
    if ($type(def) !== 'undefined') {
      value = def;
    }
  }
  return value;
}
 
module.exports = get;