From: Eduardo San Martin Morote Date: Wed, 12 Feb 2020 11:10:57 +0000 (+0100) Subject: refactor: rename historyquery X-Git-Tag: v4.0.0-alpha.0~24 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=fc38eecf084149aab5e496b8f42dd480c38ac55c;p=thirdparty%2Fvuejs%2Frouter.git refactor: rename historyquery --- diff --git a/src/history/common.ts b/src/history/common.ts index 20226d31..f8eab7f0 100644 --- a/src/history/common.ts +++ b/src/history/common.ts @@ -1,5 +1,5 @@ import { ListenerRemover } from '../types' -import { RawHistoryQuery, HistoryQuery } from '../utils/query' +import { LocationQueryRaw, LocationQuery } from '../utils/query' interface HistoryLocation { fullPath: string @@ -10,14 +10,14 @@ export type RawHistoryLocation = HistoryLocation | string export type HistoryLocationNormalized = Pick export interface LocationPartial { path: string - query?: RawHistoryQuery + query?: LocationQueryRaw hash?: string } export interface LocationNormalized { path: string fullPath: string hash: string - query: HistoryQuery + query: LocationQuery } // pushState clones the state passed and do not accept everything @@ -94,11 +94,11 @@ export interface RouterHistory { * @returns a normalized history location */ export function parseURL( - parseQuery: (search: string) => HistoryQuery, + parseQuery: (search: string) => LocationQuery, location: string ): LocationNormalized { let path = '', - query: HistoryQuery = {}, + query: LocationQuery = {}, searchString = '', hash = '' @@ -139,7 +139,7 @@ export function parseURL( * @param location */ export function stringifyURL( - stringifyQuery: (query: RawHistoryQuery) => string, + stringifyQuery: (query: LocationQueryRaw) => string, location: LocationPartial ): string { let query: string = location.query ? stringifyQuery(location.query) : '' diff --git a/src/types/index.ts b/src/types/index.ts index 93ec05fb..040f76ee 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,4 +1,4 @@ -import { HistoryQuery, RawHistoryQuery } from '../utils/query' +import { LocationQuery, LocationQueryRaw } from '../utils/query' import { PathParserOptions } from '../matcher/path-parser-ranker' import { markNonReactive } from 'vue' import { RouteRecordNormalized } from '../matcher/types' @@ -20,7 +20,7 @@ export type ListenerRemover = () => void export type RouteParams = Record export interface RouteQueryAndHash { - query?: RawHistoryQuery + query?: LocationQueryRaw hash?: string } export interface LocationAsPath { @@ -53,7 +53,7 @@ export interface RouteLocationNormalized extends Required { fullPath: string // the normalized version cannot have numbers or undefined - query: HistoryQuery + query: LocationQuery // TODO: do the same for params name: string | null | undefined matched: RouteRecordNormalized[] // non-enumerable diff --git a/src/utils/query.ts b/src/utils/query.ts index 0ceb9c87..0830707e 100644 --- a/src/utils/query.ts +++ b/src/utils/query.ts @@ -1,14 +1,14 @@ import { decode, encodeQueryProperty } from '../utils/encoding' -type HistoryQueryValue = string | null -type RawHistoryQueryValue = HistoryQueryValue | number | undefined -export type HistoryQuery = Record< +type LocationQueryValue = string | null +type LocationQueryValueRaw = LocationQueryValue | number | undefined +export type LocationQuery = Record< string, - HistoryQueryValue | HistoryQueryValue[] + LocationQueryValue | LocationQueryValue[] > -export type RawHistoryQuery = Record< +export type LocationQueryRaw = Record< string | number, - RawHistoryQueryValue | RawHistoryQueryValue[] + LocationQueryValueRaw | LocationQueryValueRaw[] > /** @@ -17,8 +17,8 @@ export type RawHistoryQuery = Record< * @param search * @returns a query object */ -export function parseQuery(search: string): HistoryQuery { - const query: HistoryQuery = {} +export function parseQuery(search: string): LocationQuery { + const query: LocationQuery = {} // avoid creating an object with an empty key and empty value // because of split('&') if (search === '' || search === '?') return query @@ -50,7 +50,7 @@ export function parseQuery(search: string): HistoryQuery { * Stringify an object query. Works like URLSearchParams. Doesn't prepend a `?` * @param query */ -export function stringifyQuery(query: RawHistoryQuery): string { +export function stringifyQuery(query: LocationQueryRaw): string { let search = '' for (let key in query) { if (search.length) search += '&' @@ -62,7 +62,7 @@ export function stringifyQuery(query: RawHistoryQuery): string { continue } // keep null values - let values: RawHistoryQueryValue[] = Array.isArray(value) + let values: LocationQueryValueRaw[] = Array.isArray(value) ? value.map(v => v && encodeQueryProperty(v)) : [value && encodeQueryProperty(value)] @@ -82,8 +82,8 @@ export function stringifyQuery(query: RawHistoryQuery): string { * null in arrays * @param query */ -export function normalizeQuery(query: RawHistoryQuery): HistoryQuery { - const normalizedQuery: HistoryQuery = {} +export function normalizeQuery(query: LocationQueryRaw): LocationQuery { + const normalizedQuery: LocationQuery = {} for (let key in query) { let value = query[key]