@discuzq/sdk
Version:
discuz dsk
35 lines (31 loc) • 897 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.get = void 0;
/**
* 取数方法
* @param source 源对象
* @param path 路径
* @param defaultValue 默认值
* ```js
* async() => {
* const data = await axios.get('');
* const example = get(data, 'data.a.b.c.d.e', 1);
* // 如果取不到
* console.log(example) //1
* // 如果取的到
* console.log(example) //对应的值
* }
* ```
*/
var get = function get(source, path, defaultValue) {
var travel = function travel(regexp) {
return String.prototype.split.call(path, regexp).filter(Boolean).reduce(function (res, key) {
return res !== null && res !== undefined ? res[key] : res;
}, source);
};
var result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/);
return result === undefined || result === source ? defaultValue : result;
};
exports.get = get;