From: Eduardo San Martin Morote Date: Mon, 15 Apr 2019 19:31:16 +0000 (+0200) Subject: arrays query X-Git-Tag: v4.0.0-alpha.0~451 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f9b3678289fc76cca7731fabf61adddb2f9a07f4;p=thirdparty%2Fvuejs%2Frouter.git arrays query --- diff --git a/__tests__/url.spec.js b/__tests__/url.spec.js index 6d4fe768..a2305f42 100644 --- a/__tests__/url.spec.js +++ b/__tests__/url.spec.js @@ -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'] }, + }) + }) }) diff --git a/src/history/base.ts b/src/history/base.ts index 7ddb09b3..57aa5f17 100644 --- a/src/history/base.ts +++ b/src/history/base.ts @@ -1,11 +1,13 @@ export type HistoryLocation = string + +export type HistoryQuery = Record export interface HistoryURL { // full path (like href) fullPath: string // pathname section path: string // search string parsed - query: Record // 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) } diff --git a/src/matcher.ts b/src/matcher.ts index 3c53280d..945e63bc 100644 --- a/src/matcher.ts +++ b/src/matcher.ts @@ -42,14 +42,15 @@ export class RouterMatcher { location: Readonly, currentLocation: Readonly ): 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: {}, diff --git a/src/types/index.ts b/src/types/index.ts index 1f3b17f6..effcbe90 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -3,7 +3,7 @@ import { HistoryURL } from '../history/base' type TODO = any export type RouteParams = Record -export type RouteQuery = Record +export type RouteQuery = Record // interface PropsTransformer { // (params: RouteParams): any diff --git a/src/utils/index.ts b/src/utils/index.ts index 99c787c4..58d59e1e 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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]}` }