@mapl/router 0.6.4 → 0.7.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/constants.d.ts +0 -5
- package/constants.js +1 -1
- package/package.json +5 -7
- package/path/index.d.ts +1 -2
- package/path/index.js +1 -1
- package/tree/compiler.js +1 -1
- package/utils.d.ts +1 -0
- package/utils.js +1 -0
package/constants.d.ts
CHANGED
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
export declare const REQ = "r";
|
|
2
1
|
export declare const PATH = "p";
|
|
3
2
|
export declare const PATH_START = "s";
|
|
4
3
|
export declare const PATH_END = "e";
|
|
5
|
-
export declare const URL = "u";
|
|
6
4
|
export declare const PATH_LEN = "l";
|
|
7
5
|
export declare const PARAMS = "q";
|
|
8
6
|
export declare const PREV_PARAM_IDX = "i";
|
|
9
7
|
export declare const CURRENT_PARAM_IDX = "j";
|
|
10
|
-
export declare const TMP = "t";
|
|
11
|
-
export declare const DEP = "f";
|
|
12
|
-
export declare const CTX = "c";
|
package/constants.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export let
|
|
1
|
+
export let PATH=`p`;export let PATH_START=`s`;export let PATH_END=`e`;export let PATH_LEN=`l`;export let PARAMS=`q`;export let PREV_PARAM_IDX=`i`;export let CURRENT_PARAM_IDX=`j`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mapl/router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.1",
|
|
4
4
|
"description": "The fastest possible JS router",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -8,16 +8,14 @@
|
|
|
8
8
|
},
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"type": "module",
|
|
11
|
-
"main": "./index.js",
|
|
12
|
-
"types": "./index.d.ts",
|
|
13
|
-
"packageManager": "pnpm@10.12.1+sha512.f0dda8580f0ee9481c5c79a1d927b9164f2c478e90992ad268bbb2465a736984391d6333d2c327913578b2804af33474ca554ba29c04a8b13060a717675ae3ac",
|
|
14
11
|
"exports": {
|
|
15
12
|
"./constants": "./constants.js",
|
|
16
|
-
"./
|
|
17
|
-
"./
|
|
13
|
+
"./utils": "./utils.js",
|
|
14
|
+
"./method": "./method/index.js",
|
|
18
15
|
"./path": "./path/index.js",
|
|
16
|
+
"./method/compiler": "./method/compiler.js",
|
|
19
17
|
"./path/compiler": "./path/compiler.js",
|
|
20
|
-
"./
|
|
18
|
+
"./tree/compiler": "./tree/compiler.js",
|
|
21
19
|
"./tree/node": "./tree/node.js"
|
|
22
20
|
}
|
|
23
21
|
}
|
package/path/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { type Node } from "../tree/node.js";
|
|
2
|
-
export type Router<T = unknown> = [staticMap: [path: string, item: T][], root: Node<T> |
|
|
2
|
+
export type Router<T = unknown> = [staticMap: [path: string, item: T][], root: Node<T> | null];
|
|
3
3
|
export declare const createRouter: <T>() => Router<T>;
|
|
4
4
|
export declare const insertItem: <T>(router: Router<T>, path: string, item: T) => void;
|
|
5
|
-
export declare const countParams: (path: string) => number;
|
package/path/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createNode,insertItem as nodeInsertItem}from"../tree/node.js";export let createRouter=()=>[[]
|
|
1
|
+
import{createNode,insertItem as nodeInsertItem}from"../tree/node.js";export let createRouter=()=>[[],null];export let insertItem=(router,path,item)=>{if(path.includes(`*`))nodeInsertItem(router[1]??=createNode(`/`),path,item);else router[0].push([path,item])};
|
package/tree/compiler.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export let compile=(node,paramCount,idx,idxPrefix)=>{let builder=`{`;let noStore=node[1]==null;let partLen=node[0].length;let currentIdx=idxPrefix+(idx+partLen);if(partLen>1){let start=idxPrefix+(idx+1);builder=partLen===2?`if(l`+(noStore?`>`:`>=`)+currentIdx+`)if(p.charCodeAt(`+start+`)===`+node[0].charCodeAt(1)+`){`:(noStore?`if(l>`+currentIdx+`)if(`:`if(`)+`p.startsWith("`+node[0].slice(1)+`",`+start+`)){`}else if(noStore)builder=`if(l>`+currentIdx+`){`;idx+=partLen;noStore||(builder+=`if(l===`+currentIdx+`){`+node[1]+`}`);if(node[2]!=null){let childrenEntries=Object.entries(node[2]);if(childrenEntries.length===1)builder+=`if(p.charCodeAt(`+currentIdx+`)===`+childrenEntries[0][0]+`){`+compile(childrenEntries[0][1],paramCount,idx,idxPrefix)+`}`;else{builder+=`switch(p.charCodeAt(`+currentIdx+`)){`;for(let i=0;i<childrenEntries.length;i++)builder+=`case `+childrenEntries[i][0]+`:{`+compile(childrenEntries[i][1],paramCount,idx,idxPrefix)+`}`;builder+=`}`}}if(node[3]!=null){let params=node[3];let hasStore=params[1]!=null;let hasChild=params[0]!=null;if(paramCount>0){builder+=`let i=`+currentIdx+`;`;currentIdx=`i`}let slashIndex=`p.indexOf("/"`+(currentIdx===`0`?``:`,`+currentIdx)+`)`;if(hasChild||!hasStore){builder+=(paramCount>0?``:`let `)+`j=`+slashIndex+`;`;slashIndex=`j`}hasStore&&(builder+=`if(`+slashIndex+`===-1){let q`+paramCount+`=`+(currentIdx===`0`?`p`:`p.slice(`+currentIdx+`)`)+`;`+params[1]+`}`);hasChild&&(builder+=(hasStore?`else if(`:`if(`)+`j>`+currentIdx+`){let q`+paramCount+`=p.slice(`+currentIdx+`,j);`+compile(params[0],paramCount+1,0,`j+`)+`}`)}node[4]==null||(builder+=`let q`+paramCount+`=`+(currentIdx===`0`?`p`:`p.slice(`+currentIdx+`)`)+`;`+node[4]);return builder+`}`};
|
|
1
|
+
export let compile=(node,paramCount,idx,idxPrefix)=>{let builder=`{`;let noStore=node[1]==null;let partLen=node[0].length;let currentIdx=idxPrefix+(idx+partLen);if(partLen>1){let start=idxPrefix+(idx+1);builder=partLen===2?`if(l`+(noStore?`>`:`>=`)+currentIdx+`)if(p.charCodeAt(`+start+`)===`+node[0].charCodeAt(1)+`){`:(noStore?`if(l>`+currentIdx+`)if(`:`if(`)+`p.startsWith("`+node[0].slice(1)+`",`+start+`)){`}else if(noStore)builder=`if(l>`+currentIdx+`){`;idx+=partLen;noStore||(builder+=`if(l===`+currentIdx+`){`+node[1]+`}`);if(node[2]!=null){let childrenEntries=Object.entries(node[2]);if(childrenEntries.length===1)builder+=`if(p.charCodeAt(`+currentIdx+`)===`+childrenEntries[0][0]+`){`+compile(childrenEntries[0][1],paramCount,idx,idxPrefix)+`}`;else{builder+=`switch(p.charCodeAt(`+currentIdx+`)){`;for(let i=0;i<childrenEntries.length;i++)builder+=`case `+childrenEntries[i][0]+`:{`+compile(childrenEntries[i][1],paramCount,idx,idxPrefix)+`break}`;builder+=`}`}}if(node[3]!=null){let params=node[3];let hasStore=params[1]!=null;let hasChild=params[0]!=null;if(paramCount>0){builder+=`let i=`+currentIdx+`;`;currentIdx=`i`}let slashIndex=`p.indexOf("/"`+(currentIdx===`0`?``:`,`+currentIdx)+`)`;if(hasChild||!hasStore){builder+=(paramCount>0?``:`let `)+`j=`+slashIndex+`;`;slashIndex=`j`}hasStore&&(builder+=`if(`+slashIndex+`===-1){let q`+paramCount+`=`+(currentIdx===`0`?`p`:`p.slice(`+currentIdx+`)`)+`;`+params[1]+`}`);hasChild&&(builder+=(hasStore?`else if(`:`if(`)+`j>`+currentIdx+`){let q`+paramCount+`=p.slice(`+currentIdx+`,j);`+compile(params[0],paramCount+1,0,`j+`)+`}`)}node[4]==null||(builder+=`let q`+paramCount+`=`+(currentIdx===`0`?`p`:`p.slice(`+currentIdx+`)`)+`;`+node[4]);return builder+`}`};
|
package/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const countParams: (path: string) => number;
|
package/utils.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export let countParams=path=>{let cnt=path.endsWith(`**`)?2:0;for(let i=path.length-cnt;(i=path.lastIndexOf(`*`,i-1))!==-1;cnt++);return cnt};
|