-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiSort.ts
More file actions
336 lines (290 loc) · 9.49 KB
/
Copy pathMultiSort.ts
File metadata and controls
336 lines (290 loc) · 9.49 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* **MultiSort** - Multiple sort fields with multiple serialization formats:
*
* 1. **Stacked params** (repeated query params)
* `GET /users?sortBy=status&sortBy=createdAt&sortOrder=asc&sortOrder=desc`
* Pros: Clear semantics, order is preserved
* Cons: Params must match in length, slightly verbose
*
* 2. **Array-like format** (comma-separated or other delimiters)
* `GET /users?sortBy=status,createdAt&sortOrder=asc,desc`
* Pros: Compact, easier to read single query string
* Cons: Requires agreed-upon delimiter, order still implicit
*
* 3. **Compact/Pair format** (field:direction pairs)
* `GET /users?sort=status:asc,createdAt:desc`
* Pros: Most compact, self-contained
* Cons: Requires parsing field:direction
*
* 4. **Record/Object formats** (field => direction mapping)
* Multiple serialization options:
* - Compact pairs: `?sort=status:asc,createdAt:desc`
* - Stacked pairs: `?sort=status:asc&sort=createdAt:desc`
* - Bracket notation: `?sort[status]=asc&sort[createdAt]=desc`
* - `qs` library: passing object directly, library handles serialization
* Pros: Intuitive object structure, libraries handle encoding
* Cons: More complex parsing on backend
*/
export enum SortDirection {
ASC = "asc",
DESC = "desc",
}
export interface SortField {
field: string // e.g., "status", "user.createdAt", "meta.rating"
direction: SortDirection
}
/**
* Record type: field => direction mapping
* e.g., { "status": "asc", "user.createdAt": "desc" }
*/
export type SortRecord = Record<string, SortDirection | string>
/**
* Stacked params approach - multiple `sortBy` and `sortOrder` params
*/
export function buildStackedSort(sorts: SortField[]): URLSearchParams {
const params = new URLSearchParams()
for (const sort of sorts) {
params.append("sortBy", sort.field)
params.append("sortOrder", sort.direction)
}
return params
}
// Example:
// buildStackedSort([
// { field: "status", direction: SortDirection.ASC },
// { field: "createdAt", direction: SortDirection.DESC },
// ])
// => "sortBy=status&sortOrder=asc&sortBy=createdAt&sortOrder=desc"
/**
* Array-like format - comma-separated fields and orders
*/
export function buildArraySort(sorts: SortField[], delimiter = ","): URLSearchParams {
const params = new URLSearchParams()
if (sorts.length > 0) {
params.set("sortBy", sorts.map((s) => s.field).join(delimiter))
params.set("sortOrder", sorts.map((s) => s.direction).join(delimiter))
}
return params
}
// Example:
// buildArraySort([
// { field: "status", direction: SortDirection.ASC },
// { field: "createdAt", direction: SortDirection.DESC },
// ])
// => "sortBy=status,createdAt&sortOrder=asc,desc"
/**
* Compact format - single param with field:direction pairs
* `GET /users?sort=status:asc,createdAt:desc`
*/
export function buildCompactSort(sorts: SortField[]): URLSearchParams {
const params = new URLSearchParams()
if (sorts.length > 0) {
params.set(
"sort",
sorts.map((s) => `${s.field}:${s.direction}`).join(","),
)
}
return params
}
// Example:
// buildCompactSort([
// { field: "status", direction: SortDirection.ASC },
// { field: "createdAt", direction: SortDirection.DESC },
// ])
// => "sort=status:asc,createdAt:desc"
/**
* Parsing back from query string (stacked format)
*/
export function parseStackedSort(params: URLSearchParams): SortField[] {
const sortBys = params.getAll("sortBy")
const sortOrders = params.getAll("sortOrder") as SortDirection[]
return sortBys.map((field, idx) => ({
field,
direction: sortOrders[idx] || SortDirection.ASC,
}))
}
/**
* Parsing back from query string (array/compact format)
*/
export function parseArraySort(params: URLSearchParams, delimiter = ","): SortField[] {
const sortBy = params.get("sortBy")?.split(delimiter) || []
const sortOrder = params.get("sortOrder")?.split(delimiter) as SortDirection[] || []
return sortBy.map((field, idx) => ({
field,
direction: sortOrder[idx] || SortDirection.ASC,
}))
}
export function parseCompactSort(params: URLSearchParams): SortField[] {
const sort = params.get("sort") || ""
return sort
.split(",")
.filter(Boolean)
.map((pair) => {
const [field, direction] = pair.split(":")
return {
field: field || "",
direction: (direction as SortDirection) || SortDirection.ASC,
}
})
}
/**
* **Record-based sorting** - Field => Direction mapping
*
* Instead of arrays of {field, direction}, use a plain object:
* { "status": "asc", "user.createdAt": "desc" }
*
* This is more intuitive and can be serialized multiple ways.
*/
/**
* Convert SortField[] to SortRecord
*/
export function fieldsToRecord(sorts: SortField[]): SortRecord {
const record: SortRecord = {}
for (const sort of sorts) {
record[sort.field] = sort.direction
}
return record
}
/**
* Convert SortRecord to SortField[]
* Preserves iteration order (modern JS objects maintain insertion order)
*/
export function recordToFields(record: SortRecord): SortField[] {
return Object.entries(record).map(([field, direction]) => ({
field,
direction: direction as SortDirection,
}))
}
/**
* Option 1: Compact pairs format
* `?sort=status:asc,user.createdAt:desc`
*
* Converts record to comma-separated field:direction pairs
*/
export function buildRecordCompactSort(record: SortRecord): URLSearchParams {
const params = new URLSearchParams()
if (Object.keys(record).length > 0) {
params.set(
"sort",
Object.entries(record)
.map(([field, direction]) => `${field}:${direction}`)
.join(","),
)
}
return params
}
// Example:
// buildRecordCompactSort({ "status": "asc", "createdAt": "desc" })
// => "sort=status:asc,createdAt:desc"
/**
* Option 2: Stacked pairs format
* `?sort=status:asc&sort=user.createdAt:desc`
*
* Repeated params, each containing one field:direction pair
*/
export function buildRecordStackedSort(record: SortRecord): URLSearchParams {
const params = new URLSearchParams()
for (const [field, direction] of Object.entries(record)) {
params.append("sort", `${field}:${direction}`)
}
return params
}
// Example:
// buildRecordStackedSort({ "status": "asc", "createdAt": "desc" })
// => "sort=status:asc&sort=createdAt:desc"
/**
* Option 3: Bracket notation format
* `?sort[status]=asc&sort[user.createdAt]=desc`
*
* Uses array-like bracket notation, where the field name is the key
* Note: URLSearchParams doesn't natively support this, manual string building required
*/
export function buildRecordBracketSort(record: SortRecord): string {
return Object.entries(record)
.map(
([field, direction]) =>
`sort[${encodeURIComponent(field)}]=${encodeURIComponent(direction as string)}`,
)
.join("&")
}
// Example:
// buildRecordBracketSort({ "status": "asc", "user.createdAt": "desc" })
// => "sort%5Bstatus%5D=asc&sort%5Buser.createdAt%5D=desc"
// (decoded: "sort[status]=asc&sort[user.createdAt]=desc")
/**
* Option 4: Using `qs` library
*
* The `qs` library (commonly used in Express, UmiJS, etc.) can serialize objects
* directly using various formats. Pass the record and let the library handle encoding:
*
* import qs from "qs"
*
* qs.stringify({ sort: record }, { arrayFormat: "repeat" })
* // => "sort=status%3Aasc&sort=createdAt%3Adesc" (stacked format)
*
* qs.stringify({ sort: record }, { arrayFormat: "brackets" })
* // => "sort%5Bstatus%5D=asc&sort%5BcreatedAt%5D=desc" (bracket notation)
*
* qs.stringify({ sort: record }, { arrayFormat: "indices" })
* // => "sort%5B0%5D%5Bfield%5D=status&sort%5B0%5D%5Bdirection%5D=asc" (nested)
*
* qs.stringify({ sort: record }, { arrayFormat: "comma" })
* // => "sort=status%2Casc%2CcreatedAt%2Cdesc" (comma-separated)
*
* The `qs` library handles all encoding/decoding, making it ideal for complex structures.
* This is the recommended approach if your project already uses `qs`.
*/
/**
* Parse compact pairs: `sort=status:asc,createdAt:desc`
*/
export function parseRecordCompactSort(params: URLSearchParams): SortRecord {
const sort = params.get("sort") || ""
const record: SortRecord = {}
sort
.split(",")
.filter(Boolean)
.forEach((pair) => {
const [field, direction] = pair.split(":")
if (field) {
record[field] = (direction as SortDirection) || SortDirection.ASC
}
})
return record
}
/**
* Parse stacked pairs: `sort=status:asc&sort=createdAt:desc`
*/
export function parseRecordStackedSort(params: URLSearchParams): SortRecord {
const sorts = params.getAll("sort")
const record: SortRecord = {}
for (const pair of sorts) {
const [field, direction] = pair.split(":")
if (field) {
record[field] = (direction as SortDirection) || SortDirection.ASC
}
}
return record
}
/**
* Parse bracket notation: `sort[status]=asc&sort[createdAt]=desc`
* Returns the record directly since bracket notation already uses field names as keys
*/
export function parseRecordBracketSort(
params: URLSearchParams | Record<string, any>,
): SortRecord {
// If params is already an object (from `qs.parse`), use it directly
if (!(params instanceof URLSearchParams)) {
return params as SortRecord
}
// Manual parsing for URLSearchParams with bracket notation
// This requires iterating through keys matching `sort[*]`
const record: SortRecord = {}
for (const key of params.keys()) {
const match = key.match(/^sort\[(.+)\]$/)
if (match) {
const field = match[1]
record[field] = params.get(key) as SortDirection
}
}
return record
}