-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathformat_query_params.test.ts
More file actions
300 lines (268 loc) · 7.17 KB
/
Copy pathformat_query_params.test.ts
File metadata and controls
300 lines (268 loc) · 7.17 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { describe, it, expect } from "vitest";
import { formatQueryParams, TupleParam } from "../../src/index";
describe("formatQueryParams", () => {
it("formats null", () => {
expect(
formatQueryParams({
value: null,
}),
).toBe("\\N");
});
it("formats undefined", () => {
expect(
formatQueryParams({
value: undefined,
}),
).toBe("\\N");
});
it("formats boolean", () => {
expect(
formatQueryParams({
value: true,
}),
).toBe("1");
expect(
formatQueryParams({
value: false,
}),
).toBe("0");
});
it("formats number", () => {
expect(
formatQueryParams({
value: 1,
}),
).toBe("1");
});
it("formats BigInt", () => {
expect(
formatQueryParams({
value: 0n,
}),
).toBe("0");
expect(
formatQueryParams({
value: -1n,
}),
).toBe("-1");
expect(
formatQueryParams({
value: 123456789012345678901234567890n,
}),
).toBe("123456789012345678901234567890");
});
it("formats NaN", () => {
expect(
formatQueryParams({
value: NaN,
}),
).toBe("nan");
});
it("formats Infinity", () => {
expect(
formatQueryParams({
value: Infinity,
}),
).toBe("+inf");
expect(
formatQueryParams({
value: +Infinity,
}),
).toBe("+inf");
expect(
formatQueryParams({
value: -Infinity,
}),
).toBe("-inf");
});
it("formats an array", () => {
expect(formatQueryParams({ value: [1, 2, 3] })).toBe("[1,2,3]");
});
it("formats an empty Array", () => {
expect(formatQueryParams({ value: [] })).toBe("[]");
});
it("formats a date without timezone", () => {
const date = new Date(Date.UTC(2022, 6, 29, 7, 52, 14));
expect(
formatQueryParams({
value: date,
}),
).toBe("1659081134");
});
it("formats a date with only nine digits in its Unix timestamp (seconds)", () => {
const date = new Date(Date.UTC(1973, 10, 29, 21, 33, 9));
expect(
formatQueryParams({
value: date,
}),
).toBe("0123456789");
});
it("formats a date with millis", () => {
expect(
formatQueryParams({
value: new Date(Date.UTC(2022, 6, 29, 7, 52, 14, 123)),
}),
).toBe("1659081134.123");
expect(
formatQueryParams({
value: new Date(Date.UTC(2022, 6, 29, 7, 52, 14, 42)),
}),
).toBe("1659081134.042");
expect(
formatQueryParams({
value: new Date(Date.UTC(2022, 6, 29, 7, 52, 14, 5)),
}),
).toBe("1659081134.005");
});
it("does not wrap a string in quotes", () => {
expect(
formatQueryParams({
value: "hello",
}),
).toBe("hello");
});
it("escapes special characters in an input string", () => {
expect(formatQueryParams({ value: "hel'lo" })).toBe("hel\\'lo");
expect(formatQueryParams({ value: "hel\\lo" })).toBe("hel\\\\lo");
expect(formatQueryParams({ value: "hel\tlo" })).toBe("hel\\tlo");
expect(formatQueryParams({ value: "hel\nlo" })).toBe("hel\\nlo");
expect(formatQueryParams({ value: "hel\rlo" })).toBe("hel\\rlo");
});
it("wraps strings in an array in quotes", () => {
expect(formatQueryParams({ value: ["1", "2"] })).toBe("['1','2']");
});
it("formats an object and escapes keys and values", () => {
expect(
formatQueryParams({
value: {
["na'me"]: "cust'om",
},
}),
).toBe("{'na\\'me':'cust\\'om'}");
expect(
formatQueryParams({
value: {
["a'b\nc\td\re\\"]: "\\q'w\ne\tr\rt\\y",
},
}),
).toBe("{'a\\'b\\nc\\td\\re\\\\':'\\\\q\\'w\\ne\\tr\\rt\\\\y'}");
});
it("formats a nested object", () => {
expect(
formatQueryParams({
value: {
name: "custom",
id: 42,
params: { refs: [44] },
},
}),
).toBe("{'name':'custom','id':42,'params':{'refs':[44]}}");
});
it("formats booleans in arrays as TRUE/FALSE", () => {
expect(formatQueryParams({ value: [true, false] })).toBe("[TRUE,FALSE]");
expect(formatQueryParams({ value: [true] })).toBe("[TRUE]");
expect(formatQueryParams({ value: [false] })).toBe("[FALSE]");
});
it("formats booleans in nested arrays as TRUE/FALSE", () => {
expect(
formatQueryParams({
value: [
[true, false],
[false, true],
],
}),
).toBe("[[TRUE,FALSE],[FALSE,TRUE]]");
expect(
formatQueryParams({
value: [[[true]], [[false]]],
}),
).toBe("[[[TRUE]],[[FALSE]]]");
});
it("formats booleans in arrays with mixed types", () => {
expect(formatQueryParams({ value: [1, true, "test", false, null] })).toBe(
"[1,TRUE,'test',FALSE,NULL]",
);
});
it("formats booleans in tuples as TRUE/FALSE", () => {
expect(
formatQueryParams({
value: new TupleParam([true, false]),
}),
).toBe("(TRUE,FALSE)");
expect(
formatQueryParams({
value: new TupleParam([1, true, "test", false]),
}),
).toBe("(1,TRUE,'test',FALSE)");
});
it("formats booleans in nested tuples as TRUE/FALSE", () => {
expect(
formatQueryParams({
value: new TupleParam([new TupleParam([true, false]), true]),
}),
).toBe("((TRUE,FALSE),TRUE)");
});
it("formats booleans in objects (Maps) as TRUE/FALSE", () => {
expect(
formatQueryParams({
value: {
isActive: true,
isDeleted: false,
},
}),
).toBe("{'isActive':TRUE,'isDeleted':FALSE}");
});
it("formats booleans in nested structures", () => {
expect(
formatQueryParams({
value: {
name: "test",
flags: [true, false],
tuple: new TupleParam([false, true]),
},
}),
).toBe("{'name':'test','flags':[TRUE,FALSE],'tuple':(FALSE,TRUE)}");
});
it("formats a Date inside an array as a quoted date string", () => {
expect(
formatQueryParams({
value: [new Date(Date.UTC(2022, 6, 29, 7, 52, 14))],
}),
).toBe("['2022-07-29']");
});
it("formats a Date inside a nested array as a quoted date string", () => {
expect(
formatQueryParams({
value: [[new Date(Date.UTC(2023, 4, 5))]],
}),
).toBe("[['2023-05-05']]");
});
it("formats a Date inside a tuple as a quoted date string", () => {
expect(
formatQueryParams({
value: new TupleParam([new Date(Date.UTC(2023, 4, 5))]),
}),
).toBe("('2023-05-05')");
});
it("formats a Date inside an object value as a quoted date string", () => {
expect(
formatQueryParams({
value: { d: new Date(Date.UTC(2023, 4, 5)) },
}),
).toBe("{'d':'2023-05-05'}");
});
it("uses the UTC date and drops the time for a Date inside an array", () => {
expect(
formatQueryParams({
value: [new Date(Date.UTC(2022, 6, 29, 23, 59, 59, 999))],
}),
).toBe("['2022-07-29']");
});
it("formats a Date alongside other types inside an array", () => {
expect(
formatQueryParams({
value: [new Date(Date.UTC(2023, 4, 5)), "foo", 42, null],
}),
).toBe("['2023-05-05','foo',42,NULL]");
});
});