From: Eduardo San Martin Morote Date: Sun, 27 Oct 2019 17:56:56 +0000 (+0100) Subject: refactor(types): stricter RouteRecordNormalized types X-Git-Tag: v4.0.0-alpha.0~174 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cc70fbc35b0357a923969000bb163455e012917e;p=thirdparty%2Fvuejs%2Frouter.git refactor(types): stricter RouteRecordNormalized types --- diff --git a/src/matcher/path-matcher.ts b/src/matcher/path-matcher.ts index 8e2c1d24..86898c67 100644 --- a/src/matcher/path-matcher.ts +++ b/src/matcher/path-matcher.ts @@ -1,64 +1,40 @@ import pathToRegexp from 'path-to-regexp' import { RouteRecord, - RouteRecordRedirect, - RouteRecordMultipleViews, - RouteRecordSingleView, - Mutable, // TODO: add it to matched // MatchedRouteRecord, } from '../types' -import { NormalizedRouteRecord, RouteRecordMatcher } from './types' +import { RouteRecordNormalized, RouteRecordMatcher } from './types' -function copyObject( - a: T, - keys: K[] -): Mutable> { - const copy: Pick = {} as Pick - - for (const key of keys) { - if (!a.hasOwnProperty(key)) continue - if (key in a) copy[key] = a[key] - } - - return copy -} - -const ROUTE_RECORD_REDIRECT_KEYS: (keyof RouteRecordRedirect)[] = [ - 'path', - 'name', - 'beforeEnter', - 'redirect', - 'meta', -] -const ROUTE_RECORD_MULTIPLE_VIEWS_KEYS: (keyof ( - | RouteRecordMultipleViews - | RouteRecordSingleView))[] = [ - 'path', - 'name', - 'beforeEnter', - 'children', - 'meta', -] /** - * Normalizes a RouteRecord into a MatchedRouteRecord. Creates a copy + * Normalizes a RouteRecord into a MatchedRouteRecord. It also ensures removes + * traling slashes Returns a copy * @param record * @returns the normalized version */ export function normalizeRouteRecord( record: Readonly -): NormalizedRouteRecord { - // TODO: could be refactored to improve typings +): RouteRecordNormalized { if ('redirect' in record) { - return copyObject(record, ROUTE_RECORD_REDIRECT_KEYS) + return { + path: record.path, + redirect: record.redirect, + name: record.name, + beforeEnter: record.beforeEnter, + meta: record.meta, + } } else { - const copy: RouteRecordMultipleViews = copyObject( - record, - ROUTE_RECORD_MULTIPLE_VIEWS_KEYS - ) as RouteRecordMultipleViews - copy.components = - 'components' in record ? record.components : { default: record.component } - return copy + return { + path: record.path, + components: + 'components' in record + ? record.components + : { default: record.component }, + children: record.children, + name: record.name, + beforeEnter: record.beforeEnter, + meta: record.meta, + } } } @@ -90,8 +66,8 @@ const enum PathScore { const isDefaultPathRegExpRE = /^\[\^[^\]]+\]\+\?$/ export function createRouteRecordMatcher( - record: Readonly, - parent: RouteRecordMatcher | void, + record: Readonly, + parent: RouteRecordMatcher | undefined, options: pathToRegexp.RegExpOptions ): RouteRecordMatcher { const keys: pathToRegexp.Key[] = [] diff --git a/src/matcher/types.ts b/src/matcher/types.ts index 851d3773..27aa2a28 100644 --- a/src/matcher/types.ts +++ b/src/matcher/types.ts @@ -4,16 +4,31 @@ import { RouteRecordRedirect, } from '../types' +interface RouteRecordRedirectNormalized { + path: RouteRecordRedirect['path'] + name: RouteRecordRedirect['name'] + redirect: RouteRecordRedirect['redirect'] + meta: RouteRecordRedirect['meta'] + beforeEnter: RouteRecordRedirect['beforeEnter'] +} +interface RouteRecordViewNormalized { + path: RouteRecordMultipleViews['path'] + name: RouteRecordMultipleViews['name'] + components: RouteRecordMultipleViews['components'] + children: RouteRecordMultipleViews['children'] + meta: RouteRecordMultipleViews['meta'] + beforeEnter: RouteRecordMultipleViews['beforeEnter'] +} // normalize component/components into components -export type NormalizedRouteRecord = - | Omit - | Omit +export type RouteRecordNormalized = + | RouteRecordRedirectNormalized + | RouteRecordViewNormalized export interface RouteRecordMatcher { re: RegExp resolve: (params?: RouteParams) => string - record: NormalizedRouteRecord - parent: RouteRecordMatcher | void + record: RouteRecordNormalized + parent: RouteRecordMatcher | undefined // TODO: children so they can be removed // children: RouteMatcher[] // TODO: needs information like optional, repeatable