فهرست منبع

Simplify URL path and query parsing methods

Shaun 2 سال پیش
والد
کامیت
0baebe6d54
1فایلهای تغییر یافته به همراه7 افزوده شده و 17 حذف شده
  1. 7 17
      src/url.js

+ 7 - 17
src/url.js

@@ -16,35 +16,25 @@ export class RouterURL {
   }
 
   get path () {
-    return (this.mode === 'hash')
-      ? (
-        this.#url.hash
-        ? this.#url.hash.slice(1).split("?").shift()
-        : this.#url.href.replace(new RegExp('^' + this.#url.origin + this.base), '').split("?").shift()
-      )
+    return (this.mode === 'hash' && this.#url.hash)
+      ? this.#url.hash.slice(1).split("?").shift()
       : this.#url.pathname.replace(this.base, '')
   }
 
   get query () {
     return Object.fromEntries(
       new URLSearchParams(
-        this.mode === 'web' ? this.#url.search : this.#url.hash.split('?').pop()
+        this.#url.href.indexOf('?') > -1 ? this.#url.href.split('?').pop() : ''
       )
     )
   }
 
   resolve (path, params, replace = false) {
-    let [l, r] = this.#url.href.split('?')
-    l = (this.mode === 'hash')
-      ? l.indexOf('#') > -1 ? l.replace(/#.+$/, '#' + path) : this.#url.origin + this.base + '#' + path
-      : l.replace(new RegExp(this.#url.pathname + '$'), this.base + path)
-    const q = replace
+    const l = this.#url.origin + this.base + (this.mode === 'hash' ? '#' : '') + path
+    const r = replace
       ? new URLSearchParams(params).toString()
-      : new URLSearchParams({
-        ...Object.fromEntries(new URLSearchParams(r ?? '').entries()),
-        ...params
-      }).toString()
-    this.url = l + (q ? '?' + q : '')
+      : new URLSearchParams({ ...this.query, ...params }).toString()
+    this.url = l + (r ? '?' + r : '')
     return this
   }
 }