feat(utils): getPropByPath function adjust (#1276)

Co-authored-by: 三咲智子 <sxzz@sxzz.moe>
This commit is contained in:
jectychen 2021-09-13 03:14:47 +08:00 committed by GitHub
parent 00d38e1807
commit 4d3a499fa6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -49,28 +49,37 @@ export function getPropByPath(
v: Nullable<unknown>
} {
let tempObj = obj
path = path.replace(/\[(\w+)\]/g, '.$1')
path = path.replace(/^\./, '')
let key, value
const keyArr = path.split('.')
let i = 0
for (i; i < keyArr.length - 1; i++) {
if (!tempObj && !strict) break
const key = keyArr[i]
if (obj && hasOwn(obj, path)) {
key = path
value = tempObj?.[path]
} else {
path = path.replace(/\[(\w+)\]/g, '.$1')
path = path.replace(/^\./, '')
if (key in tempObj) {
tempObj = tempObj[key]
} else {
if (strict) {
throw new Error('please transfer a valid prop path to form item!')
const keyArr = path.split('.')
let i = 0
for (i; i < keyArr.length - 1; i++) {
if (!tempObj && !strict) break
const key = keyArr[i]
if (key in tempObj) {
tempObj = tempObj[key]
} else {
if (strict) {
throw new Error('please transfer a valid prop path to form item!')
}
break
}
break
}
key = keyArr[i]
value = tempObj?.[keyArr[i]]
}
return {
o: tempObj,
k: keyArr[i],
v: tempObj?.[keyArr[i]],
k: key,
v: value,
}
}