Skip to content

Commit 6a52334

Browse files
mingXtaOrbisKantfu
authored
feat(useUrlSearchParams): Add a stringify option for users to provide stringify logic (#4773)
Co-authored-by: Robin <robin.kehl@singular-it.de> Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 118c28d commit 6a52334

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

packages/core/useUrlSearchParams/index.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,22 @@ params.foo = 'bar'
4747
params.vueuse = 'awesome'
4848
// url updated to `/your/route#foo=bar&vueuse=awesome`
4949
```
50+
51+
### Custom Stringify Function
52+
53+
You can provide a custom function to serialize URL parameters using the `stringify` option. This is useful when you need special formatting for your query string.
54+
55+
```js
56+
import { useUrlSearchParams } from '@vueuse/core'
57+
58+
// Custom stringify function that removes equal signs for empty values
59+
const params = useUrlSearchParams('history', {
60+
stringify: (params) => {
61+
return params.toString().replace(/=(&|$)/g, '$1')
62+
}
63+
})
64+
65+
params.foo = ''
66+
params.bar = 'value'
67+
// url updated to `?foo&bar=value` instead of `?foo=&bar=value`
68+
```

packages/core/useUrlSearchParams/index.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,24 @@ describe('useUrlSearchParams', () => {
227227
await nextTick()
228228
expect(params).toEqual({ foo: null, bar: false })
229229
})
230+
231+
it('strips equal sign for empty params use customer stringify function', async () => {
232+
const params = useUrlSearchParams(mode, { stringify: params => params.toString().replace(/=(&|$)/g, '$1') })
233+
params.foo = ''
234+
params.bar = ''
235+
await nextTick()
236+
switch (mode) {
237+
case 'history':
238+
expect(window.history.replaceState).toBeCalledWith(null, '', '/?foo&bar')
239+
break
240+
case 'hash':
241+
expect(window.history.replaceState).toBeCalledWith(null, '', '/#?foo&bar')
242+
break
243+
case 'hash-params':
244+
expect(window.history.replaceState).toBeCalledWith(null, '', '/#foo&bar')
245+
break
246+
}
247+
})
230248
})
231249
})
232250

packages/core/useUrlSearchParams/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ export interface UseUrlSearchParamsOptions<T> extends ConfigurableWindow {
3636
* @default 'replace'
3737
*/
3838
writeMode?: 'replace' | 'push'
39+
40+
/**
41+
* Custom function to serialize URL parameters
42+
* When provided, this function will be used instead of the default URLSearchParams.toString()
43+
* @param params The URLSearchParams object to serialize
44+
* @returns The serialized query string (should not include the leading '?' or '#')
45+
*/
46+
stringify?: (params: URLSearchParams) => string
3947
}
4048

4149
/**
@@ -56,6 +64,7 @@ export function useUrlSearchParams<T extends Record<string, any> = UrlParams>(
5664
write: enableWrite = true,
5765
writeMode = 'replace',
5866
window = defaultWindow!,
67+
stringify = params => params.toString(),
5968
} = options
6069

6170
if (!window)
@@ -78,8 +87,7 @@ export function useUrlSearchParams<T extends Record<string, any> = UrlParams>(
7887
}
7988

8089
function constructQuery(params: URLSearchParams) {
81-
const stringified = params.toString()
82-
90+
const stringified = stringify(params)
8391
if (mode === 'history')
8492
return `${stringified ? `?${stringified}` : ''}${window.location.hash || ''}`
8593
if (mode === 'hash-params')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy