mirror of
https://github.com/honojs/hono.git
synced 2024-11-30 01:56:18 +01:00
a2cc1a01c2
* feat: Another idea of validator middleware * denoify * set property value in constructor * rename `option` to `options` * add test for handling type error * use `test` instead of `match`
18 lines
438 B
TypeScript
18 lines
438 B
TypeScript
export const JSONPath = (data: object, path: string) => {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
let val: any = data
|
|
const parts = path.split('.')
|
|
const length = parts.length
|
|
for (let i = 0; i < length && val !== undefined; i++) {
|
|
const p = parts[i]
|
|
if (p !== '') {
|
|
if (typeof val === 'object') {
|
|
val = val[p]
|
|
} else {
|
|
val = undefined
|
|
}
|
|
}
|
|
}
|
|
return val
|
|
}
|