-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathobjects.ts
More file actions
107 lines (86 loc) · 2.38 KB
/
objects.ts
File metadata and controls
107 lines (86 loc) · 2.38 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* eslint-disable */
import { ShortHandSymbol } from "../native/styles/constants";
import { transformKeys } from "../native/styles/defaults";
export function getDeepPath(source: any, paths: string | string[] | false) {
if (!source) {
return;
}
if (paths === false) {
return undefined;
}
if (Array.isArray(paths)) {
let target = source;
for (const path of paths) {
if (typeof target !== "object" || !target || !(path in target)) {
return undefined;
}
target = target[path];
}
return target;
} else if (transformKeys.has(paths)) {
return source?.transform?.find((t: any) => t[paths] !== undefined);
} else {
return source?.[paths];
}
}
export function applyShorthand(value: any) {
if (value === undefined) {
return;
}
const target: Record<string, unknown> = { [ShortHandSymbol]: true };
const values = value as [unknown, string][];
for (const [value, prop] of values) {
target[prop] = value;
}
return target;
}
export function applyValue(
target: Record<string, any>,
prop: string,
value: any,
) {
// This is confusing.
// An undefined value means "don't set anything" (something failed while parsing)
// While a null value means "remove this value", which in React Native means "set to undefined"
if (value === undefined) {
return;
} else if (value === null) {
value = undefined;
}
if (transformKeys.has(prop)) {
if (!Array.isArray(target.transform)) {
target.transform = [];
}
const transformArray: Record<string, unknown>[] = target.transform;
// Remove any existing values
target.transform = transformArray.filter((t) => !(prop in t));
if (Array.isArray(value)) {
target.transform.push(...value);
} else {
target.transform.push(value);
}
return;
} else if (typeof value === "object" && value && ShortHandSymbol in value) {
delete value[ShortHandSymbol];
Object.assign(target, value);
return;
}
target[prop] = value;
}
export function setDeepPath(
target: Record<string, any>,
paths: string | string[] | readonly string[],
value: any,
) {
if (typeof paths === "string") {
target[paths] = value;
return target;
}
for (let i = 0; i < paths.length - 1; i++) {
const path = paths[i]!;
target[path] ??= {};
target = target[path];
}
target[paths[paths.length - 1]!] = value;
return target;
}