-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathconversionUtils.ts
More file actions
116 lines (97 loc) · 2.77 KB
/
Copy pathconversionUtils.ts
File metadata and controls
116 lines (97 loc) · 2.77 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
108
109
110
111
112
113
114
115
116
import type { Column, FilterOperator } from "@/data";
const TEXT_MATCH_OPERATORS = new Set<FilterOperator>([
"like",
"not like",
"ilike",
"not ilike",
]);
export function coerceToValue(
column: Column | undefined,
operator: FilterOperator,
value: string,
): unknown {
const dataTypeGroup = column?.datatype.group;
if (dataTypeGroup === undefined) {
return "";
}
if (operator === "is" || operator === "is not") {
return value.toLowerCase() === "null" ? null : value;
}
if (
column?.datatype.isArray &&
operator != null &&
!TEXT_MATCH_OPERATORS.has(operator)
) {
try {
const parsed = JSON.parse(value) as unknown;
return Array.isArray(parsed) ? parsed : value;
} catch {
return value;
}
}
if (dataTypeGroup === "boolean") return value === "true";
if (dataTypeGroup === "datetime") return value;
if (dataTypeGroup === "enum") return value;
if (dataTypeGroup === "raw") return value;
if (dataTypeGroup === "string") return value;
if (dataTypeGroup === "time") return value;
if (dataTypeGroup === "json") {
try {
return JSON.parse(value) as unknown;
} catch {
return value;
}
}
if (dataTypeGroup === "numeric") {
if (value === "") {
return null;
}
const parsed = Number(value);
// Only coerce input that actually parses as a number. Non-numeric text is
// kept as-is (never NaN) so e.g. date strings in SQLite NUMERIC-affinity
// columns survive, matching SQLite's own affinity semantics where
// non-numeric text is stored as TEXT.
return Number.isNaN(parsed) ? value : parsed;
}
return value;
}
export function coerceToString(
column: Column | undefined,
operator: FilterOperator | undefined,
value: unknown,
): string {
const dataTypeGroup = column?.datatype.group;
if (operator && (operator === "is" || operator === "is not")) {
return value === null ? "null" : String(value);
}
if (dataTypeGroup === undefined || value == null) {
return "";
}
if (
column?.datatype.isArray &&
operator != null &&
!TEXT_MATCH_OPERATORS.has(operator)
) {
try {
return JSON.stringify(value);
} catch {
return String(value);
}
}
if (dataTypeGroup === "boolean") return String(value);
if (dataTypeGroup === "datetime") return String(value);
if (dataTypeGroup === "enum") return String(value);
if (dataTypeGroup === "numeric") return String(value);
if (dataTypeGroup === "raw") return String(value);
if (dataTypeGroup === "string") return String(value);
if (dataTypeGroup === "time") return String(value);
if (dataTypeGroup === "json") {
if (value === "") return "";
try {
return JSON.stringify(value);
} catch {
return String(value);
}
}
return String(value);
}