boring-router
Advanced tools
Comparing version 0.1.0-alpha.1 to 0.1.0-alpha.2
@@ -18,6 +18,6 @@ "use strict"; | ||
else if (match === '*') { | ||
this._matchPattern = /[^/]*/; | ||
this._matchPattern = /[^/]+/; | ||
} | ||
else if (match === '**') { | ||
this._matchPattern = /.*/; | ||
this._matchPattern = /.+/; | ||
} | ||
@@ -71,3 +71,3 @@ else { | ||
/** @internal */ | ||
_push(skipped, upperRest, upperFragmentDict, upperQueryDict, sourceQueryDict) { | ||
_push(skipped, upperRest, upperFragmentDict, sourceQueryDict) { | ||
let { current, rest } = this._match(skipped, upperRest); | ||
@@ -85,11 +85,13 @@ let name = this._name; | ||
let queryKeys = this._queryKeys; | ||
let queryDict = Object.assign({}, upperQueryDict, (queryKeys && matched | ||
? queryKeys.reduce((dict, key) => { | ||
let value = sourceQueryDict[key]; | ||
if (value !== undefined) { | ||
dict[key] = sourceQueryDict[key]; | ||
} | ||
return dict; | ||
}, {}) | ||
: undefined)); | ||
let queryDict = queryKeys | ||
? matched | ||
? queryKeys.reduce((dict, key) => { | ||
let value = sourceQueryDict[key]; | ||
if (value !== undefined) { | ||
dict[key] = sourceQueryDict[key]; | ||
} | ||
return dict; | ||
}, {}) | ||
: {} | ||
: undefined; | ||
this._query = queryDict; | ||
@@ -102,3 +104,2 @@ this._matched = matched; | ||
fragmentDict, | ||
queryDict, | ||
}; | ||
@@ -105,0 +106,0 @@ } |
@@ -16,9 +16,9 @@ import { History } from 'history'; | ||
export declare type RouteSchemaDict = Dict<RouteSchema | boolean>; | ||
export declare type RouteMatchType<TRouteSchema, TFragmentKey extends string, TQueryKey extends string> = RouteMatch<{ | ||
export declare type RouteMatchType<TRouteSchema, TFragmentKey extends string> = RouteMatch<{ | ||
[K in TFragmentKey]: string; | ||
}, Record<Extract<keyof RouteQuerySchemaType<TRouteSchema>, string> | TQueryKey, string | undefined>> & (TRouteSchema extends RouteSchemaChildrenPartial<infer TNestedRouteSchemaDict> ? { | ||
[K in Extract<keyof TNestedRouteSchemaDict, string>]: RouteMatchType<TNestedRouteSchemaDict[K], TFragmentKey | K, Extract<keyof RouteQuerySchemaType<TRouteSchema>, string> | TQueryKey>; | ||
}, Record<Extract<keyof RouteQuerySchemaType<TRouteSchema>, string>, string | undefined>> & (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, never>; | ||
[K in Extract<keyof TRouteSchemaDict, string>]: RouteMatchType<TRouteSchemaDict[K], K>; | ||
}; | ||
@@ -25,0 +25,0 @@ export declare class Router { |
@@ -12,3 +12,3 @@ "use strict"; | ||
}, {}); | ||
this.pushRouteChange(this, false, pathname, {}, {}, queryDict); | ||
this.pushRouteChange(this, false, pathname, {}, queryDict); | ||
}; | ||
@@ -19,3 +19,3 @@ this._children = this.buildRouteMatches(this, schema); | ||
} | ||
pushRouteChange(target, skipped, upperRest, upperFragmentDict, upperQueryDict, sourceQueryDict) { | ||
pushRouteChange(target, skipped, upperRest, upperFragmentDict, sourceQueryDict) { | ||
if (!target._children) { | ||
@@ -25,7 +25,7 @@ return; | ||
for (let routeMatch of target._children) { | ||
let { matched, rest, fragmentDict, queryDict } = routeMatch._push(skipped, upperRest, upperFragmentDict, upperQueryDict, sourceQueryDict); | ||
let { matched, rest, fragmentDict } = routeMatch._push(skipped, upperRest, upperFragmentDict, sourceQueryDict); | ||
if (matched) { | ||
skipped = true; | ||
} | ||
this.pushRouteChange(routeMatch, !matched, rest, fragmentDict, queryDict, sourceQueryDict); | ||
this.pushRouteChange(routeMatch, !matched, rest, fragmentDict, sourceQueryDict); | ||
} | ||
@@ -32,0 +32,0 @@ } |
{ | ||
"name": "boring-router", | ||
"version": "0.1.0-alpha.1", | ||
"version": "0.1.0-alpha.2", | ||
"scripts": { | ||
"build:library": "tslint -p src/library && rimraf bld/library && tsc -p src/library" | ||
"build:library": "tslint -p src/library && rimraf bld/library && tsc -p src/library", | ||
"build": "yarn build:library" | ||
}, | ||
@@ -23,2 +24,3 @@ "main": "bld/library/index.js", | ||
"@types/react": "^16.4.12", | ||
"@types/react-dom": "^16.0.7", | ||
"history": "^4.7.2", | ||
@@ -29,2 +31,3 @@ "mobx": "^5.1.0", | ||
"react": "^16.4.2", | ||
"react-dom": "^16.4.2", | ||
"rimraf": "^2.6.2", | ||
@@ -31,0 +34,0 @@ "ts-node": "^7.0.1", |
@@ -19,3 +19,3 @@ # Boring Router | ||
import {Route, Router} from 'boring-router'; | ||
import {observer} from 'mobx'; | ||
import {observer} from 'mobx-react'; | ||
import {createBrowserHistory} from 'history'; | ||
@@ -26,3 +26,3 @@ import React, {Component} from 'react'; | ||
const router = new Router( | ||
const router = Router.create( | ||
{ | ||
@@ -52,4 +52,59 @@ account: true, | ||
## Schema | ||
Boring Router defines routes via a tree-structure schema: | ||
```ts | ||
type RouteSchemaDict = Dict<RouteSchema | boolean>; | ||
interface RouteSchema { | ||
$match?: string | RegExp; | ||
$query?: Dict<boolean>; | ||
$children?: Dict<RouteSchema | boolean>; | ||
} | ||
const schema: RouteSchemaDict = {}; | ||
``` | ||
## Examples | ||
### Example list | ||
- [Basic](examples/basic) | ||
Basic usage. | ||
- [Exact](examples/exact) | ||
Match exact path. | ||
- [Fragment](examples/fragment) | ||
Boring Router's version of `/account/:id` alike parameter. | ||
- [Query](examples/query) | ||
Handle query string parameter. | ||
- [Route Component](examples/route-component) | ||
Use `<Route />` with a route component. | ||
- [Link](examples/link) | ||
Write a useful `<Link>`. | ||
### Run an example | ||
```sh | ||
yarn install | ||
yarn build | ||
yarn global add parcel | ||
parcel examples/[name]/index.html | ||
``` | ||
## License | ||
MIT License. |
14875
314
108
16