get

说明

根据object对象的path路径获取值

源码

用法

get.test.ts
1
import { get } from "../../utility/object/get";
2
3
describe('get() tests...', () => {
4
5
test('method get should return the correct value', () => {
6
const received: Array<{
7
origin: any,
8
path: string | string[],
9
defaultValue: any,
10
}> = [
11
{
12
origin: {},
13
path: 'a.b.c',
14
defaultValue: undefined,
15
},
16
{
17
origin: {},
18
path: 'a[0].b[1].c',
19
defaultValue: 'default',
20
},
21
{
22
origin: {
23
a: [
24
{
25
b: {
26
c: 3,
27
},
28
},
29
],
30
},
31
path: 'a[0].b.c',
32
defaultValue: undefined,
33
},
34
{
35
origin: [
36
{
37
a: [
38
{
39
b: 1998,
40
},
41
],
42
},
43
],
44
path: ['0', 'a', '0', 'b'],
45
defaultValue: undefined,
46
},
47
];
48
const expected = [undefined, 'default', 3, 1998];
49
50
received.forEach((v, i) => {
51
const result = get(v.origin, v.path, v.defaultValue);
52
53
expect(result).toBe(expected[i]);
54
});
55
});
56
});
Copied!
最近更新 1yr ago
复制链接