use-prms 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +320 -0
- package/dist/hash.cjs +505 -0
- package/dist/hash.cjs.map +1 -0
- package/dist/hash.d.cts +1 -0
- package/dist/hash.d.ts +1 -0
- package/dist/hash.js +475 -0
- package/dist/hash.js.map +1 -0
- package/dist/index.cjs +502 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +341 -0
- package/dist/index.d.ts +341 -0
- package/dist/index.js +472 -0
- package/dist/index.js.map +1 -0
- package/package.json +60 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core multi-value operations and location strategies
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Multi-value encoded representation
|
|
6
|
+
* An array of strings representing multiple values for a single URL parameter key
|
|
7
|
+
*/
|
|
8
|
+
type MultiEncoded = string[];
|
|
9
|
+
/**
|
|
10
|
+
* Location strategy interface for abstracting URL storage location
|
|
11
|
+
* (query string vs hash fragment)
|
|
12
|
+
*/
|
|
13
|
+
interface LocationStrategy {
|
|
14
|
+
/** Get raw string from location (for caching comparison) */
|
|
15
|
+
getRaw(): string;
|
|
16
|
+
/** Parse current location to multi-value params */
|
|
17
|
+
parse(): Record<string, MultiEncoded>;
|
|
18
|
+
/** Build URL string with updated params */
|
|
19
|
+
buildUrl(base: URL, params: Record<string, MultiEncoded>): string;
|
|
20
|
+
/** Subscribe to location changes, returns unsubscribe function */
|
|
21
|
+
subscribe(callback: () => void): () => void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parse URL string to multi-value params
|
|
25
|
+
* Each key maps to an array of all values for that key
|
|
26
|
+
*/
|
|
27
|
+
declare function parseMultiParams(source: string | URLSearchParams): Record<string, MultiEncoded>;
|
|
28
|
+
/**
|
|
29
|
+
* Serialize multi-value params to URL string format
|
|
30
|
+
* Repeated keys are serialized as separate entries: key=a&key=b
|
|
31
|
+
*/
|
|
32
|
+
declare function serializeMultiParams(params: Record<string, MultiEncoded>): string;
|
|
33
|
+
/**
|
|
34
|
+
* Query string location strategy
|
|
35
|
+
* Reads/writes to window.location.search
|
|
36
|
+
*/
|
|
37
|
+
declare const queryStrategy: LocationStrategy;
|
|
38
|
+
/**
|
|
39
|
+
* Hash fragment location strategy
|
|
40
|
+
* Reads/writes to window.location.hash
|
|
41
|
+
* Hash is parsed as URLSearchParams format: #key=value&key2=value2
|
|
42
|
+
*/
|
|
43
|
+
declare const hashStrategy: LocationStrategy;
|
|
44
|
+
/**
|
|
45
|
+
* Get the current default location strategy
|
|
46
|
+
*/
|
|
47
|
+
declare function getDefaultStrategy(): LocationStrategy;
|
|
48
|
+
/**
|
|
49
|
+
* Set the default location strategy
|
|
50
|
+
* Called by entry points (e.g., hash.ts sets this to hashStrategy)
|
|
51
|
+
*/
|
|
52
|
+
declare function setDefaultStrategy(strategy: LocationStrategy): void;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Built-in parameter types with smart defaults and minimal encoding
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Optional string parameter.
|
|
60
|
+
* - undefined → not present
|
|
61
|
+
* - empty string → ?key=
|
|
62
|
+
* - non-empty → ?key=value
|
|
63
|
+
*/
|
|
64
|
+
declare function stringParam(init?: string): Param<string | undefined>;
|
|
65
|
+
/**
|
|
66
|
+
* Required string parameter with default.
|
|
67
|
+
* Omitted from URL when equal to default.
|
|
68
|
+
*/
|
|
69
|
+
declare function defStringParam(init: string): Param<string>;
|
|
70
|
+
/**
|
|
71
|
+
* Boolean parameter.
|
|
72
|
+
* - true → ?key (valueless)
|
|
73
|
+
* - false → not present
|
|
74
|
+
*/
|
|
75
|
+
declare const boolParam: Param<boolean>;
|
|
76
|
+
/**
|
|
77
|
+
* Integer parameter with default.
|
|
78
|
+
* Omitted from URL when equal to default.
|
|
79
|
+
*/
|
|
80
|
+
declare function intParam(init: number): Param<number>;
|
|
81
|
+
/**
|
|
82
|
+
* Optional integer parameter.
|
|
83
|
+
* - null → not present
|
|
84
|
+
* - number → ?key=123
|
|
85
|
+
*/
|
|
86
|
+
declare const optIntParam: Param<number | null>;
|
|
87
|
+
/**
|
|
88
|
+
* Float parameter with default.
|
|
89
|
+
* Omitted from URL when equal to default.
|
|
90
|
+
*/
|
|
91
|
+
declare function floatParam(init: number): Param<number>;
|
|
92
|
+
/**
|
|
93
|
+
* Enum parameter with validation.
|
|
94
|
+
* Omitted from URL when equal to default.
|
|
95
|
+
* Invalid values fall back to default with console warning.
|
|
96
|
+
*/
|
|
97
|
+
declare function enumParam<T extends string>(init: T, values: readonly T[]): Param<T>;
|
|
98
|
+
/**
|
|
99
|
+
* String array parameter with delimiter.
|
|
100
|
+
* Omitted from URL when equal to default.
|
|
101
|
+
* Empty array encodes as empty string (?key=)
|
|
102
|
+
*/
|
|
103
|
+
declare function stringsParam(init?: string[], delimiter?: string): Param<string[]>;
|
|
104
|
+
/**
|
|
105
|
+
* Number array parameter.
|
|
106
|
+
* Omitted from URL when equal to default.
|
|
107
|
+
* Uses comma delimiter.
|
|
108
|
+
*/
|
|
109
|
+
declare function numberArrayParam(init?: number[]): Param<number[]>;
|
|
110
|
+
/**
|
|
111
|
+
* Pagination parameter combining offset and page size.
|
|
112
|
+
* Uses space (which encodes as + in URLs) as delimiter.
|
|
113
|
+
*
|
|
114
|
+
* Encoding rules:
|
|
115
|
+
* - offset=0, pageSize=default → not present (undefined)
|
|
116
|
+
* - offset=0, pageSize=custom → " pageSize" (e.g., " 20" → +20 in URL)
|
|
117
|
+
* - offset>0, pageSize=default → "offset" (e.g., "100")
|
|
118
|
+
* - offset>0, pageSize=custom → "offset pageSize" (e.g., "100 20" → 100+20 in URL)
|
|
119
|
+
*
|
|
120
|
+
* @param defaultPageSize - The default page size (omitted from URL when used)
|
|
121
|
+
* @param validPageSizes - Optional array of valid page sizes for validation
|
|
122
|
+
*/
|
|
123
|
+
type Pagination = {
|
|
124
|
+
offset: number;
|
|
125
|
+
pageSize: number;
|
|
126
|
+
};
|
|
127
|
+
declare function paginationParam(defaultPageSize: number, validPageSizes?: readonly number[]): Param<Pagination>;
|
|
128
|
+
/**
|
|
129
|
+
* Code mapping for enum values - maps full values to short codes for compact URLs.
|
|
130
|
+
* Can be specified as:
|
|
131
|
+
* - Array of [value, code] tuples: [['Rides', 'r'], ['Minutes', 'm']]
|
|
132
|
+
* - Object mapping values to codes: { Rides: 'r', Minutes: 'm' }
|
|
133
|
+
*/
|
|
134
|
+
type CodeMap<T extends string> = [T, string][] | Record<T, string>;
|
|
135
|
+
/**
|
|
136
|
+
* Single-value enum parameter with short code mapping.
|
|
137
|
+
* Maps full enum values to abbreviated codes for compact URLs.
|
|
138
|
+
* Omitted from URL when equal to default.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* // ?y=r for "Rides", ?y=m for "Minutes", omitted for default "Rides"
|
|
142
|
+
* codeParam('Rides', [['Rides', 'r'], ['Minutes', 'm']])
|
|
143
|
+
* // or with object syntax:
|
|
144
|
+
* codeParam('Rides', { Rides: 'r', Minutes: 'm' })
|
|
145
|
+
*/
|
|
146
|
+
declare function codeParam<T extends string>(init: T, codeMap: CodeMap<T>): Param<T>;
|
|
147
|
+
/**
|
|
148
|
+
* Multi-value parameter with short code mapping.
|
|
149
|
+
* Maps full values to abbreviated codes for compact URLs.
|
|
150
|
+
* Omitted from URL when all values are selected.
|
|
151
|
+
*
|
|
152
|
+
* @param allValues - Array of all possible values (used to detect "all selected")
|
|
153
|
+
* @param codeMap - Mapping from values to short codes
|
|
154
|
+
* @param separator - Delimiter between codes (default: '' for most compact URLs)
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* // Regions: ?r=nj for NYC+JC, ?r=njh or omitted for all three
|
|
158
|
+
* codesParam(['NYC', 'JC', 'HOB'], [['NYC', 'n'], ['JC', 'j'], ['HOB', 'h']])
|
|
159
|
+
* // or with object syntax and custom separator:
|
|
160
|
+
* codesParam(['NYC', 'JC', 'HOB'], { NYC: 'n', JC: 'j', HOB: 'h' }, ',')
|
|
161
|
+
*/
|
|
162
|
+
declare function codesParam<T extends string>(allValues: readonly T[], codeMap: CodeMap<T>, separator?: string): Param<T[]>;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Multi-value parameter types for handling repeated URL params
|
|
166
|
+
* e.g., ?tag=a&tag=b&tag=c
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* A bidirectional converter between a typed value and its multi-value URL representation.
|
|
171
|
+
* Similar to Param<T> but works with string[] instead of string | undefined.
|
|
172
|
+
*/
|
|
173
|
+
type MultiParam<T> = {
|
|
174
|
+
encode: (value: T) => MultiEncoded;
|
|
175
|
+
decode: (encoded: MultiEncoded) => T;
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* Multi-value string array parameter.
|
|
179
|
+
* Each string becomes a separate URL param with the same key.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* // ?tag=a&tag=b&tag=c → ['a', 'b', 'c']
|
|
183
|
+
* const [tags, setTags] = useMultiUrlParam('tag', multiStringParam())
|
|
184
|
+
*/
|
|
185
|
+
declare function multiStringParam(init?: string[]): MultiParam<string[]>;
|
|
186
|
+
/**
|
|
187
|
+
* Multi-value integer array parameter.
|
|
188
|
+
* Each number becomes a separate URL param with the same key.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* // ?id=1&id=2&id=3 → [1, 2, 3]
|
|
192
|
+
* const [ids, setIds] = useMultiUrlParam('id', multiIntParam())
|
|
193
|
+
*/
|
|
194
|
+
declare function multiIntParam(init?: number[]): MultiParam<number[]>;
|
|
195
|
+
/**
|
|
196
|
+
* Multi-value float array parameter.
|
|
197
|
+
* Each number becomes a separate URL param with the same key.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* // ?val=1.5&val=2.7 → [1.5, 2.7]
|
|
201
|
+
* const [vals, setVals] = useMultiUrlParam('val', multiFloatParam())
|
|
202
|
+
*/
|
|
203
|
+
declare function multiFloatParam(init?: number[]): MultiParam<number[]>;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* React hooks for managing URL parameters
|
|
207
|
+
*/
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* React hook for managing a single URL query parameter.
|
|
211
|
+
*
|
|
212
|
+
* @param key - Query parameter key
|
|
213
|
+
* @param param - Param encoder/decoder
|
|
214
|
+
* @param push - Use pushState (true) or replaceState (false) when updating
|
|
215
|
+
* @returns Tuple of [value, setValue]
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```tsx
|
|
219
|
+
* const [zoom, setZoom] = useUrlParam('z', boolParam)
|
|
220
|
+
* const [device, setDevice] = useUrlParam('d', stringParam('default'))
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
declare function useUrlParam<T>(key: string, param: Param<T>, push?: boolean): [T, (value: T) => void];
|
|
224
|
+
/**
|
|
225
|
+
* React hook for managing multiple URL query parameters together.
|
|
226
|
+
* Updates are batched into a single history entry.
|
|
227
|
+
*
|
|
228
|
+
* @param params - Object mapping keys to Param types
|
|
229
|
+
* @param push - Use pushState (true) or replaceState (false) when updating
|
|
230
|
+
* @returns Object with decoded values and update function
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```tsx
|
|
234
|
+
* const { values, setValues } = useUrlParams({
|
|
235
|
+
* zoom: boolParam,
|
|
236
|
+
* device: stringParam('default'),
|
|
237
|
+
* count: intParam(10)
|
|
238
|
+
* })
|
|
239
|
+
*
|
|
240
|
+
* // Update multiple params at once
|
|
241
|
+
* setValues({ zoom: true, count: 20 })
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
declare function useUrlParams<P extends Record<string, Param<any>>>(params: P, push?: boolean): {
|
|
245
|
+
values: {
|
|
246
|
+
[K in keyof P]: P[K] extends Param<infer T> ? T : never;
|
|
247
|
+
};
|
|
248
|
+
setValues: (updates: Partial<{
|
|
249
|
+
[K in keyof P]: P[K] extends Param<infer T> ? T : never;
|
|
250
|
+
}>) => void;
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* React hook for managing a single multi-value URL parameter.
|
|
254
|
+
* Supports repeated params like ?tag=a&tag=b&tag=c
|
|
255
|
+
*
|
|
256
|
+
* @param key - Query parameter key
|
|
257
|
+
* @param param - MultiParam encoder/decoder
|
|
258
|
+
* @param push - Use pushState (true) or replaceState (false) when updating
|
|
259
|
+
* @returns Tuple of [value, setValue]
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```tsx
|
|
263
|
+
* const [tags, setTags] = useMultiUrlParam('tag', multiStringParam())
|
|
264
|
+
* // URL: ?tag=a&tag=b → tags = ['a', 'b']
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
declare function useMultiUrlParam<T>(key: string, param: MultiParam<T>, push?: boolean): [T, (value: T) => void];
|
|
268
|
+
/**
|
|
269
|
+
* React hook for managing multiple multi-value URL parameters together.
|
|
270
|
+
* Updates are batched into a single history entry.
|
|
271
|
+
*
|
|
272
|
+
* @param params - Object mapping keys to MultiParam types
|
|
273
|
+
* @param push - Use pushState (true) or replaceState (false) when updating
|
|
274
|
+
* @returns Object with decoded values and update function
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```tsx
|
|
278
|
+
* const { values, setValues } = useMultiUrlParams({
|
|
279
|
+
* tags: multiStringParam(),
|
|
280
|
+
* ids: multiIntParam()
|
|
281
|
+
* })
|
|
282
|
+
*
|
|
283
|
+
* // Update multiple multi-value params at once
|
|
284
|
+
* setValues({ tags: ['a', 'b'], ids: [1, 2, 3] })
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
declare function useMultiUrlParams<P extends Record<string, MultiParam<any>>>(params: P, push?: boolean): {
|
|
288
|
+
values: {
|
|
289
|
+
[K in keyof P]: P[K] extends MultiParam<infer T> ? T : never;
|
|
290
|
+
};
|
|
291
|
+
setValues: (updates: Partial<{
|
|
292
|
+
[K in keyof P]: P[K] extends MultiParam<infer T> ? T : never;
|
|
293
|
+
}>) => void;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Core types and utilities for URL parameter management
|
|
298
|
+
*/
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Encodes a value to a URL query parameter string.
|
|
302
|
+
* - undefined: parameter not present in URL
|
|
303
|
+
* - "": valueless parameter (e.g., ?z)
|
|
304
|
+
* - string: parameter with value (e.g., ?z=foo)
|
|
305
|
+
*/
|
|
306
|
+
type Encoded = string | undefined;
|
|
307
|
+
/**
|
|
308
|
+
* A bidirectional converter between a typed value and its URL representation.
|
|
309
|
+
*/
|
|
310
|
+
type Param<T> = {
|
|
311
|
+
encode: (value: T) => Encoded;
|
|
312
|
+
decode: (encoded: Encoded) => T;
|
|
313
|
+
};
|
|
314
|
+
/**
|
|
315
|
+
* Serialize query parameters to URL string.
|
|
316
|
+
* Uses URLSearchParams for proper form-urlencoded format (space → +)
|
|
317
|
+
* Handles valueless params (empty string → ?key without =) manually
|
|
318
|
+
*
|
|
319
|
+
* @deprecated For multi-value support, use serializeMultiParams instead
|
|
320
|
+
*/
|
|
321
|
+
declare function serializeParams(params: Record<string, Encoded>): string;
|
|
322
|
+
/**
|
|
323
|
+
* Parse query parameters from URL string or URLSearchParams.
|
|
324
|
+
* Note: URLSearchParams treats ?z and ?z= identically (both as empty string).
|
|
325
|
+
* Note: For repeated params, only the first value is returned.
|
|
326
|
+
*
|
|
327
|
+
* @deprecated For multi-value support, use parseMultiParams instead
|
|
328
|
+
*/
|
|
329
|
+
declare function parseParams(source: string | URLSearchParams): Record<string, Encoded>;
|
|
330
|
+
/**
|
|
331
|
+
* Get current URL query parameters (browser only)
|
|
332
|
+
*/
|
|
333
|
+
declare function getCurrentParams(): Record<string, Encoded>;
|
|
334
|
+
/**
|
|
335
|
+
* Update URL without reloading (browser only)
|
|
336
|
+
* @param params - New query parameters
|
|
337
|
+
* @param push - Use pushState (true) or replaceState (false)
|
|
338
|
+
*/
|
|
339
|
+
declare function updateUrl(params: Record<string, Encoded>, push?: boolean): void;
|
|
340
|
+
|
|
341
|
+
export { type CodeMap, type Encoded, type LocationStrategy, type MultiEncoded, type MultiParam, type Pagination, type Param, boolParam, codeParam, codesParam, defStringParam, enumParam, floatParam, getCurrentParams, getDefaultStrategy, hashStrategy, intParam, multiFloatParam, multiIntParam, multiStringParam, numberArrayParam, optIntParam, paginationParam, parseMultiParams, parseParams, queryStrategy, serializeMultiParams, serializeParams, setDefaultStrategy, stringParam, stringsParam, updateUrl, useMultiUrlParam, useMultiUrlParams, useUrlParam, useUrlParams };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core multi-value operations and location strategies
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Multi-value encoded representation
|
|
6
|
+
* An array of strings representing multiple values for a single URL parameter key
|
|
7
|
+
*/
|
|
8
|
+
type MultiEncoded = string[];
|
|
9
|
+
/**
|
|
10
|
+
* Location strategy interface for abstracting URL storage location
|
|
11
|
+
* (query string vs hash fragment)
|
|
12
|
+
*/
|
|
13
|
+
interface LocationStrategy {
|
|
14
|
+
/** Get raw string from location (for caching comparison) */
|
|
15
|
+
getRaw(): string;
|
|
16
|
+
/** Parse current location to multi-value params */
|
|
17
|
+
parse(): Record<string, MultiEncoded>;
|
|
18
|
+
/** Build URL string with updated params */
|
|
19
|
+
buildUrl(base: URL, params: Record<string, MultiEncoded>): string;
|
|
20
|
+
/** Subscribe to location changes, returns unsubscribe function */
|
|
21
|
+
subscribe(callback: () => void): () => void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parse URL string to multi-value params
|
|
25
|
+
* Each key maps to an array of all values for that key
|
|
26
|
+
*/
|
|
27
|
+
declare function parseMultiParams(source: string | URLSearchParams): Record<string, MultiEncoded>;
|
|
28
|
+
/**
|
|
29
|
+
* Serialize multi-value params to URL string format
|
|
30
|
+
* Repeated keys are serialized as separate entries: key=a&key=b
|
|
31
|
+
*/
|
|
32
|
+
declare function serializeMultiParams(params: Record<string, MultiEncoded>): string;
|
|
33
|
+
/**
|
|
34
|
+
* Query string location strategy
|
|
35
|
+
* Reads/writes to window.location.search
|
|
36
|
+
*/
|
|
37
|
+
declare const queryStrategy: LocationStrategy;
|
|
38
|
+
/**
|
|
39
|
+
* Hash fragment location strategy
|
|
40
|
+
* Reads/writes to window.location.hash
|
|
41
|
+
* Hash is parsed as URLSearchParams format: #key=value&key2=value2
|
|
42
|
+
*/
|
|
43
|
+
declare const hashStrategy: LocationStrategy;
|
|
44
|
+
/**
|
|
45
|
+
* Get the current default location strategy
|
|
46
|
+
*/
|
|
47
|
+
declare function getDefaultStrategy(): LocationStrategy;
|
|
48
|
+
/**
|
|
49
|
+
* Set the default location strategy
|
|
50
|
+
* Called by entry points (e.g., hash.ts sets this to hashStrategy)
|
|
51
|
+
*/
|
|
52
|
+
declare function setDefaultStrategy(strategy: LocationStrategy): void;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Built-in parameter types with smart defaults and minimal encoding
|
|
56
|
+
*/
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Optional string parameter.
|
|
60
|
+
* - undefined → not present
|
|
61
|
+
* - empty string → ?key=
|
|
62
|
+
* - non-empty → ?key=value
|
|
63
|
+
*/
|
|
64
|
+
declare function stringParam(init?: string): Param<string | undefined>;
|
|
65
|
+
/**
|
|
66
|
+
* Required string parameter with default.
|
|
67
|
+
* Omitted from URL when equal to default.
|
|
68
|
+
*/
|
|
69
|
+
declare function defStringParam(init: string): Param<string>;
|
|
70
|
+
/**
|
|
71
|
+
* Boolean parameter.
|
|
72
|
+
* - true → ?key (valueless)
|
|
73
|
+
* - false → not present
|
|
74
|
+
*/
|
|
75
|
+
declare const boolParam: Param<boolean>;
|
|
76
|
+
/**
|
|
77
|
+
* Integer parameter with default.
|
|
78
|
+
* Omitted from URL when equal to default.
|
|
79
|
+
*/
|
|
80
|
+
declare function intParam(init: number): Param<number>;
|
|
81
|
+
/**
|
|
82
|
+
* Optional integer parameter.
|
|
83
|
+
* - null → not present
|
|
84
|
+
* - number → ?key=123
|
|
85
|
+
*/
|
|
86
|
+
declare const optIntParam: Param<number | null>;
|
|
87
|
+
/**
|
|
88
|
+
* Float parameter with default.
|
|
89
|
+
* Omitted from URL when equal to default.
|
|
90
|
+
*/
|
|
91
|
+
declare function floatParam(init: number): Param<number>;
|
|
92
|
+
/**
|
|
93
|
+
* Enum parameter with validation.
|
|
94
|
+
* Omitted from URL when equal to default.
|
|
95
|
+
* Invalid values fall back to default with console warning.
|
|
96
|
+
*/
|
|
97
|
+
declare function enumParam<T extends string>(init: T, values: readonly T[]): Param<T>;
|
|
98
|
+
/**
|
|
99
|
+
* String array parameter with delimiter.
|
|
100
|
+
* Omitted from URL when equal to default.
|
|
101
|
+
* Empty array encodes as empty string (?key=)
|
|
102
|
+
*/
|
|
103
|
+
declare function stringsParam(init?: string[], delimiter?: string): Param<string[]>;
|
|
104
|
+
/**
|
|
105
|
+
* Number array parameter.
|
|
106
|
+
* Omitted from URL when equal to default.
|
|
107
|
+
* Uses comma delimiter.
|
|
108
|
+
*/
|
|
109
|
+
declare function numberArrayParam(init?: number[]): Param<number[]>;
|
|
110
|
+
/**
|
|
111
|
+
* Pagination parameter combining offset and page size.
|
|
112
|
+
* Uses space (which encodes as + in URLs) as delimiter.
|
|
113
|
+
*
|
|
114
|
+
* Encoding rules:
|
|
115
|
+
* - offset=0, pageSize=default → not present (undefined)
|
|
116
|
+
* - offset=0, pageSize=custom → " pageSize" (e.g., " 20" → +20 in URL)
|
|
117
|
+
* - offset>0, pageSize=default → "offset" (e.g., "100")
|
|
118
|
+
* - offset>0, pageSize=custom → "offset pageSize" (e.g., "100 20" → 100+20 in URL)
|
|
119
|
+
*
|
|
120
|
+
* @param defaultPageSize - The default page size (omitted from URL when used)
|
|
121
|
+
* @param validPageSizes - Optional array of valid page sizes for validation
|
|
122
|
+
*/
|
|
123
|
+
type Pagination = {
|
|
124
|
+
offset: number;
|
|
125
|
+
pageSize: number;
|
|
126
|
+
};
|
|
127
|
+
declare function paginationParam(defaultPageSize: number, validPageSizes?: readonly number[]): Param<Pagination>;
|
|
128
|
+
/**
|
|
129
|
+
* Code mapping for enum values - maps full values to short codes for compact URLs.
|
|
130
|
+
* Can be specified as:
|
|
131
|
+
* - Array of [value, code] tuples: [['Rides', 'r'], ['Minutes', 'm']]
|
|
132
|
+
* - Object mapping values to codes: { Rides: 'r', Minutes: 'm' }
|
|
133
|
+
*/
|
|
134
|
+
type CodeMap<T extends string> = [T, string][] | Record<T, string>;
|
|
135
|
+
/**
|
|
136
|
+
* Single-value enum parameter with short code mapping.
|
|
137
|
+
* Maps full enum values to abbreviated codes for compact URLs.
|
|
138
|
+
* Omitted from URL when equal to default.
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* // ?y=r for "Rides", ?y=m for "Minutes", omitted for default "Rides"
|
|
142
|
+
* codeParam('Rides', [['Rides', 'r'], ['Minutes', 'm']])
|
|
143
|
+
* // or with object syntax:
|
|
144
|
+
* codeParam('Rides', { Rides: 'r', Minutes: 'm' })
|
|
145
|
+
*/
|
|
146
|
+
declare function codeParam<T extends string>(init: T, codeMap: CodeMap<T>): Param<T>;
|
|
147
|
+
/**
|
|
148
|
+
* Multi-value parameter with short code mapping.
|
|
149
|
+
* Maps full values to abbreviated codes for compact URLs.
|
|
150
|
+
* Omitted from URL when all values are selected.
|
|
151
|
+
*
|
|
152
|
+
* @param allValues - Array of all possible values (used to detect "all selected")
|
|
153
|
+
* @param codeMap - Mapping from values to short codes
|
|
154
|
+
* @param separator - Delimiter between codes (default: '' for most compact URLs)
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* // Regions: ?r=nj for NYC+JC, ?r=njh or omitted for all three
|
|
158
|
+
* codesParam(['NYC', 'JC', 'HOB'], [['NYC', 'n'], ['JC', 'j'], ['HOB', 'h']])
|
|
159
|
+
* // or with object syntax and custom separator:
|
|
160
|
+
* codesParam(['NYC', 'JC', 'HOB'], { NYC: 'n', JC: 'j', HOB: 'h' }, ',')
|
|
161
|
+
*/
|
|
162
|
+
declare function codesParam<T extends string>(allValues: readonly T[], codeMap: CodeMap<T>, separator?: string): Param<T[]>;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Multi-value parameter types for handling repeated URL params
|
|
166
|
+
* e.g., ?tag=a&tag=b&tag=c
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* A bidirectional converter between a typed value and its multi-value URL representation.
|
|
171
|
+
* Similar to Param<T> but works with string[] instead of string | undefined.
|
|
172
|
+
*/
|
|
173
|
+
type MultiParam<T> = {
|
|
174
|
+
encode: (value: T) => MultiEncoded;
|
|
175
|
+
decode: (encoded: MultiEncoded) => T;
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* Multi-value string array parameter.
|
|
179
|
+
* Each string becomes a separate URL param with the same key.
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* // ?tag=a&tag=b&tag=c → ['a', 'b', 'c']
|
|
183
|
+
* const [tags, setTags] = useMultiUrlParam('tag', multiStringParam())
|
|
184
|
+
*/
|
|
185
|
+
declare function multiStringParam(init?: string[]): MultiParam<string[]>;
|
|
186
|
+
/**
|
|
187
|
+
* Multi-value integer array parameter.
|
|
188
|
+
* Each number becomes a separate URL param with the same key.
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* // ?id=1&id=2&id=3 → [1, 2, 3]
|
|
192
|
+
* const [ids, setIds] = useMultiUrlParam('id', multiIntParam())
|
|
193
|
+
*/
|
|
194
|
+
declare function multiIntParam(init?: number[]): MultiParam<number[]>;
|
|
195
|
+
/**
|
|
196
|
+
* Multi-value float array parameter.
|
|
197
|
+
* Each number becomes a separate URL param with the same key.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* // ?val=1.5&val=2.7 → [1.5, 2.7]
|
|
201
|
+
* const [vals, setVals] = useMultiUrlParam('val', multiFloatParam())
|
|
202
|
+
*/
|
|
203
|
+
declare function multiFloatParam(init?: number[]): MultiParam<number[]>;
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* React hooks for managing URL parameters
|
|
207
|
+
*/
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* React hook for managing a single URL query parameter.
|
|
211
|
+
*
|
|
212
|
+
* @param key - Query parameter key
|
|
213
|
+
* @param param - Param encoder/decoder
|
|
214
|
+
* @param push - Use pushState (true) or replaceState (false) when updating
|
|
215
|
+
* @returns Tuple of [value, setValue]
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```tsx
|
|
219
|
+
* const [zoom, setZoom] = useUrlParam('z', boolParam)
|
|
220
|
+
* const [device, setDevice] = useUrlParam('d', stringParam('default'))
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
declare function useUrlParam<T>(key: string, param: Param<T>, push?: boolean): [T, (value: T) => void];
|
|
224
|
+
/**
|
|
225
|
+
* React hook for managing multiple URL query parameters together.
|
|
226
|
+
* Updates are batched into a single history entry.
|
|
227
|
+
*
|
|
228
|
+
* @param params - Object mapping keys to Param types
|
|
229
|
+
* @param push - Use pushState (true) or replaceState (false) when updating
|
|
230
|
+
* @returns Object with decoded values and update function
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* ```tsx
|
|
234
|
+
* const { values, setValues } = useUrlParams({
|
|
235
|
+
* zoom: boolParam,
|
|
236
|
+
* device: stringParam('default'),
|
|
237
|
+
* count: intParam(10)
|
|
238
|
+
* })
|
|
239
|
+
*
|
|
240
|
+
* // Update multiple params at once
|
|
241
|
+
* setValues({ zoom: true, count: 20 })
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
declare function useUrlParams<P extends Record<string, Param<any>>>(params: P, push?: boolean): {
|
|
245
|
+
values: {
|
|
246
|
+
[K in keyof P]: P[K] extends Param<infer T> ? T : never;
|
|
247
|
+
};
|
|
248
|
+
setValues: (updates: Partial<{
|
|
249
|
+
[K in keyof P]: P[K] extends Param<infer T> ? T : never;
|
|
250
|
+
}>) => void;
|
|
251
|
+
};
|
|
252
|
+
/**
|
|
253
|
+
* React hook for managing a single multi-value URL parameter.
|
|
254
|
+
* Supports repeated params like ?tag=a&tag=b&tag=c
|
|
255
|
+
*
|
|
256
|
+
* @param key - Query parameter key
|
|
257
|
+
* @param param - MultiParam encoder/decoder
|
|
258
|
+
* @param push - Use pushState (true) or replaceState (false) when updating
|
|
259
|
+
* @returns Tuple of [value, setValue]
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```tsx
|
|
263
|
+
* const [tags, setTags] = useMultiUrlParam('tag', multiStringParam())
|
|
264
|
+
* // URL: ?tag=a&tag=b → tags = ['a', 'b']
|
|
265
|
+
* ```
|
|
266
|
+
*/
|
|
267
|
+
declare function useMultiUrlParam<T>(key: string, param: MultiParam<T>, push?: boolean): [T, (value: T) => void];
|
|
268
|
+
/**
|
|
269
|
+
* React hook for managing multiple multi-value URL parameters together.
|
|
270
|
+
* Updates are batched into a single history entry.
|
|
271
|
+
*
|
|
272
|
+
* @param params - Object mapping keys to MultiParam types
|
|
273
|
+
* @param push - Use pushState (true) or replaceState (false) when updating
|
|
274
|
+
* @returns Object with decoded values and update function
|
|
275
|
+
*
|
|
276
|
+
* @example
|
|
277
|
+
* ```tsx
|
|
278
|
+
* const { values, setValues } = useMultiUrlParams({
|
|
279
|
+
* tags: multiStringParam(),
|
|
280
|
+
* ids: multiIntParam()
|
|
281
|
+
* })
|
|
282
|
+
*
|
|
283
|
+
* // Update multiple multi-value params at once
|
|
284
|
+
* setValues({ tags: ['a', 'b'], ids: [1, 2, 3] })
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
287
|
+
declare function useMultiUrlParams<P extends Record<string, MultiParam<any>>>(params: P, push?: boolean): {
|
|
288
|
+
values: {
|
|
289
|
+
[K in keyof P]: P[K] extends MultiParam<infer T> ? T : never;
|
|
290
|
+
};
|
|
291
|
+
setValues: (updates: Partial<{
|
|
292
|
+
[K in keyof P]: P[K] extends MultiParam<infer T> ? T : never;
|
|
293
|
+
}>) => void;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Core types and utilities for URL parameter management
|
|
298
|
+
*/
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Encodes a value to a URL query parameter string.
|
|
302
|
+
* - undefined: parameter not present in URL
|
|
303
|
+
* - "": valueless parameter (e.g., ?z)
|
|
304
|
+
* - string: parameter with value (e.g., ?z=foo)
|
|
305
|
+
*/
|
|
306
|
+
type Encoded = string | undefined;
|
|
307
|
+
/**
|
|
308
|
+
* A bidirectional converter between a typed value and its URL representation.
|
|
309
|
+
*/
|
|
310
|
+
type Param<T> = {
|
|
311
|
+
encode: (value: T) => Encoded;
|
|
312
|
+
decode: (encoded: Encoded) => T;
|
|
313
|
+
};
|
|
314
|
+
/**
|
|
315
|
+
* Serialize query parameters to URL string.
|
|
316
|
+
* Uses URLSearchParams for proper form-urlencoded format (space → +)
|
|
317
|
+
* Handles valueless params (empty string → ?key without =) manually
|
|
318
|
+
*
|
|
319
|
+
* @deprecated For multi-value support, use serializeMultiParams instead
|
|
320
|
+
*/
|
|
321
|
+
declare function serializeParams(params: Record<string, Encoded>): string;
|
|
322
|
+
/**
|
|
323
|
+
* Parse query parameters from URL string or URLSearchParams.
|
|
324
|
+
* Note: URLSearchParams treats ?z and ?z= identically (both as empty string).
|
|
325
|
+
* Note: For repeated params, only the first value is returned.
|
|
326
|
+
*
|
|
327
|
+
* @deprecated For multi-value support, use parseMultiParams instead
|
|
328
|
+
*/
|
|
329
|
+
declare function parseParams(source: string | URLSearchParams): Record<string, Encoded>;
|
|
330
|
+
/**
|
|
331
|
+
* Get current URL query parameters (browser only)
|
|
332
|
+
*/
|
|
333
|
+
declare function getCurrentParams(): Record<string, Encoded>;
|
|
334
|
+
/**
|
|
335
|
+
* Update URL without reloading (browser only)
|
|
336
|
+
* @param params - New query parameters
|
|
337
|
+
* @param push - Use pushState (true) or replaceState (false)
|
|
338
|
+
*/
|
|
339
|
+
declare function updateUrl(params: Record<string, Encoded>, push?: boolean): void;
|
|
340
|
+
|
|
341
|
+
export { type CodeMap, type Encoded, type LocationStrategy, type MultiEncoded, type MultiParam, type Pagination, type Param, boolParam, codeParam, codesParam, defStringParam, enumParam, floatParam, getCurrentParams, getDefaultStrategy, hashStrategy, intParam, multiFloatParam, multiIntParam, multiStringParam, numberArrayParam, optIntParam, paginationParam, parseMultiParams, parseParams, queryStrategy, serializeMultiParams, serializeParams, setDefaultStrategy, stringParam, stringsParam, updateUrl, useMultiUrlParam, useMultiUrlParams, useUrlParam, useUrlParams };
|