]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(query): filter undefined values in arrays
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 10 May 2021 12:13:52 +0000 (14:13 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 10 May 2021 12:13:52 +0000 (14:13 +0200)
Same behavior as with Vue Router 3

__tests__/stringifyQuery.spec.ts
src/query.ts

index fc6604eb508b984410da854ad00991fd794b0f2d..64e774ee648e2775d7dc99dc9d4fd04a4f62f355 100644 (file)
@@ -28,10 +28,17 @@ describe('stringifyQuery', () => {
     expect(stringifyQuery({ e: undefined, b: 'a' })).toEqual('b=a')
   })
 
-  it('ignores undefined and empty arrays', () => {
-    expect(
-      stringifyQuery({ a: [undefined, 'b'], b: undefined, c: [] })
-    ).toEqual('a=b')
+  it('avoids trailing &', () => {
+    expect(stringifyQuery({ a: 'a', b: undefined })).toEqual('a=a')
+    expect(stringifyQuery({ a: 'a', c: [] })).toEqual('a=a')
+  })
+
+  it('skips undefined in arrays', () => {
+    expect(stringifyQuery({ a: [undefined, '3'] })).toEqual('a=3')
+    expect(stringifyQuery({ a: [1, undefined, '3'] })).toEqual('a=1&a=3')
+    expect(stringifyQuery({ a: [1, undefined, '3', undefined] })).toEqual(
+      'a=1&a=3'
+    )
   })
 
   it('stringifies arrays', () => {
index b32ba105d991c0a4f6ef38ce9d9bc8271ffa5e7f..db951c75e50f4e0e2288c0258f76ce945a8f74e5 100644 (file)
@@ -1,13 +1,15 @@
 import { decode, encodeQueryKey, encodeQueryValue, PLUS_RE } from './encoding'
 
 /**
- * Possible values in normalized {@link LocationQuery}
+ * Possible values in normalized {@link LocationQuery}. `null` renders the query
+ * param but without an `=`: `?isNull&isEmpty=&other=other` -> `{ isNull: null,
+ * isEmpty: '', other: 'other' }`.
  *
  * @internal
  */
 export type LocationQueryValue = string | null
 /**
- * Possible values when defining a query
+ * Possible values when defining a query.
  *
  * @internal
  */
@@ -93,7 +95,9 @@ export function stringifyQuery(query: LocationQueryRaw): string {
     key = encodeQueryKey(key)
     if (value == null) {
       // only null adds the value
-      if (value !== undefined) search += (search.length ? '&' : '') + key
+      if (value !== undefined) {
+        search += (search.length ? '&' : '') + key
+      }
       continue
     }
     // keep null values
@@ -101,12 +105,15 @@ export function stringifyQuery(query: LocationQueryRaw): string {
       ? value.map(v => v && encodeQueryValue(v))
       : [value && encodeQueryValue(value)]
 
-    for (let i = 0; i < values.length; i++) {
-      if (values[i] === undefined) continue
-      // only append & with non-empty search
-      search += (search.length ? '&' : '') + key
-      if (values[i] !== null) search += ('=' + values[i]) as string
-    }
+    values.forEach(value => {
+      // skip undefined values in arrays as if they were not present
+      // smaller code than using filter
+      if (value !== undefined) {
+        // only append & with non-empty search
+        search += (search.length ? '&' : '') + key
+        if (value != null) search += '=' + value
+      }
+    })
   }
 
   return search