describe('URL parsing', () => {
it('works with no query no hash', () => {
expect(parseURL('/foo')).toEqual({
+ fullPath: '/foo',
path: '/foo',
hash: '',
query: {},
it('extracts the query', () => {
expect(parseURL('/foo?a=one&b=two')).toEqual({
+ fullPath: '/foo?a=one&b=two',
path: '/foo',
hash: '',
query: {
it('extracts the hash', () => {
expect(parseURL('/foo#bar')).toEqual({
+ fullPath: '/foo#bar',
path: '/foo',
hash: '#bar',
query: {},
it('extracts query and hash', () => {
expect(parseURL('/foo?a=one#bar')).toEqual({
+ fullPath: '/foo?a=one#bar',
path: '/foo',
hash: '#bar',
query: { a: 'one' },
export type HistoryLocation = string
export interface HistoryURL {
+ // full path (like href)
+ fullPath: string
+ // pathname section
path: string
+ // search string parsed
query: Record<string, string> // TODO: handle arrays
+ // hash with the #
hash: string
}
path = path || location
return {
+ fullPath: location,
path,
// TODO: transform searchString
query,
import {
RouteRecord,
RouteParams,
- RouterLocation,
+ MatcherLocation,
RouterLocationNormalized,
} from './types/index'
import { stringifyQuery } from './utils'
}
/**
- * Transforms a RouterLocation object into a URL string. If a string is
- * passed, it returns the string itself
- * @param location RouterLocation to resolve to a url
+ * Transforms a MatcherLocation object into a normalized location
+ * @param location MatcherLocation to resolve to a url
*/
resolve(
- location: Readonly<RouterLocation>,
+ location: Readonly<MatcherLocation>,
currentLocation: Readonly<RouterLocationNormalized>
): RouterLocationNormalized {
if (typeof location === 'string')
this.history.listen((to, from, info) => {
// TODO: check navigation guards
- this.currentRoute = this.matcher.resolve(to, this.currentRoute)
+ const url = this.history.parseURL(to)
+ this.currentRoute = this.matcher.resolve(url, this.currentRoute)
})
}
*/
push(to: RouterLocation) {
// TODO: resolve URL
- const location = this.matcher.resolve(to, this.currentRoute)
+ const url = typeof to === 'string' ? this.history.parseURL(to) : to
+ const location = this.matcher.resolve(url, this.currentRoute)
// TODO: call hooks, guards
this.history.push(location.fullPath)
this.currentRoute = location
+import { HistoryURL } from '../history/base'
+
type TODO = any
export type RouteParams = Record<string, string | string[]>
// props: PT
}
-// TODO: location should be an object
-export type RouterLocation =
- | string
+type RouteObjectLocation =
| {
path: string
query?: RouteQuery
hash?: string
}
+// TODO: location should be an object
+export type MatcherLocation = HistoryURL | RouteObjectLocation
+
+export type RouterLocation = string | RouteObjectLocation
+
export interface RouterLocationNormalized {
path: string
fullPath: string
name?: string
params: RouteParams
- query: TODO
+ query: RouteQuery
hash: string
}