-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathnormalizeEmbeddedViewConfig.ts
More file actions
75 lines (72 loc) · 2.16 KB
/
Copy pathnormalizeEmbeddedViewConfig.ts
File metadata and controls
75 lines (72 loc) · 2.16 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
import type { IterableEmbeddedViewConfig } from '../types/IterableEmbeddedViewConfig';
const NUMERIC_KEYS: (keyof Pick<
IterableEmbeddedViewConfig,
'borderWidth' | 'borderCornerRadius'
>)[] = ['borderWidth', 'borderCornerRadius'];
function coerceNumericField(
key: 'borderWidth' | 'borderCornerRadius',
value: unknown
): number | undefined {
if (value === undefined || value === null) {
return undefined;
}
if (typeof value === 'number') {
if (Number.isFinite(value)) {
return value;
}
console.warn(
`[IterableEmbeddedView] Ignoring ${String(key)}: expected a finite number, got ${String(value)}`
);
return undefined;
}
if (typeof value === 'string') {
const trimmed = value.trim();
if (trimmed === '') {
console.warn(
`[IterableEmbeddedView] Ignoring ${String(key)}: empty string is not a valid number`
);
return undefined;
}
const n = parseFloat(trimmed);
if (Number.isFinite(n)) {
return n;
}
console.warn(
`[IterableEmbeddedView] Ignoring ${String(key)}: could not parse string as a number: ${JSON.stringify(value)}`
);
return undefined;
}
console.warn(
`[IterableEmbeddedView] Ignoring ${String(key)}: expected number or numeric string, got ${typeof value}`
);
return undefined;
}
/**
* Returns a shallow copy of config with numeric fields coerced from strings when possible.
* Values that cannot be coerced are omitted so style resolution can fall back to defaults.
*/
export function normalizeEmbeddedViewConfig(
config: IterableEmbeddedViewConfig | null | undefined
): IterableEmbeddedViewConfig | null | undefined {
if (config == null) {
return config;
}
const next: IterableEmbeddedViewConfig = { ...config };
const loose = config as Record<string, unknown>;
for (const key of NUMERIC_KEYS) {
const raw = loose[key as string];
if (raw === undefined) {
continue;
}
if (typeof raw === 'number' && Number.isFinite(raw)) {
continue;
}
const coerced = coerceNumericField(key, raw);
if (coerced === undefined) {
delete next[key];
} else {
next[key] = coerced;
}
}
return next;
}