element-plus/packages/utils/objects.ts
三咲智子 6503e55277
refactor(utils): migrate utils (#5949)
* refactor(utils-v2): migrate utils

* refactor(utils-v2): migrate utils

* refactor(utils-v2): migrate utils

* refactor(utils): remove

* refactor(utils): rename

* refactor(utils): move EVENT_CODE to constants

* refactor: remove generic
2022-02-11 11:03:15 +08:00

55 lines
1.2 KiB
TypeScript

import { hasOwn } from '@vue/shared'
import { throwError } from './error'
import type { Entries } from 'type-fest'
const SCOPE = 'UtilV2/objects'
export const keysOf = <T>(arr: T) => Object.keys(arr) as Array<keyof T>
export const entriesOf = <T>(arr: T) => Object.entries(arr) as Entries<T>
export { hasOwn } from '@vue/shared'
/** @deprecated TODO: improve it, use lodash */
export function getPropByPath(
obj: any,
path: string,
strict: boolean
): {
o: any
k: string
v: any
} {
let tempObj = obj
let key, value
if (obj && hasOwn(obj, path)) {
key = path
value = tempObj?.[path]
} else {
path = path.replace(/\[(\w+)\]/g, '.$1')
path = path.replace(/^\./, '')
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) {
throwError(SCOPE, 'Please transfer a valid prop path to form item!')
}
break
}
}
key = keyArr[i]
value = tempObj?.[keyArr[i]]
}
return {
o: tempObj,
k: key,
v: value,
}
}