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'] },
+ })
+ })
})
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
}
// 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)
}
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) {
// TODO: extract query and hash? warn about presence
return {
path: location.path,
+ // TODO: normalize query?
query: location.query || {},
hash: location.hash || '',
params: {},
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
let search = '?'
for (const key in query) {
+ // TODO: handle arrays
search += `${key}=${query[key]}`
}