-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathget.ts
More file actions
24 lines (23 loc) · 771 Bytes
/
get.ts
File metadata and controls
24 lines (23 loc) · 771 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import {hasOwnProperty as has} from '@jsonjoy.com/util/lib/hasOwnProperty';
import type {Path} from './types';
export const get = (val: unknown, path: Path): unknown | undefined => {
const pathLength = path.length;
let key: string | number;
if (!pathLength) return val;
for (let i = 0; i < pathLength; i++) {
key = path[i];
if (val instanceof Array) {
if (typeof key !== 'number') {
if (key === '-') return undefined;
const key2 = ~~key;
if ('' + key2 !== key) return undefined;
key = key2;
}
val = val[key];
} else if (typeof val === 'object') {
if (!val || !has(val as object, key as string)) return undefined;
val = (val as any)[key];
} else return undefined;
}
return val;
};