NuclearDispersionSystem/ant-design-vue-jeecg/node_modules/xe-utils/methods/base/get.js
2023-09-14 14:47:11 +08:00

54 lines
1.4 KiB
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var staticHGKeyRE = require('../static/staticHGKeyRE')
var helperGetHGSKeys = require('./helperGetHGSKeys')
var hasOwnProp = require('./hasOwnProp')
var isUndefined = require('./isUndefined')
var eqNull = require('./eqNull')
/**
* 获取对象的属性的值,如果值为 undefined则返回默认值
* @param {Object/Array} obj 对象
* @param {String/Function} property 键、路径
* @param {Object} defaultValue 默认值
* @return {Object}
*/
function get (obj, property, defaultValue) {
if (eqNull(obj)) {
return defaultValue
}
var result = pathGet(obj, property)
return isUndefined(result) ? defaultValue : result
}
function valGet (obj, key) {
var matchs = key ? key.match(staticHGKeyRE) : ''
return matchs ? (matchs[1] ? (obj[matchs[1]] ? obj[matchs[1]][matchs[2]] : undefined) : obj[matchs[2]]) : obj[key]
}
function pathGet (obj, property) {
if (obj) {
var rest, props, len
var index = 0
if (obj[property] || hasOwnProp(obj, property)) {
return obj[property]
} else {
props = helperGetHGSKeys(property)
len = props.length
if (len) {
for (rest = obj; index < len; index++) {
rest = valGet(rest, props[index])
if (eqNull(rest)) {
if (index === len - 1) {
return rest
}
return
}
}
}
return rest
}
}
}
module.exports = get