"use strict";

exports.__esModule = true;
exports.get = void 0;

/**
 * ---
 *  获取对象或数组的属性对应值
 *  * 主要用于深度获取未知对象的值.
 * @param {(array | object)} obj  对象或者数组
 * @param {string} key  对象的key值
 * @param {string} [split="."]  分隔符
 * @return {*} 返回key对应的任何数据格式的值
 * @function get
 * @static
 * @example 01 常见获取值
 * --------------------------------------------------------------------------
 * import { get } from '@riil-frontend/utils';
 * const obj = { a: { b: { c: [{'.f.m':'age'}, { d: 'name' }] } } };
 * get(obj, 'a.b.c.1.d');  // "name"
 *
 * @example 02 未知深度
 * --------------------------------------------------------------------------
 * get(obj, 'm.b.c.1.d');  // undefined
 * @example 03 自定义分割
 * --------------------------------------------------------------------------
 * get(obj, 'a@b@c@0@.f.m', '@') //age
 *
 */
var get = function get(obj, key) {
  var split = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '.';

  if (obj && typeof key == 'string') {
    var keys = key.split(split);
    var value = obj;

    for (var i = 0; i < keys.length; i++) {
      var _value = value[keys[i]];

      if ('undefined' != typeof _value) {
        value = _value;
      } else {
        return undefined;
      }
    }

    return value;
  }

  return undefined;
};

exports.get = get;