|
1 | 1 | import { decode, encodeQueryKey, encodeQueryValue, PLUS_RE } from './encoding'
|
2 | 2 |
|
3 | 3 | /**
|
4 |
| - * Possible values in normalized {@link LocationQuery} |
| 4 | + * Possible values in normalized {@link LocationQuery}. `null` renders the query |
| 5 | + * param but without an `=`: `?isNull&isEmpty=&other=other` -> `{ isNull: null, |
| 6 | + * isEmpty: '', other: 'other' }`. |
5 | 7 | *
|
6 | 8 | * @internal
|
7 | 9 | */
|
8 | 10 | export type LocationQueryValue = string | null
|
9 | 11 | /**
|
10 |
| - * Possible values when defining a query |
| 12 | + * Possible values when defining a query. |
11 | 13 | *
|
12 | 14 | * @internal
|
13 | 15 | */
|
@@ -93,20 +95,25 @@ export function stringifyQuery(query: LocationQueryRaw): string {
|
93 | 95 | key = encodeQueryKey(key)
|
94 | 96 | if (value == null) {
|
95 | 97 | // only null adds the value
|
96 |
| - if (value !== undefined) search += (search.length ? '&' : '') + key |
| 98 | + if (value !== undefined) { |
| 99 | + search += (search.length ? '&' : '') + key |
| 100 | + } |
97 | 101 | continue
|
98 | 102 | }
|
99 | 103 | // keep null values
|
100 | 104 | let values: LocationQueryValueRaw[] = Array.isArray(value)
|
101 | 105 | ? value.map(v => v && encodeQueryValue(v))
|
102 | 106 | : [value && encodeQueryValue(value)]
|
103 | 107 |
|
104 |
| - for (let i = 0; i < values.length; i++) { |
105 |
| - if (values[i] === undefined) continue |
106 |
| - // only append & with non-empty search |
107 |
| - search += (search.length ? '&' : '') + key |
108 |
| - if (values[i] !== null) search += ('=' + values[i]) as string |
109 |
| - } |
| 108 | + values.forEach(value => { |
| 109 | + // skip undefined values in arrays as if they were not present |
| 110 | + // smaller code than using filter |
| 111 | + if (value !== undefined) { |
| 112 | + // only append & with non-empty search |
| 113 | + search += (search.length ? '&' : '') + key |
| 114 | + if (value != null) search += '=' + value |
| 115 | + } |
| 116 | + }) |
110 | 117 | }
|
111 | 118 |
|
112 | 119 | return search
|
|
0 commit comments