@mapl/router 0.8.1 → 0.8.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 CHANGED
@@ -1,21 +1 @@
1
- {
2
- "name": "@mapl/router",
3
- "version": "0.8.1",
4
- "description": "The fastest possible JS router",
5
- "repository": {
6
- "type": "git",
7
- "url": "git+https://github.com/mapljs/router.git"
8
- },
9
- "license": "MIT",
10
- "type": "module",
11
- "exports": {
12
- "./utils": "./utils.js",
13
- "./method/compiler": "./method/compiler.js",
14
- "./constants": "./constants.js",
15
- "./method": "./method/index.js",
16
- "./path": "./path/index.js",
17
- "./tree/node": "./tree/node.js",
18
- "./path/compiler": "./path/compiler.js",
19
- "./tree/compiler": "./tree/compiler.js"
20
- }
21
- }
1
+ {"name":"@mapl/router","version":"0.8.2","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","./method/compiler":"./method/compiler.js","./path":"./path/index.js","./tree/node":"./tree/node.js","./tree/compiler":"./tree/compiler.js","./method":"./method/index.js"}}
@@ -1,3 +1,7 @@
1
1
  import type { Router } from "./index.js";
2
2
  declare const _default: (router: Router<string>, startIndex: 0 | 1) => string;
3
+ /**
4
+ * If `startIndex === 1`, input path should start with `/`.
5
+ * If `startIndex === 0`, input path should have sliced first `/`.
6
+ */
3
7
  export default _default;
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==="`+router[i].slice(1-startIndex)+`"){`+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=``;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,``))};
package/path/index.d.ts CHANGED
@@ -6,3 +6,4 @@ import { type Node } from "../tree/node.js";
6
6
  export type Router<T = unknown> = [root: Node<T>, ...staticMap: any[]];
7
7
  export declare const createRouter: <T>() => Router<T>;
8
8
  export declare const insertItem: <T>(router: Router<T>, path: string, item: T) => void;
9
+ export declare const createStaticMap: <T>(router: Router<T>) => Map<string, T>;
package/path/index.js CHANGED
@@ -1 +1 @@
1
- import{createNode,insertItem as nodeInsertItem}from"../tree/node.js";export let createRouter=()=>[createNode(`/`)];export let insertItem=(router,path,item)=>{path.includes(`*`)?nodeInsertItem(router[0],path,item):router.push(path,item)};
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};
package/tree/compiler.js CHANGED
@@ -1 +1 @@
1
- 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+=(childNode[1]==null?(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`}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]+`}`);if(hasChild){let childNode=params[0];let nodePath=childNode[0];builder+=(hasStore?`else if(`:`if(`)+`j>`+currentIdx+`){let q`+paramCount+`=p.slice(`+currentIdx+`,j);`+(childNode[1]==null?`if(l>j+`+nodePath.length+`)`:``)+(nodePath.length>1?`if(p.startsWith("`+nodePath.slice(1)+`",j+1)){`:`{`)+compile(childNode,paramCount+1,nodePath.length,`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 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+=(childNode[1]==null?(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`}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]+`}`);if(hasChild){let childNode=params[0];let nodePath=childNode[0];builder+=(hasStore?`else if(`:`if(`)+`j>`+currentIdx+`){let q`+paramCount+`=p.slice(`+currentIdx+`,j);`+(childNode[1]==null?`if(l>j+`+nodePath.length+`)`:``)+(nodePath.length>1?`if(p.startsWith("`+nodePath.slice(1)+`",j+1)){`:`{`)+compile(childNode,paramCount+1,nodePath.length,`j+`)+`}}`}}node[4]==null||(builder+=`let q`+paramCount+`=`+(currentIdx===`0`?`p`:`p.slice(`+currentIdx+`)`)+`;`+node[4]);return builder};
package/tree/node.d.ts CHANGED
@@ -1,9 +1,5 @@
1
1
  export type Node<T = unknown> = [part: string, store: T | null, children: Node<T>[] | null, 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 createNode: <T>(part: string) => Node<T>;
5
- export declare const createParamNode: (nextNode: ParamNode[0]) => ParamNode;
6
- export declare const cloneNode: (node: Node, part: string) => Node;
7
- export declare const resetNode: (node: Node, part: string, children: Node[2]) => void;
8
- export declare const visitNode: (node: Node, parts: string[]) => Node;
9
- export declare const insertItem: <T>(node: Node<T>, path: string, item: T) => void;
4
+ export declare const insertNewBranch: <T>(root: Node<T>, path: string, currentIdx: number, value: T) => void;
5
+ 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]===null&&node[3]===null&&node[4]===null;export let createNode=part=>[part,null,null,null,null];export let createParamNode=nextNode=>[nextNode,null];export let cloneNode=(node,part)=>[part,node[1],node[2],node[3],node[4]];export let resetNode=(node,part,children)=>{node[0]=part;node[2]=children;node[1]=node[3]=node[4]=null};export let visitNode=(node,parts)=>{for(let i=0;i<parts.length;++i){let pathPart=parts[i];if(i!==0)if(node[3]==null){let nextNode=createNode(pathPart);node[3]=createParamNode(nextNode);node=nextNode}else node=node[3][0]??=createNode(pathPart);for(let j=0;;++j){let nodePart=node[0];if(j===pathPart.length){if(j<nodePart.length){let children=[];children[nodePart.charCodeAt(j)]=cloneNode(node,nodePart.slice(j));resetNode(node,pathPart,children)}break}if(j===nodePart.length){if(node[2]==null)node[2]=[];else{let nextNode=node[2][pathPart.charCodeAt(j)];if(nextNode!=null){node=nextNode;pathPart=pathPart.slice(j);j=0;continue}}let nextNode=createNode(pathPart.slice(j));node[2][pathPart.charCodeAt(j)]=nextNode;node=nextNode;break}if(pathPart[j]!==nodePart[j]){let children=[];children[nodePart.charCodeAt(j)]=cloneNode(node,nodePart.slice(j));let nextNode=createNode(pathPart.slice(j));children[pathPart.charCodeAt(j)]=nextNode;resetNode(node,nodePart.substring(0,j),children);node=nextNode;break}}}return node};export let insertItem=(node,path,item)=>{if(path.endsWith(`*`))if(path[path.length-2]===`*`)visitNode(node,path.slice(0,-2).split(`*`))[4]=item;else (visitNode(node,path.slice(0,-1).split(`*`))[3]??=[null,null])[1]=item;else visitNode(node,path.split(`*`))[1]=item};
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};