]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: rename href property
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 15 Apr 2019 14:58:24 +0000 (16:58 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 15 Apr 2019 14:58:24 +0000 (16:58 +0200)
__tests__/url.spec.js
src/history/base.ts
src/matcher.ts
src/router.ts
src/types/index.ts

index cbfc60477b99e8ec81e324d7c7a87bbe63a0043b..6d4fe768fdd132e15293951437220d43d8773890 100644 (file)
@@ -8,6 +8,7 @@ const parseURL = BaseHistory.prototype.parseURL
 describe('URL parsing', () => {
   it('works with no query no hash', () => {
     expect(parseURL('/foo')).toEqual({
+      fullPath: '/foo',
       path: '/foo',
       hash: '',
       query: {},
@@ -16,6 +17,7 @@ describe('URL parsing', () => {
 
   it('extracts the query', () => {
     expect(parseURL('/foo?a=one&b=two')).toEqual({
+      fullPath: '/foo?a=one&b=two',
       path: '/foo',
       hash: '',
       query: {
@@ -27,6 +29,7 @@ describe('URL parsing', () => {
 
   it('extracts the hash', () => {
     expect(parseURL('/foo#bar')).toEqual({
+      fullPath: '/foo#bar',
       path: '/foo',
       hash: '#bar',
       query: {},
@@ -35,6 +38,7 @@ describe('URL parsing', () => {
 
   it('extracts query and hash', () => {
     expect(parseURL('/foo?a=one#bar')).toEqual({
+      fullPath: '/foo?a=one#bar',
       path: '/foo',
       hash: '#bar',
       query: { a: 'one' },
index eb0a4e3ac3b394da66760c8c3c93898e2365a1a4..7ddb09b3a0cf168bf62fad1cff59ab13b0d6aeb2 100644 (file)
@@ -1,7 +1,12 @@
 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
 }
 
@@ -107,6 +112,7 @@ export abstract class BaseHistory {
     path = path || location
 
     return {
+      fullPath: location,
       path,
       // TODO: transform searchString
       query,
index 012e3b5ea63f454d396ffcb32f24254af5bd94f2..3c53280d92244654981473b9405e0158a7983fec 100644 (file)
@@ -2,7 +2,7 @@ import pathToRegexp from 'path-to-regexp'
 import {
   RouteRecord,
   RouteParams,
-  RouterLocation,
+  MatcherLocation,
   RouterLocationNormalized,
 } from './types/index'
 import { stringifyQuery } from './utils'
@@ -35,12 +35,11 @@ export class RouterMatcher {
   }
 
   /**
-   * 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')
index 50c78bf5b81253dffdade97bfe76441f133c416e..9afbd670995a1e696dab62c66f7b55a7b5383211 100644 (file)
@@ -25,7 +25,8 @@ export class Router {
 
     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)
     })
   }
 
@@ -35,7 +36,8 @@ export class Router {
    */
   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
index e29cac3a340801056702de04ea4e3fe7e466833d..1f3b17f62a758a30ada4d3a9de0643a23a8fb08e 100644 (file)
@@ -1,3 +1,5 @@
+import { HistoryURL } from '../history/base'
+
 type TODO = any
 
 export type RouteParams = Record<string, string | string[]>
@@ -24,9 +26,7 @@ export interface RouteRecord {
   // props: PT
 }
 
-// TODO: location should be an object
-export type RouterLocation =
-  | string
+type RouteObjectLocation =
   | {
       path: string
       query?: RouteQuery
@@ -44,12 +44,17 @@ export type RouterLocation =
       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
 }