6 Commits

Author SHA1 Message Date
50e14811d4 1.0.5 2020-11-30 01:38:22 +00:00
f7e5af4113 fix(core): update 2020-11-30 01:38:21 +00:00
806e121144 1.0.4 2020-07-15 18:51:18 +00:00
b1ecf655ad fix(core): update 2020-07-15 18:51:17 +00:00
8955fd5fa3 1.0.3 2020-06-03 14:08:08 +00:00
9c4a81d9cd fix(core): update 2020-06-03 14:08:08 +00:00
7 changed files with 8451 additions and 728 deletions

9097
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smartrouter",
"version": "1.0.2",
"version": "1.0.5",
"private": false,
"description": "a router for routing on websites",
"main": "dist_ts/index.js",
@ -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"
]
}

View File

@ -19,7 +19,7 @@ TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@pushrocks/smartrouter)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/smartrouter)](https://lossless.cloud)
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@pushrocks/smartrouter)](https://lossless.cloud)
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@pushrocks/smartrouter)](https://lossless.cloud)
Platform support | [![Supports Windows 10](https://badgen.net/badge/supports%20Windows%2010/yes/green?icon=windows)](https://lossless.cloud) [![Supports Mac OS X](https://badgen.net/badge/supports%20Mac%20OS%20X/yes/green?icon=apple)](https://lossless.cloud)

32
test/test.browser.ts Normal file
View 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();

View File

@ -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();

View File

@ -8,6 +8,13 @@ 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
*/
@ -19,7 +26,10 @@ export class SmartRouter {
/**
* the routes we are handling
*/
public routes: Array<{ matchFunction: plugins.pathToRegExp.MatchFunction; handler: <T extends object>(matchArg: plugins.pathToRegExp.Match<T>) => Promise<any> }> = [];
public routes: Array<{
matchFunction: plugins.pathToRegExp.MatchFunction;
handler: THandlerFunction;
}> = [];
/**
* Creates an instance of Router.
@ -58,8 +68,7 @@ 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,
@ -71,13 +80,13 @@ export class SmartRouter {
*/
async _handleRouteState() {
const currentLocation = window.location.pathname;
const wantedRoute = this.routes.find(routeArg => {
const wantedRoutes = this.routes.filter((routeArg) => {
return !!routeArg.matchFunction(currentLocation);
});
if (wantedRoute) {
for (const wantedRoute of wantedRoutes) {
const routeResult = wantedRoute.matchFunction(currentLocation);
await wantedRoute.handler(routeResult);
await wantedRoute.handler(routeResult.valueOf() as IRouteInfo);
}
}
}

View File

@ -1,5 +1,3 @@
import * as pathToRegExp from 'path-to-regexp';
export {
pathToRegExp
};
export { pathToRegExp };