]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
refactor: move query utils out of history
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 5 Feb 2020 13:34:11 +0000 (14:34 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 5 Feb 2020 13:34:11 +0000 (14:34 +0100)
__tests__/parseQuery.spec.ts
__tests__/query.spec.ts [deleted file]
__tests__/stringifyQuery.spec.ts
__tests__/url.spec.ts
src/history/common.ts
src/router.ts
src/types/index.ts

index f42b92faca14ecc8d7a28a945e1d56577abb6e34..ee017eeb0cbcd4108ad4691138b062ff676ed62a 100644 (file)
@@ -1,8 +1,28 @@
-import { parseQuery } from '../src/history/common'
+import { parseQuery } from '../src/utils/query'
 import { mockWarn } from './mockWarn'
 
 describe('parseQuery', () => {
   mockWarn()
+
+  it('works with leading ?', () => {
+    expect(parseQuery('?foo=a')).toEqual({
+      foo: 'a',
+    })
+  })
+
+  it('works without leading ?', () => {
+    expect(parseQuery('foo=a')).toEqual({
+      foo: 'a',
+    })
+  })
+
+  it('works with an empty string', () => {
+    const emptyQuery = parseQuery('')
+    expect(Object.keys(emptyQuery)).toHaveLength(0)
+    expect(emptyQuery).toEqual({})
+    expect(parseQuery('?')).toEqual({})
+  })
+
   it('decodes values in query', () => {
     expect(parseQuery('e=%25')).toEqual({
       e: '%',
diff --git a/__tests__/query.spec.ts b/__tests__/query.spec.ts
deleted file mode 100644 (file)
index 8c50da2..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-import { parseQuery } from '../src/history/common'
-
-describe('parseQuery', () => {
-  it('works with leading ?', () => {
-    expect(parseQuery('?foo=a')).toEqual({
-      foo: 'a',
-    })
-  })
-
-  it('works without leading ?', () => {
-    expect(parseQuery('foo=a')).toEqual({
-      foo: 'a',
-    })
-  })
-
-  it('works with an empty string', () => {
-    const emptyQuery = parseQuery('')
-    expect(Object.keys(emptyQuery)).toHaveLength(0)
-    expect(emptyQuery).toEqual({})
-    expect(parseQuery('?')).toEqual({})
-  })
-})
index de221c37416c04b99ba3c8728f1b5dc564040e81..61b436becb254d7db8f16a6a06aa7b5967b47303 100644 (file)
@@ -1,4 +1,4 @@
-import { stringifyQuery } from '../src/history/common'
+import { stringifyQuery } from '../src/utils/query'
 import { mockWarn } from './mockWarn'
 
 describe('stringifyQuery', () => {
index ea4fe7b12b3fdbe06a99bc4ed14edf02d328590a..0e3b2ca09a1201e8c89c15c3e384e5949cb1a159 100644 (file)
@@ -1,10 +1,9 @@
 import {
   parseURL as originalParseURL,
   stringifyURL as originalStringifyURL,
-  parseQuery,
-  stringifyQuery,
   normalizeHistoryLocation as normalizeLocation,
 } from '../src/history/common'
+import { parseQuery, stringifyQuery } from '../src/utils/query'
 
 describe('parseURL', () => {
   let parseURL = originalParseURL.bind(null, parseQuery)
index edade69650994fcd443b2dbea2b39e9b3ef5be0a..20226d31407b91b2ae6951ae957c2e67407fe5fb 100644 (file)
@@ -1,17 +1,5 @@
 import { ListenerRemover } from '../types'
-import { decode, encodeQueryProperty } from '../utils/encoding'
-// import { encodeQueryProperty, encodeHash } from '../utils/encoding'
-
-type HistoryQueryValue = string | null
-type RawHistoryQueryValue = HistoryQueryValue | number | undefined
-export type HistoryQuery = Record<
-  string,
-  HistoryQueryValue | HistoryQueryValue[]
->
-export type RawHistoryQuery = Record<
-  string | number,
-  RawHistoryQueryValue | RawHistoryQueryValue[]
->
+import { RawHistoryQuery, HistoryQuery } from '../utils/query'
 
 interface HistoryLocation {
   fullPath: string
@@ -158,93 +146,6 @@ export function stringifyURL(
   return location.path + (query && '?') + query + (location.hash || '')
 }
 
-/**
- * Transform a queryString into a query object. Accept both, a version with the leading `?` and without
- * Should work as URLSearchParams
- * @param search
- * @returns a query object
- */
-export function parseQuery(search: string): HistoryQuery {
-  const query: HistoryQuery = {}
-  // avoid creating an object with an empty key and empty value
-  // because of split('&')
-  if (search === '' || search === '?') return query
-  const hasLeadingIM = search[0] === '?'
-  const searchParams = (hasLeadingIM ? search.slice(1) : search).split('&')
-  for (let i = 0; i < searchParams.length; ++i) {
-    let [key, rawValue] = searchParams[i].split('=') as [
-      string,
-      string | undefined
-    ]
-    key = decode(key)
-    // avoid decoding null
-    let value = rawValue == null ? null : decode(rawValue)
-    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
-}
-/**
- * Stringify an object query. Works like URLSearchParams. Doesn't prepend a `?`
- * @param query
- */
-export function stringifyQuery(query: RawHistoryQuery): string {
-  let search = ''
-  for (let key in query) {
-    if (search.length) search += '&'
-    const value = query[key]
-    key = encodeQueryProperty(key)
-    if (value == null) {
-      // only null adds the value
-      if (value !== undefined) search += key
-      continue
-    }
-    // keep null values
-    let values: RawHistoryQueryValue[] = Array.isArray(value)
-      ? value.map(v => v && encodeQueryProperty(v))
-      : [value && encodeQueryProperty(value)]
-
-    for (let i = 0; i < values.length; i++) {
-      // only append & with i > 0
-      search += (i ? '&' : '') + key
-      if (values[i] != null) search += ('=' + values[i]) as string
-    }
-  }
-
-  return search
-}
-
-/**
- * Transforms a RawQuery intoe a NormalizedQuery by casting numbers into
- * strings, removing keys with an undefined value and replacing undefined with
- * null in arrays
- * @param query
- */
-export function normalizeQuery(query: RawHistoryQuery): HistoryQuery {
-  const normalizedQuery: HistoryQuery = {}
-
-  for (let key in query) {
-    let value = query[key]
-    if (value !== undefined) {
-      normalizedQuery[key] = Array.isArray(value)
-        ? value.map(v => (v == null ? null : '' + v))
-        : value == null
-        ? value
-        : '' + value
-    }
-  }
-
-  return normalizedQuery
-}
-
 /**
  * Strips off the base from the beginning of a location.pathname
  * @param pathname location.pathname
index e5f0cb8e12246bbbd2309b436fcf06160cee74bd..0c1cf8234c695dc6293aedc25736a6825378a4e4 100644 (file)
@@ -11,14 +11,7 @@ import {
   Immutable,
   RouteParams,
 } from './types'
-import {
-  RouterHistory,
-  parseQuery,
-  parseURL,
-  stringifyQuery,
-  stringifyURL,
-  normalizeQuery,
-} from './history/common'
+import { RouterHistory, parseURL, stringifyURL } from './history/common'
 import {
   ScrollToPosition,
   ScrollPosition,
@@ -33,6 +26,7 @@ import {
 import { extractComponentsGuards, guardToPromiseFn } from './utils'
 import { useCallbacks } from './utils/callbacks'
 import { encodeParam, decode } from './utils/encoding'
+import { normalizeQuery, parseQuery, stringifyQuery } from './utils/query'
 import { ref, Ref, markNonReactive, nextTick, App } from 'vue'
 import { RouteRecordMatched } from './matcher/types'
 import Link from './components/Link'
index cbb6c48962806510ac62947f8ce2efa8e216c3b6..c35eb36ec0e473355a23fb69719e0dbb6876e01d 100644 (file)
@@ -1,4 +1,4 @@
-import { HistoryQuery, RawHistoryQuery } from '../history/common'
+import { HistoryQuery, RawHistoryQuery } from '../utils/query'
 import { PathParserOptions } from '../matcher/path-parser-ranker'
 import { markNonReactive } from 'vue'
 import { RouteRecordMatched } from '../matcher/types'