@mapl/router 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/compile.d.ts +3 -0
- package/compile.js +1 -0
- package/index.d.ts +2 -2
- package/index.js +1 -1
- package/match.d.ts +4 -0
- package/match.js +1 -0
- package/package.json +1 -1
- package/tree/matcher.d.ts +6 -0
- package/tree/matcher.js +1 -0
- package/tree/node.d.ts +7 -7
package/README.md
CHANGED
package/compile.d.ts
ADDED
package/compile.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default (router)=>{let builder=router[0].length===0?"":`switch(p){${router[0].map((pair)=>`case "${pair[0].slice(1).replace(/"/g,"\\\"")}":{${pair[1]}break;}`).join("")}}`;return router[1]===null?builder:`${builder}let l=p.length;${compileNode(router[1],false,false,-1,"")}`};import compileNode from"./tree/compiler.js";
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { type Node } from './tree/node.js';
|
|
2
|
-
export type Router = [staticMap: [path: string, item:
|
|
3
|
-
export declare const createRouter: () => Router, insertItem: (router: Router, path: string, item: string) => void
|
|
2
|
+
export type Router<T = string> = [staticMap: [path: string, item: T][], root: Node<T> | null];
|
|
3
|
+
export declare const createRouter: () => Router, insertItem: (router: Router, path: string, item: string) => void;
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import{createNode,insertItem as nodeInsertItem}from"./tree/node.js";export var createRouter=()=>[[],null];export var insertItem=(router,path,item)=>{if(path.includes("*"))nodeInsertItem(router[1]??=createNode("/"),path,item);else router[0].push([path,item])};
|
package/match.d.ts
ADDED
package/match.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default (router)=>{let staticMap={};router[0].forEach((item)=>{staticMap[item[0].slice(1)]=item[1]});let rootNode=router[1];return rootNode===null?(path)=>staticMap[path]??null:(path,params)=>staticMap[path]??matchNode(rootNode,path,params,-1)};import matchNode from"./tree/matcher.js";
|
package/package.json
CHANGED
package/tree/matcher.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
let f=(node,path,params,start)=>{let part=node[0];let tmp=part.length;if(tmp>1&&(start+tmp>path.length||path.indexOf(part,start)!==start))return null;start+=tmp;if(start===path.length)return node[1];if(node[2]!==null){tmp=node[2][path.charCodeAt(start)];if(typeof tmp!=="undefined"){tmp=f(tmp,path,params,start);if(tmp!==null)return tmp}}if(node[3]!==null){tmp=path.indexOf("/",start);if(tmp===-1){if(node[3][1]!==null){params.push(path.substring(start));return node[3][1]}}else if(tmp!==start&&node[3][0]!==null){params.push(path.substring(start,tmp));tmp=f(node[3][0],path,params,tmp);if(tmp!==null)return tmp;params.pop()}}if(node[4]!==null){params.push(path.substring(start));return node[4]}return null};export default f;
|
package/tree/node.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
export type Node = [
|
|
1
|
+
export type Node<T = string> = [
|
|
2
2
|
part: string,
|
|
3
|
-
store:
|
|
3
|
+
store: T | null,
|
|
4
4
|
children: Node[] | null,
|
|
5
|
-
params: ParamNode | null,
|
|
6
|
-
wildcardStore:
|
|
5
|
+
params: ParamNode<T> | null,
|
|
6
|
+
wildcardStore: T | null
|
|
7
7
|
];
|
|
8
|
-
export type ParamNode = [
|
|
9
|
-
child: Node | null,
|
|
10
|
-
store:
|
|
8
|
+
export type ParamNode<T = string> = [
|
|
9
|
+
child: Node<T> | null,
|
|
10
|
+
store: T | null
|
|
11
11
|
];
|
|
12
12
|
export declare const createNode: (part: string) => Node, createParamNode: (nextNode: ParamNode[0]) => ParamNode, cloneNode: (node: Node, part: string) => Node, resetNode: (node: Node, part: string, children: Node[2]) => void, visitNode: (node: Node, path: string) => Node, insertItem: (node: Node, path: string, item: string) => void;
|