]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
arrays query
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 15 Apr 2019 19:31:16 +0000 (21:31 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 15 Apr 2019 19:31:16 +0000 (21:31 +0200)
__tests__/url.spec.js
src/history/base.ts
src/matcher.ts
src/types/index.ts
src/utils/index.ts

index 6d4fe768fdd132e15293951437220d43d8773890..a2305f42508eca0dfab5d70cc27d05575d99ba4b 100644 (file)
@@ -44,4 +44,13 @@ describe('URL parsing', () => {
       query: { a: 'one' },
     })
   })
+
+  it('extracts multiple query paramenters as an array', () => {
+    expect(parseURL('/foo?a=one&a=two&a=three')).toEqual({
+      fullPath: '/foo?a=one&a=two&a=three',
+      path: '/foo',
+      hash: '',
+      query: { a: ['one', 'two', 'three'] },
+    })
+  })
 })
index 7ddb09b3a0cf168bf62fad1cff59ab13b0d6aeb2..57aa5f17d488aaa386967ff2d4eb046f589fa957 100644 (file)
@@ -1,11 +1,13 @@
 export type HistoryLocation = string
+
+export type HistoryQuery = Record<string, string | string[]>
 export interface HistoryURL {
   // full path (like href)
   fullPath: string
   // pathname section
   path: string
   // search string parsed
-  query: Record<string, string> // TODO: handle arrays
+  query: HistoryQuery
   // hash with the #
   hash: string
 }
@@ -99,7 +101,17 @@ export abstract class BaseHistory {
       // TODO: properly do this in a util function
       query = searchString.split('&').reduce((query, entry) => {
         const [key, value] = entry.split('=')
-        query[key] = value
+        if (key in query) {
+          // an extra variable for ts types
+          let currentValue = query[key]
+          if (!Array.isArray(currentValue)) {
+            currentValue = query[key] = [currentValue]
+          }
+          currentValue.push(value)
+        } else {
+          query[key] = value
+        }
+
         return query
       }, query)
     }
index 3c53280d92244654981473b9405e0158a7983fec..945e63bc509117525c6a8aac3be51082b8f9caa6 100644 (file)
@@ -42,14 +42,15 @@ export class RouterMatcher {
     location: Readonly<MatcherLocation>,
     currentLocation: Readonly<RouterLocationNormalized>
   ): RouterLocationNormalized {
-    if (typeof location === 'string')
+    // TODO: type guard HistoryURL
+    if ('fullPath' in location)
       return {
-        path: location,
-        fullPath: location,
+        path: location.path,
+        fullPath: location.fullPath,
         // TODO: resolve params, query and hash
         params: {},
-        query: {},
-        hash: '',
+        query: location.query,
+        hash: location.hash,
       }
 
     if ('path' in location) {
@@ -57,6 +58,7 @@ export class RouterMatcher {
       // TODO: extract query and hash? warn about presence
       return {
         path: location.path,
+        // TODO: normalize query?
         query: location.query || {},
         hash: location.hash || '',
         params: {},
index 1f3b17f62a758a30ada4d3a9de0643a23a8fb08e..effcbe907411b1e06b240b38dadfe69a66e7a894 100644 (file)
@@ -3,7 +3,7 @@ import { HistoryURL } from '../history/base'
 type TODO = any
 
 export type RouteParams = Record<string, string | string[]>
-export type RouteQuery = Record<string, string | null>
+export type RouteQuery = Record<string, string | string[] | null>
 
 // interface PropsTransformer {
 //   (params: RouteParams): any
index 99c787c40bb70532c1a69e647594541fc8a007b9..58d59e1e73bc4bded57c1f280d6947fdcbf9e6fb 100644 (file)
@@ -5,6 +5,7 @@ export function stringifyQuery(query: RouteQuery | void): string {
 
   let search = '?'
   for (const key in query) {
+    // TODO: handle arrays
     search += `${key}=${query[key]}`
   }