-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathstyleUtils.ts
More file actions
98 lines (94 loc) · 2.59 KB
/
styleUtils.ts
File metadata and controls
98 lines (94 loc) · 2.59 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
import { Platform, processColor, type ColorValue } from 'react-native';
import type { MarkdownStyle } from './types/MarkdownStyle';
export const normalizeColor = (
color: string | undefined
): ColorValue | undefined => {
if (!color) return undefined;
if (Platform.OS === 'web') return color;
return processColor(color) ?? undefined;
};
export function mergeSubStyle<T extends Record<string, unknown>>(
defaultStyle: T,
userStyle?: Partial<T>
): T {
if (!userStyle) return defaultStyle;
const result: Record<string, unknown> = { ...defaultStyle, ...userStyle };
for (const key in result) {
const defaultValue = defaultStyle[key];
const userValue = userStyle[key];
if (
typeof defaultValue === 'object' &&
defaultValue !== null &&
!Array.isArray(defaultValue) &&
typeof userValue === 'object' &&
userValue !== null &&
!Array.isArray(userValue)
) {
result[key] = {
...(defaultValue as Record<string, unknown>),
...(userValue as Record<string, unknown>),
};
}
if (
key.toLowerCase().includes('color') &&
typeof result[key] === 'string'
) {
result[key] = normalizeColor(result[key] as string);
}
}
return result as T;
}
function isSubStyleEqual(
a: Record<string, unknown>,
b: Record<string, unknown>
): boolean {
const keys = Object.keys(a);
if (keys.length !== Object.keys(b).length) return false;
for (const key of keys) {
const valueA = a[key];
const valueB = b[key];
if (valueA === valueB) continue;
if (
typeof valueA === 'object' &&
valueA !== null &&
typeof valueB === 'object' &&
valueB !== null
) {
const nestedKeysA = Object.keys(valueA);
const nestedKeysB = Object.keys(valueB);
if (nestedKeysA.length !== nestedKeysB.length) return false;
for (const nestedKey of nestedKeysA) {
if (
(valueA as Record<string, unknown>)[nestedKey] !==
(valueB as Record<string, unknown>)[nestedKey]
) {
return false;
}
}
continue;
}
return false;
}
return true;
}
export function isStyleEqual(
a: MarkdownStyle,
b: MarkdownStyle,
referenceKeys: readonly string[]
): boolean {
for (const key of referenceKeys) {
const subA = a[key as keyof MarkdownStyle];
const subB = b[key as keyof MarkdownStyle];
if (subA === subB) continue;
if (!subA || !subB) return false;
if (
!isSubStyleEqual(
subA as Record<string, unknown>,
subB as Record<string, unknown>
)
) {
return false;
}
}
return true;
}