@mapl/router 0.8.7 → 0.9.0

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.
@@ -1,3 +1,3 @@
1
1
  import type { Router } from "./index.js";
2
- declare const _default: (router: Router<string>, methodInput: string, parsePath: string, startIndex: 0 | 1) => string;
2
+ declare const _default: (router: Router<string>, methodInput: string, parsePath: string, startIdx: 0 | 1) => string;
3
3
  export default _default;
@@ -1 +1 @@
1
- import compilePath from"../path/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:``)+compilePath(router[key],startIndex)+`}`);return allRouter==null?str:parsePath+str+compilePath(allRouter,startIndex)};
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> = Record<string, PathRouter<T>>;
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=()=>({});export let insertItem=(router,method,path,item)=>{insertItemToPath(router[method]??=createPathRouter(),path,item)};
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.8.7","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","./path/compiler":"./path/compiler.js","./constants":"./constants.js","./tree/node":"./tree/node.js","./path":"./path/index.js","./method/compiler":"./method/compiler.js","./tree/compiler":"./tree/compiler.js","./method":"./method/index.js"}}
1
+ {"name":"@mapl/router","version":"0.9.0","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","./method":"./method/index.js","./constants":"./constants.js","./method/compiler":"./method/compiler.js","./path":"./path/index.js","./tree/compiler":"./tree/compiler.js","./tree/node":"./tree/node.js","./path/compiler":"./path/compiler.js"}}
package/path/compiler.js CHANGED
@@ -1 +1 @@
1
- import{compile}from"../tree/compiler.js";import{isEmptyNode}from"../tree/node.js";export default (router,startIndex)=>{let str=``;for(let i=1;i<router.length;i+=2)str+=(str===``?`if(`:`else if(`)+`p==="`+(startIndex===1?router[i]:router[i].slice(1))+`"){`+router[i+1]+`}`;return str+(isEmptyNode(router[0])?``:`let l=p.length;`+compile(router[0],0,startIndex,``))};
1
+ import{compile}from"../tree/compiler.js";import{isEmptyNode}from"../tree/node.js";export default (router,startIndex)=>{let str=``;if(router.length>1){str=`if(p==="`+(startIndex===1?router[1]:router[1].slice(1))+`"){`+router[2]+`}`;for(let i=3;i<router.length;i+=2)str+=`else if(p==="`+(startIndex===1?router[i]:router[i].slice(1))+`"){`+router[i+1]+`}`}return isEmptyNode(router[0])?str:str+`let l=p.length;`+compile(router[0],0,startIndex,``)};
package/path/index.js CHANGED
@@ -1 +1 @@
1
- import{insert}from"../tree/node.js";export let createRouter=()=>[[`/`,null,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};
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&&node[4]!=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]!=null)for(let i=0,children=Object.values(node[2]);i<children.length;i++){let childNode=children[i];let nodePath=childNode[0];let nextIdx=idx+nodePath.length;builder+=(shouldBoundCheck(childNode)?(i>0?`else if(`:`if(`)+`l>`+idxPrefix+nextIdx+`&&`:i>0?`else if(`:`if(`)+`p.startsWith("`+nodePath+`",`+currentIdx+`)){`+compile(childNode,paramCount,nextIdx,idxPrefix)+`}`}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`}(hasChild||!hasStore)&&(builder+=(paramCount>0?``:`let `)+`j=p.indexOf("/",`+currentIdx+`);`);hasStore&&(builder+=`if(`+(hasChild?`j===-1){let `:`!p.includes("/",`+currentIdx+`)){let `)+`q`+paramCount+`=`+(currentIdx===`0`?`p;`:`p.slice(`+currentIdx+`);`)+params[1]+`}`);if(hasChild){let childNode=params[0];builder+=(hasStore?`else if(`:`if(`)+`j>`+currentIdx+`){let q`+paramCount+`=p.slice(`+currentIdx+`,j`+(shouldBoundCheck(childNode)?`);if(l>j+1){`+compile(childNode,paramCount+1,1,`j+`)+`}}`:`);`+compile(childNode,paramCount+1,1,`j+`)+`}`)}}node[4]==null||(builder+=`let q`+paramCount+`=`+(currentIdx===`0`?`p`:`p.slice(`+currentIdx+`)`)+`;`+node[4]);return builder};
1
+ export let shouldBoundCheck=node=>node[1]==null&&(node[2].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 children=node[3];if(children.length>1){builder+=`switch(p.charCodeAt(`+currentIdx+`)){`;for(let i=0,childrenFirstChar=node[2];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.startsWith("`+nodePath.slice(1)+`",`+idxPrefix+(idx+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.startsWith("`:`if(p.startsWith("`)+nodePath+`",`+currentIdx+`)){`+compile(childNode,paramCount,nextIdx,idxPrefix)+`}`}}if(node[4]!=null){let params=node[4];let hasStore=params[1]!=null;let hasChild=params[0]!=null;if(paramCount>0){builder+=`let i=`+currentIdx+`;`;currentIdx=`i`}(hasChild||!hasStore)&&(builder+=(paramCount>0?`j=p.indexOf("/",`:`let j=p.indexOf("/",`)+currentIdx+`);`);hasStore&&(builder+=(hasChild?`if(j===-1){let q`:`if(!p.includes("/",`+currentIdx+`)){let q`)+paramCount+(currentIdx===`0`?`=p;`:`=p.slice(`+currentIdx+`);`)+params[1]+`}`);if(hasChild){let childNode=params[0];builder+=(hasStore?`else if(j>`:`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+`)+`}`)}}node[5]==null||(builder+=`let q`+paramCount+(currentIdx===`0`?`=p;`:`=p.slice(`+currentIdx+`);`)+node[5]);return builder};
package/tree/node.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export type Node<T = unknown> = [part: string, store: T | null, children: Node<T>[] | null, params: ParamNode<T> | null, wildcardStore: T | null];
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
4
  export declare const insertNewBranch: <T>(root: Node<T>, path: string, currentIdx: number, value: T) => void;
package/tree/node.js CHANGED
@@ -1 +1 @@
1
- export let isEmptyNode=node=>node[1]===null&&node[2]===null&&node[3]===null&&node[4]===null;export let insertNewBranch=(root,path,currentIdx,value)=>{let startIdx=currentIdx;currentIdx=path.indexOf(`*`,startIdx);while(currentIdx>-1){if(startIdx<currentIdx){let nextNode=[path.slice(startIdx,currentIdx),null,null,null,null];(root[2]??=[])[path.charCodeAt(startIdx)]=nextNode;root=nextNode}if(currentIdx+2===path.length&&path.charCodeAt(currentIdx+1)===42){root[4]=value;return}if(currentIdx+1===path.length){root[3]=[null,value];return}let nextNode=[`/`,null,null,null,null];root[3]=[nextNode,null];root=nextNode;startIdx=currentIdx+2;currentIdx=path.indexOf(`*`,startIdx)}(root[2]??=[])[path.charCodeAt(startIdx)]=[path.slice(startIdx),value,null,null,null]};export let insert=(root,nodePartIdx,path,pathIdx,value)=>{let nodePart=root[0];while(pathIdx<path.length){let pathChar=path.charCodeAt(pathIdx);if(pathChar===42){if(pathIdx+2===path.length&&path.charCodeAt(pathIdx+1)===42){root[4]=value;return}if(root[3]===null){insertNewBranch(root,path,pathIdx,value);return}if(pathIdx+1===path.length){root[3][1]=value;return}root=root[3][0]??=[`/`,null,null,null,null];nodePartIdx=1;nodePart=`/`;pathIdx+=2}else if(nodePartIdx===nodePart.length){let children=root[2];if(children===null){insertNewBranch(root,path,pathIdx,value);return}let nextNode=children[pathChar];if(nextNode==null){insertNewBranch(root,path,pathIdx,value);return}root=nextNode;nodePartIdx=1;nodePart=root[0];pathIdx++}else if(pathChar!==nodePart.charCodeAt(nodePartIdx)){let children=[];children[nodePart.charCodeAt(nodePartIdx)]=[nodePart.slice(nodePartIdx),root[1],root[2],root[3],root[4]];root[0]=nodePart.slice(0,nodePartIdx);root[2]=children;root[1]=root[3]=root[4]=null;insertNewBranch(root,path,pathIdx,value);return}else{nodePartIdx++;pathIdx++}}if(nodePartIdx<nodePart.length){let children=[];children[nodePart.charCodeAt(nodePartIdx)]=[nodePart.slice(nodePartIdx),root[1],root[2],root[3],root[4]];root[0]=nodePart.slice(0,nodePartIdx);root[2]=children;root[3]=root[4]=null}root[1]=value};
1
+ export let isEmptyNode=node=>node[1]===null&&node[2].length===0&&node[4]===null&&node[5]===null;export let insertNewBranch=(root,path,currentIdx,value)=>{let startIdx=currentIdx;currentIdx=path.indexOf(`*`,startIdx);while(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}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;startIdx=currentIdx+2;currentIdx=path.indexOf(`*`,startIdx)}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){let pathChar=path.charCodeAt(pathIdx);if(pathChar===42){if(pathIdx+2===path.length&&path.charCodeAt(pathIdx+1)===42){root[5]=value;return}if(root[4]===null){insertNewBranch(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 if(nodePartIdx===nodePart.length){let childrenFirstChar=root[2];if(childrenFirstChar.length===0){insertNewBranch(root,path,pathIdx,value);return}let nextNodeId=childrenFirstChar.indexOf(pathChar);if(nextNodeId===-1){insertNewBranch(root,path,pathIdx,value);return}root=root[3][nextNodeId];nodePartIdx=1;nodePart=root[0];pathIdx++}else if(pathChar!==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};