0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-30 01:56:18 +01:00
hono/deno_dist/utils/json.ts
Yusuke Wada a2cc1a01c2
feat: another idea of Validator Middleware (#535)
* 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`
2022-09-20 10:11:34 +09:00

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
}