@mapl/router 0.8.8 → 0.9.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/method/compiler.d.ts +1 -1
- package/method/compiler.js +1 -1
- package/method/index.d.ts +1 -1
- package/method/index.js +1 -1
- package/package.json +1 -1
- package/path/index.js +1 -1
- package/tree/compiler.js +1 -1
- package/tree/node.d.ts +2 -1
- package/tree/node.js +1 -1
package/method/compiler.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { Router } from "./index.js";
|
|
2
|
-
declare const _default: (router: Router<string>, methodInput: string, parsePath: string,
|
|
2
|
+
declare const _default: (router: Router<string>, methodInput: string, parsePath: string, startIdx: 0 | 1) => string;
|
|
3
3
|
export default _default;
|
package/method/compiler.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import compilePath from"../path/compiler.js";export default (router,methodInput,parsePath,
|
|
1
|
+
import compilePath from"../path/compiler.js";export default (router,methodInput,parsePath,startIdx)=>{let pathRouters=router[1];let methods=router[0];let hasAllMethodHandler=router.length===3;let parsePathStr=hasAllMethodHandler?`"){`+parsePath:`"){`;let str=(hasAllMethodHandler?parsePath+`if(`:`if(`)+methodInput+`==="`+methods[0]+parsePathStr+compilePath(pathRouters[0],startIdx)+`}`;for(let i=1;i<pathRouters.length;i++)str+=`else if(`+methodInput+`==="`+methods[i]+parsePathStr+compilePath(pathRouters[i],startIdx)+`}`;return hasAllMethodHandler?str+compilePath(router[2],startIdx):str};
|
package/method/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import type { Router as PathRouter } from "../path/index.js";
|
|
2
|
-
export type Router<T = unknown> =
|
|
2
|
+
export type Router<T = unknown> = [string[], PathRouter<T>[]] | [string[], PathRouter<T>[], PathRouter<T>];
|
|
3
3
|
export declare const createRouter: <T>() => Router<T>;
|
|
4
4
|
export declare const insertItem: <T>(router: Router<T>, method: string, path: string, item: T) => void;
|
package/method/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createRouter as createPathRouter,insertItem as insertItemToPath}from"../path/index.js";export let createRouter=()=>
|
|
1
|
+
import{createRouter as createPathRouter,insertItem as insertItemToPath}from"../path/index.js";export let createRouter=()=>[[],[]];export let insertItem=(router,method,path,item)=>{if(method.length>0){let idx=router[0].indexOf(method);if(idx>-1)insertItemToPath(router[1][idx],path,item);else{let newRouter=createPathRouter();insertItemToPath(newRouter,path,item);router[0].push(method);router[1].push(newRouter)}}else{router.length===2&&router.push(createPathRouter());insertItemToPath(router[2],path,item)}};
|
package/package.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"name":"@mapl/router","version":"0.
|
|
1
|
+
{"name":"@mapl/router","version":"0.9.1","description":"The fastest possible JS router","repository":{"type":"git","url":"git+https://github.com/mapljs/router.git"},"license":"MIT","type":"module","exports":{"./utils":"./utils.js","./constants":"./constants.js","./path":"./path/index.js","./path/compiler":"./path/compiler.js","./method":"./method/index.js","./tree/compiler":"./tree/compiler.js","./tree/node":"./tree/node.js","./method/compiler":"./method/compiler.js"}}
|
package/path/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{insert}from"../tree/node.js";export let createRouter=()=>[[`/`,null,
|
|
1
|
+
import{insert}from"../tree/node.js";export let createRouter=()=>[[`/`,null,[],[],null,null]];export let insertItem=(router,path,item)=>{path.includes(`*`)?insert(router[0],1,path,1,item):router.push(path,item)};export let createStaticMap=router=>{let mp=new Map;for(let i=1;i<router.length;i+=2)mp.set(router[i],router[i+1]);return mp};
|
package/tree/compiler.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export let shouldBoundCheck=node=>node[1]==null;export let compile=(node,paramCount,idx,idxPrefix)=>{let builder=``;let currentIdx=idxPrefix+idx;node[1]==null||(builder+=`if(l
|
|
1
|
+
export let shouldBoundCheck=node=>node[1]==null&&(node[2].length!==1||node[3][0][0].length===1||node[4]!==null||node[5]!==null);export let compile=(node,paramCount,idx,idxPrefix)=>{let builder=``;let currentIdx=idxPrefix+idx;node[1]==null||(builder+=`if(l===${currentIdx}){${node[1]}}`);if(node[2].length>0){let childrenFirstChar=node[2];let children=node[3];if(children.length>1){builder+=`switch(p.charCodeAt(${currentIdx})){`;for(let i=0;i<children.length;i++){let childNode=children[i];let nodePath=childNode[0];let nextIdx=idx+nodePath.length;builder+=`case `+childrenFirstChar[i]+(shouldBoundCheck(childNode)?`:if(l>${idxPrefix+nextIdx})`:`:`)+(nodePath.length>1?`if(p`+(nodePath.length>2?`.startsWith("${nodePath.slice(1)}",${idxPrefix+(idx+1)})){`:`.charCodeAt(${idxPrefix+(idx+1)})===${nodePath.charCodeAt(1)}){`):`{`)+compile(childNode,paramCount,nextIdx,idxPrefix)+`}`}builder+=`}`}else{let childNode=children[0];let nodePath=childNode[0];let nextIdx=idx+nodePath.length;builder+=(shouldBoundCheck(childNode)?`if(l>${idxPrefix+nextIdx})if(p`:`if(p`)+(nodePath.length>1?`.startsWith("${nodePath}",${currentIdx})){`:`.charCodeAt(${currentIdx})===${childrenFirstChar[0]}){`)+compile(childNode,paramCount,nextIdx,idxPrefix)+`}`}}if(node[4]!=null){let params=node[4];let hasChild=params[0]!=null;if(hasChild){let childNode=params[0];if(paramCount>0){builder+=`let i=${currentIdx};`;currentIdx=`i`}else builder+=`let `;builder+=`j=p.indexOf("/",${currentIdx});if(j>${currentIdx}){let q${paramCount}=p.slice(`+currentIdx+(shouldBoundCheck(childNode)?`,j);if(l>j+1){${compile(childNode,paramCount+1,1,`j+`)}}}`:`,j);${compile(childNode,paramCount+1,1,`j+`)}}`)}params[1]!=null&&(builder+=(hasChild?`else if(j===-1){let q`:`if(!p.includes("/",${currentIdx})){let q`)+paramCount+(currentIdx===`0`?`=p;`:`=p.slice(${currentIdx});`)+params[1]+`}`)}node[5]==null||(builder+=`let q`+paramCount+(currentIdx===`0`?`=p;`:`=p.slice(${currentIdx});`)+node[5]);return builder};
|
package/tree/node.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
export type Node<T = unknown> = [part: string, store: T | null, children: Node<T>[]
|
|
1
|
+
export type Node<T = unknown> = [part: string, store: T | null, childrenFirstChar: number[], children: Node<T>[], params: ParamNode<T> | null, wildcardStore: T | null];
|
|
2
2
|
export type ParamNode<T = unknown> = [child: Node<T> | null, store: T | null];
|
|
3
3
|
export declare const isEmptyNode: (node: Node<any>) => boolean;
|
|
4
|
+
export declare const insertNewParam: <T>(root: Node<T>, path: string, currentIdx: number, value: T) => void;
|
|
4
5
|
export declare const insertNewBranch: <T>(root: Node<T>, path: string, currentIdx: number, value: T) => void;
|
|
5
6
|
export declare const insert: <T>(root: Node<T>, nodePartIdx: number, path: string, pathIdx: number, value: T) => void;
|
package/tree/node.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export let isEmptyNode=node=>node[1]===null&&node[2]===
|
|
1
|
+
export let isEmptyNode=node=>node[1]===null&&node[2].length===0&&node[4]===null&&node[5]===null;export let insertNewParam=(root,path,currentIdx,value)=>{if(currentIdx+2===path.length&&path.charCodeAt(currentIdx+1)===42){root[5]=value;return}if(currentIdx+1===path.length){root[4]=[null,value];return}let nextNode=[`/`,null,[],[],null,null];root[4]=[nextNode,null];root=nextNode;insertNewBranch(root,path,currentIdx+2,value)};export let insertNewBranch=(root,path,currentIdx,value)=>{let startIdx=currentIdx;currentIdx=path.indexOf(`*`,startIdx);if(currentIdx>-1){if(startIdx<currentIdx){let nextNode=[path.slice(startIdx,currentIdx),null,[],[],null,null];root[2].push(path.charCodeAt(startIdx));root[3].push(nextNode);root=nextNode}insertNewParam(root,path,currentIdx,value)}else{root[2].push(path.charCodeAt(startIdx));root[3].push([path.slice(startIdx),value,[],[],null,null])}};export let insert=(root,nodePartIdx,path,pathIdx,value)=>{let nodePart=root[0];while(pathIdx<path.length)if(nodePartIdx===nodePart.length)if(path.charCodeAt(pathIdx)===42){if(pathIdx+2===path.length&&path.charCodeAt(pathIdx+1)===42){root[5]=value;return}if(root[4]===null){insertNewParam(root,path,pathIdx,value);return}if(pathIdx+1===path.length){root[4][1]=value;return}root=root[4][0]??=[`/`,null,[],[],null,null];nodePartIdx=1;nodePart=`/`;pathIdx+=2}else{let childrenFirstChar=root[2];let nextNodeId=childrenFirstChar.indexOf(path.charCodeAt(pathIdx));if(nextNodeId===-1){insertNewBranch(root,path,pathIdx,value);return}root=root[3][nextNodeId];nodePartIdx=1;nodePart=root[0];pathIdx++}else if(path.charCodeAt(pathIdx)!==nodePart.charCodeAt(nodePartIdx)){root[3]=[[nodePart.slice(nodePartIdx),root[1],root[2],root[3],root[4],root[5]]];root[2]=[nodePart.charCodeAt(nodePartIdx)];root[0]=nodePart.slice(0,nodePartIdx);root[1]=root[4]=root[5]=null;insertNewBranch(root,path,pathIdx,value);return}else{nodePartIdx++;pathIdx++}if(nodePartIdx<nodePart.length){root[3]=[[nodePart.slice(nodePartIdx),root[1],root[2],root[3],root[4],root[5]]];root[2]=[nodePart.charCodeAt(nodePartIdx)];root[0]=nodePart.slice(0,nodePartIdx);root[4]=root[5]=null}root[1]=value};
|