fix(core): update
This commit is contained in:
parent
8955fd5fa3
commit
b1ecf655ad
9095
package-lock.json
generated
9095
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
15
package.json
15
package.json
@ -14,11 +14,11 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.0.22",
|
||||
"@gitzone/tsbundle": "^1.0.69",
|
||||
"@gitzone/tstest": "^1.0.15",
|
||||
"@pushrocks/tapbundle": "^3.0.7",
|
||||
"@types/node": "^10.11.7",
|
||||
"tslint": "^5.11.0",
|
||||
"@gitzone/tsbundle": "^1.0.72",
|
||||
"@gitzone/tstest": "^1.0.43",
|
||||
"@pushrocks/tapbundle": "^3.2.9",
|
||||
"@types/node": "^14.0.23",
|
||||
"tslint": "^6.1.2",
|
||||
"tslint-config-prettier": "^1.15.0"
|
||||
},
|
||||
"dependencies": {
|
||||
@ -35,5 +35,8 @@
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
]
|
||||
}
|
||||
}
|
32
test/test.browser.ts
Normal file
32
test/test.browser.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartrouter from '../ts/index';
|
||||
|
||||
let testrouter: smartrouter.SmartRouter;
|
||||
|
||||
tap.test('first test', async () => {
|
||||
testrouter = new smartrouter.SmartRouter({});
|
||||
expect(testrouter).to.be.instanceOf(smartrouter.SmartRouter);
|
||||
});
|
||||
|
||||
tap.test('should handle a route change', async (tools) => {
|
||||
const done = tools.defer();
|
||||
testrouter.on('/myawesomeroute/:any', async (routeInfoArg) => {
|
||||
expect(routeInfoArg.params.any).to.equal('hello');
|
||||
done.resolve();
|
||||
});
|
||||
testrouter.pushUrl('/myawesomeroute/hello');
|
||||
await done.promise;
|
||||
});
|
||||
|
||||
tap.test('should handle a route change', async (tools) => {
|
||||
const done = tools.defer();
|
||||
testrouter.on('/myawesomeroute2/:wow', async (routeInfoArg) => {
|
||||
expect(routeInfoArg.params.wow).to.equal('hello2');
|
||||
done.resolve();
|
||||
});
|
||||
testrouter.pushUrl('/myawesomeroute2/hello2');
|
||||
await done.promise;
|
||||
expect(window.location.href).to.equal('http://localhost:3007/myawesomeroute2/hello2');
|
||||
});
|
||||
|
||||
tap.start();
|
@ -1,8 +0,0 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartrouter from '../ts/index';
|
||||
|
||||
tap.test('first test', async () => {
|
||||
const router = new smartrouter.SmartRouter({});
|
||||
});
|
||||
|
||||
tap.start();
|
25
ts/index.ts
25
ts/index.ts
@ -1,6 +1,6 @@
|
||||
import * as plugins from './smartrouter.plugins';
|
||||
|
||||
const routeLog = message => {
|
||||
const routeLog = (message) => {
|
||||
console.log(`%c[Router]%c ${message}`, 'color: rgb(255, 105, 100);', 'color: inherit');
|
||||
};
|
||||
|
||||
@ -8,12 +8,19 @@ export interface IRouterOptions {
|
||||
debug?: boolean;
|
||||
}
|
||||
|
||||
export type THandlerFunction = <T extends object>(routeArg: IRouteInfo) => Promise<any>;
|
||||
export interface IRouteInfo {
|
||||
path: string;
|
||||
index:number;
|
||||
params: {[key: string]: string};
|
||||
}
|
||||
|
||||
/**
|
||||
* Router
|
||||
*/
|
||||
export class SmartRouter {
|
||||
public options: IRouterOptions = {
|
||||
debug: false
|
||||
debug: false,
|
||||
};
|
||||
|
||||
/**
|
||||
@ -21,7 +28,7 @@ export class SmartRouter {
|
||||
*/
|
||||
public routes: Array<{
|
||||
matchFunction: plugins.pathToRegExp.MatchFunction;
|
||||
handler: <T extends object>(matchArg: plugins.pathToRegExp.Match<T>) => Promise<any>;
|
||||
handler: THandlerFunction;
|
||||
}> = [];
|
||||
|
||||
/**
|
||||
@ -31,11 +38,11 @@ export class SmartRouter {
|
||||
// lets set the router options
|
||||
this.options = {
|
||||
...this.options,
|
||||
...optionsArg
|
||||
...optionsArg,
|
||||
};
|
||||
|
||||
// lets subscribe to route changes
|
||||
window.addEventListener('popstate', popStateEventArg => {
|
||||
window.addEventListener('popstate', (popStateEventArg) => {
|
||||
popStateEventArg.preventDefault();
|
||||
this._handleRouteState();
|
||||
});
|
||||
@ -61,10 +68,10 @@ export class SmartRouter {
|
||||
* @param {string|RegExp} routeArg
|
||||
* @param {function} handlerArg
|
||||
*/
|
||||
public on(routeArg: string, handlerArg: () => Promise<any>) {
|
||||
public on(routeArg: string, handlerArg: THandlerFunction) {
|
||||
this.routes.push({
|
||||
matchFunction: plugins.pathToRegExp.match(routeArg),
|
||||
handler: handlerArg
|
||||
handler: handlerArg,
|
||||
});
|
||||
}
|
||||
|
||||
@ -73,13 +80,13 @@ export class SmartRouter {
|
||||
*/
|
||||
async _handleRouteState() {
|
||||
const currentLocation = window.location.pathname;
|
||||
const wantedRoute = this.routes.find(routeArg => {
|
||||
const wantedRoute = this.routes.find((routeArg) => {
|
||||
return !!routeArg.matchFunction(currentLocation);
|
||||
});
|
||||
|
||||
if (wantedRoute) {
|
||||
const routeResult = wantedRoute.matchFunction(currentLocation);
|
||||
await wantedRoute.handler(routeResult);
|
||||
await wantedRoute.handler(routeResult.valueOf() as IRouteInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user