boring-router
Advanced tools
Comparing version 0.1.0-alpha.8 to 0.1.0-alpha.9
import { Dict } from 'tslang'; | ||
export declare type GeneralFragmentDict = Dict<string>; | ||
export declare type GeneralFragmentDict = Dict<string | undefined>; | ||
export declare type GeneralQueryDict = Dict<string | undefined>; | ||
@@ -15,3 +15,3 @@ export declare type GeneralParamDict = Dict<string | undefined>; | ||
private _exact; | ||
private _fragments; | ||
private _pathFragments; | ||
private _sourceQuery; | ||
@@ -23,4 +23,6 @@ private _params; | ||
readonly $params: TParamDict; | ||
$path(params?: Partial<TParamDict>, preserveQuery?: boolean): string; | ||
$ref(params?: Partial<TParamDict>, preserveQuery?: boolean): string; | ||
private _match; | ||
static fragment: RegExp; | ||
static rest: RegExp; | ||
} |
@@ -11,17 +11,6 @@ "use strict"; | ||
this._exact = false; | ||
if (match instanceof RegExp) { | ||
if (match.global) { | ||
throw new Error('Expecting a non-global regular expression as match pattern'); | ||
} | ||
this._matchPattern = match; | ||
if (match instanceof RegExp && match.global) { | ||
throw new Error('Expecting a non-global regular expression as match pattern'); | ||
} | ||
else if (match === '*') { | ||
this._matchPattern = /[^/]+/; | ||
} | ||
else if (match === '**') { | ||
this._matchPattern = /.+/; | ||
} | ||
else { | ||
this._matchPattern = match; | ||
} | ||
this._matchPattern = match; | ||
if (query) { | ||
@@ -40,4 +29,4 @@ this._queryKeys = Object.keys(query); | ||
} | ||
$path(params = {}, preserveQuery = false) { | ||
let fragmentDict = this._fragments; | ||
$ref(params = {}, preserveQuery = false) { | ||
let fragmentDict = this._pathFragments; | ||
let paramKeySet = new Set(Object.keys(params)); | ||
@@ -48,15 +37,7 @@ let path = Object.keys(fragmentDict) | ||
let param = params[key]; | ||
let fragment = fragmentDict[key]; | ||
if (typeof fragment === 'string') { | ||
if (typeof param === 'string') { | ||
throw new Error(`Parameter "${key}" cannot override fragment "${fragment}"`); | ||
} | ||
return `/${fragment}`; | ||
let fragment = typeof param === 'string' ? param : fragmentDict[key]; | ||
if (typeof fragment !== 'string') { | ||
throw new Error(`Parameter "${key}" is required`); | ||
} | ||
else { | ||
if (typeof param !== 'string') { | ||
throw new Error(`Parameter "${key}" is required`); | ||
} | ||
return `/${param}`; | ||
} | ||
return `/${fragment}`; | ||
}) | ||
@@ -74,3 +55,3 @@ .join(''); | ||
/** @internal */ | ||
_push(skipped, upperRest, upperFragmentDict, sourceQueryDict) { | ||
_push(skipped, upperRest, upperPathFragmentDict, upperParamFragmentDict, sourceQueryDict) { | ||
let { current, rest } = this._match(skipped, upperRest); | ||
@@ -81,8 +62,5 @@ let name = this._name; | ||
let matchPattern = this._matchPattern; | ||
let fragmentDict = Object.assign({}, upperFragmentDict, { [name]: matched | ||
? current | ||
: typeof matchPattern === 'string' | ||
? matchPattern | ||
: undefined }); | ||
this._fragments = fragmentDict; | ||
let pathFragmentDict = Object.assign({}, upperPathFragmentDict, { [name]: typeof matchPattern === 'string' ? matchPattern : current }); | ||
let paramFragmentDict = Object.assign({}, upperParamFragmentDict, (typeof matchPattern === 'string' ? undefined : { [name]: current })); | ||
this._pathFragments = pathFragmentDict; | ||
let queryKeys = this._queryKeys; | ||
@@ -101,3 +79,3 @@ let queryDict = queryKeys | ||
this._sourceQuery = sourceQueryDict; | ||
this._params = Object.assign({}, fragmentDict, queryDict); | ||
this._params = Object.assign({}, paramFragmentDict, queryDict); | ||
this._matched = matched; | ||
@@ -108,3 +86,4 @@ this._exact = exact; | ||
rest, | ||
fragmentDict, | ||
pathFragmentDict, | ||
paramFragmentDict, | ||
}; | ||
@@ -159,2 +138,4 @@ } | ||
} | ||
RouteMatch.fragment = /[^/]+/; | ||
RouteMatch.rest = /.+/; | ||
tslib_1.__decorate([ | ||
@@ -161,0 +142,0 @@ mobx_1.observable |
import { History } from 'history'; | ||
import { Dict } from 'tslang'; | ||
import { RouteMatch } from './route-match'; | ||
export declare type FragmentMatcherCallback = (key: string) => string | RegExp; | ||
declare type RouteQuerySchemaType<T> = T extends { | ||
$query: infer TSchema; | ||
} ? TSchema : never; | ||
export declare type FragmentMatcherCallback = (key: string) => string; | ||
declare type RouteQuerySchemaType<TRouteSchema> = TRouteSchema extends { | ||
$query: infer TQuerySchema; | ||
} ? TQuerySchema : never; | ||
declare type FilterRouteMatchNonStringFragment<TRouteSchema, T> = TRouteSchema extends { | ||
$match: infer TMatch; | ||
} ? TMatch extends string ? never : T : never; | ||
interface RouteSchemaChildrenPartial<TRouteSchemaDict> { | ||
@@ -17,10 +20,9 @@ $children: TRouteSchemaDict; | ||
export declare type RouteSchemaDict = Dict<RouteSchema | boolean>; | ||
export declare type RouteMatchFragmentType<TRouteSchemaDict, TFragmentKey extends string> = { | ||
[K in Extract<keyof TRouteSchemaDict, string>]: RouteMatchType<TRouteSchemaDict[K], TFragmentKey | FilterRouteMatchNonStringFragment<TRouteSchemaDict[K], K>>; | ||
}; | ||
export declare type RouteMatchType<TRouteSchema, TFragmentKey extends string> = RouteMatch<Record<Extract<keyof RouteQuerySchemaType<TRouteSchema>, string>, string | undefined> & { | ||
[K in TFragmentKey]: string; | ||
}> & (TRouteSchema extends RouteSchemaChildrenPartial<infer TNestedRouteSchemaDict> ? { | ||
[K in Extract<keyof TNestedRouteSchemaDict, string>]: RouteMatchType<TNestedRouteSchemaDict[K], TFragmentKey | K>; | ||
} : {}); | ||
export declare type RootRouterType<TRouteSchemaDict> = Router & { | ||
[K in Extract<keyof TRouteSchemaDict, string>]: RouteMatchType<TRouteSchemaDict[K], K>; | ||
}; | ||
}> & (TRouteSchema extends RouteSchemaChildrenPartial<infer TNestedRouteSchemaDict> ? RouteMatchFragmentType<TNestedRouteSchemaDict, TFragmentKey> : {}); | ||
export declare type RouterType<TRouteSchemaDict> = Router & RouteMatchFragmentType<TRouteSchemaDict, never>; | ||
export interface RouterOptions { | ||
@@ -35,4 +37,4 @@ fragmentMatcher?: FragmentMatcherCallback; | ||
private buildRouteMatches; | ||
static create<TSchema extends RouteSchemaDict>(schema: TSchema, history: History, options?: RouterOptions): RootRouterType<TSchema>; | ||
static create<TSchema extends RouteSchemaDict>(schema: TSchema, history: History, options?: RouterOptions): RouterType<TSchema>; | ||
} | ||
export {}; |
@@ -15,3 +15,3 @@ "use strict"; | ||
}, {}); | ||
this.pushRouteChange(this, false, pathname, {}, queryDict); | ||
this.pushRouteChange(this, false, pathname, {}, {}, queryDict); | ||
}; | ||
@@ -24,3 +24,3 @@ this._fragmentMatcher = | ||
} | ||
pushRouteChange(target, skipped, upperRest, upperFragmentDict, sourceQueryDict) { | ||
pushRouteChange(target, skipped, upperRest, upperPathFragmentDict, upperParamFragmentDict, sourceQueryDict) { | ||
if (!target._children) { | ||
@@ -30,7 +30,7 @@ return; | ||
for (let routeMatch of target._children) { | ||
let { matched, rest, fragmentDict } = routeMatch._push(skipped, upperRest, upperFragmentDict, sourceQueryDict); | ||
let { matched, rest, pathFragmentDict, paramFragmentDict, } = routeMatch._push(skipped, upperRest, upperPathFragmentDict, upperParamFragmentDict, sourceQueryDict); | ||
if (matched) { | ||
skipped = true; | ||
} | ||
this.pushRouteChange(routeMatch, !matched, rest, fragmentDict, sourceQueryDict); | ||
this.pushRouteChange(routeMatch, !matched, rest, pathFragmentDict, paramFragmentDict, sourceQueryDict); | ||
} | ||
@@ -37,0 +37,0 @@ } |
{ | ||
"name": "boring-router", | ||
"version": "0.1.0-alpha.8", | ||
"version": "0.1.0-alpha.9", | ||
"description": "A light-weight, type-safe, yet reactive router service using MobX.", | ||
@@ -10,4 +10,12 @@ "repository": { | ||
"scripts": { | ||
"build:library": "tslint -p src/library && rimraf bld/library && tsc -p src/library", | ||
"build": "yarn build:library" | ||
"build:library": "rimraf bld/library && tsc -p src/library", | ||
"typecheck:test": "tsc -p test", | ||
"typecheck:examples": "tsc -p examples/basic && tsc -p examples/exact && tsc -p examples/fragment && tsc -p examples/query && tsc -p examples/route-component && tsc -p examples/multi-route-match", | ||
"lint:library": "tslint -p src/library", | ||
"lint:test": "tslint -p test", | ||
"lint:examples": "tslint -p examples/basic && tslint -p examples/exact && tslint -p examples/fragment && tslint -p examples/query && tslint -p examples/route-component && tslint -p examples/multi-route-match", | ||
"test:library": "yarn build:library && yarn lint:library && jest", | ||
"test:test": "yarn typecheck:test && yarn lint:test", | ||
"test:examples": "yarn typecheck:examples && yarn lint:examples", | ||
"test": "yarn test:library && yarn test:test && yarn test:examples" | ||
}, | ||
@@ -32,2 +40,3 @@ "main": "bld/library/index.js", | ||
"history": "^4.7.2", | ||
"jest": "^23.5.0", | ||
"mobx": "^5.1.0", | ||
@@ -39,2 +48,3 @@ "mobx-react": "^5.2.5", | ||
"rimraf": "^2.6.2", | ||
"ts-jest": "^23.1.4", | ||
"ts-node": "^7.0.1", | ||
@@ -47,2 +57,3 @@ "tslib": "^1.9.3", | ||
"dependencies": { | ||
"@types/jest": "^23.3.1", | ||
"hyphenate": "^0.2.1", | ||
@@ -49,0 +60,0 @@ "tslang": "^0.1.5" |
@@ -0,1 +1,4 @@ | ||
[](https://www.npmjs.com/package/boring-router) | ||
[](https://travis-ci.org/makeflow/boring-router) | ||
# Boring Router | ||
@@ -30,3 +33,3 @@ | ||
notFound: { | ||
$match: '**', | ||
$match: RouteMatch.rest, | ||
}, | ||
@@ -63,7 +66,5 @@ }, | ||
} | ||
const schema: RouteSchemaDict = {}; | ||
``` | ||
> Option `$match` with value `'*'` and `'**'` will be converted to regular expressions `/[^/]+/` and `/.+/` respectively. | ||
> Two pre-defined `$match` regular expression is available as `RouteMatch.fragment` (`/[^/]+/`) and `RouteMatch.rest` (`/.+/`). | ||
@@ -80,3 +81,3 @@ ## Route match | ||
$path(params?: Partial<TParamDict>, preserveQuery?: boolean): string; | ||
$ref(params?: Partial<TParamDict>, preserveQuery?: boolean): string; | ||
} | ||
@@ -83,0 +84,0 @@ ``` |
18031
178
7
18
309
+ Added@types/jest@^23.3.1
+ Added@types/jest@23.3.14(transitive)