@mapl/router 0.9.0 → 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/package.json CHANGED
@@ -1 +1 @@
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"}}
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/tree/compiler.js CHANGED
@@ -1 +1 @@
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};
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
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].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};
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};