-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetValueAtPath.test.ts
49 lines (43 loc) · 1.29 KB
/
getValueAtPath.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { assertEquals } from '@std/assert';
import { getValueAtPath } from './getValueAtPath.ts';
Deno.test('getValueAtPath - can get values from simple object', () =>
{
const obj = {
name: 'test',
age: 25,
nested: {
key: 'value',
},
};
assertEquals(getValueAtPath(obj, ['name']), 'test');
assertEquals(getValueAtPath(obj, ['age']), 25);
assertEquals(getValueAtPath(obj, ['nested', 'key']), 'value');
assertEquals(getValueAtPath(obj, ['nonexistent']), undefined);
});
Deno.test('getValueAtPath - can get values from arrays', () =>
{
const obj = {
items: ['a', 'b', 'c'],
nested: [
{ id: 1 },
{ id: 2 },
],
};
assertEquals(getValueAtPath(obj, ['items', 1]), 'b');
assertEquals(getValueAtPath(obj, ['nested', 0, 'id']), 1);
assertEquals(getValueAtPath(obj, ['items', 99]), undefined);
});
Deno.test('getValueAtPath - handles null and undefined values', () =>
{
const obj = {
nullValue: null,
undefinedValue: undefined,
nested: {
nullValue: null,
},
};
assertEquals(getValueAtPath(obj, ['nullValue']), null);
assertEquals(getValueAtPath(obj, ['undefinedValue']), undefined);
assertEquals(getValueAtPath(obj, ['nested', 'nullValue']), null);
assertEquals(getValueAtPath(obj, ['nullValue', 'anything']), undefined);
});