@mapl/router 0.7.0 → 0.7.2
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/package.json +5 -7
- package/tree/compiler.js +1 -1
- package/utils.d.ts +7 -0
- package/utils.js +1 -1
- package/fast-method/compiler.d.ts +0 -13
- package/fast-method/compiler.js +0 -1
- package/fast-method/index.d.ts +0 -4
- package/fast-method/index.js +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mapl/router",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"description": "The fastest possible JS router",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -9,15 +9,13 @@
|
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"type": "module",
|
|
11
11
|
"exports": {
|
|
12
|
-
"./path/compiler": "./path/compiler.js",
|
|
13
|
-
"./method/compiler": "./method/compiler.js",
|
|
14
12
|
"./utils": "./utils.js",
|
|
15
|
-
"./fast-method/compiler": "./fast-method/compiler.js",
|
|
16
|
-
"./tree/node": "./tree/node.js",
|
|
17
13
|
"./constants": "./constants.js",
|
|
18
14
|
"./method": "./method/index.js",
|
|
15
|
+
"./method/compiler": "./method/compiler.js",
|
|
16
|
+
"./path/compiler": "./path/compiler.js",
|
|
17
|
+
"./path": "./path/index.js",
|
|
19
18
|
"./tree/compiler": "./tree/compiler.js",
|
|
20
|
-
"./
|
|
21
|
-
"./path": "./path/index.js"
|
|
19
|
+
"./tree/node": "./tree/node.js"
|
|
22
20
|
}
|
|
23
21
|
}
|
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
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
1
|
export declare const countParams: (path: string) => number;
|
|
2
|
+
declare const _: unique symbol;
|
|
3
|
+
export type ArgsCache = string[] & {
|
|
4
|
+
[_]: ""
|
|
5
|
+
};
|
|
6
|
+
export declare const paramsCache: (paramCnt: number) => ArgsCache;
|
|
7
|
+
export declare const addArg: (name: string, cache: ArgsCache) => ArgsCache;
|
|
8
|
+
export {};
|
package/utils.js
CHANGED
|
@@ -1 +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};
|
|
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};export let paramsCache=paramCnt=>{let arr=[``,`q1`];for(let i=2;i<=paramCnt;i++)arr.push(arr[i-1]+`,q`+i);return arr};export let addArg=(name,cache)=>{let arr=[cache[0]===``?name:cache[0]+name];for(let i=1;i<cache.length;i++)arr.push(cache[i]+`,`+name);return arr};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { Router } from "./index.js";
|
|
2
|
-
declare const _default: (router: Router<string>, methodInput: string, parsePath: string, startIndex: 0 | 1) => string;
|
|
3
|
-
/**
|
|
4
|
-
* Faster string comparisons for frameworks as most request URL strings are not optimized for slice & comparison.
|
|
5
|
-
*
|
|
6
|
-
* @example
|
|
7
|
-
* // Example path parser
|
|
8
|
-
* let $PATH = req.url,
|
|
9
|
-
* $PATH_START = p.indexOf('/', 12) + 1,
|
|
10
|
-
* $PATH_END = p.indexOf('?', s),
|
|
11
|
-
* $PATH_LEN = e === -1 ? p.length : e;
|
|
12
|
-
*/
|
|
13
|
-
export default _default;
|
package/fast-method/compiler.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{compile}from"../tree/compiler.js";export default (router,methodInput,parsePath,startIndex)=>{let allRouter=router[``];let str=``;for(let key in router)key!==``&&(str+=(str===``?`if(`:`else if(`)+methodInput+`==="`+key+`"){`+(allRouter==null?parsePath:``)+compile(router[key],0,-startIndex,`s+`)+`}`);return allRouter==null?str:parsePath+str+compile(allRouter,0,-startIndex,`s+`)};
|
package/fast-method/index.d.ts
DELETED
package/fast-method/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import{createNode,insertItem as nodeInsertItem}from"../tree/node.js";export let createRouter=()=>({});export let insertItem=(router,method,path,item)=>{nodeInsertItem(router[method]??=createNode(`/`),path,item)};
|