@mapl/router 0.5.2 → 0.5.3

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/constants.js CHANGED
@@ -1 +1 @@
1
- export var REQ="r";export var PATH="p";export var PATH_START="s";export var PATH_END="e";export var URL="u";export var PATH_LEN="l";export var PARAMS="q";export var PREV_PARAM_IDX="i";export var CURRENT_PARAM_IDX="j";export var TMP="t";export var DEP="f";export var CTX="c";
1
+ export let REQ=`r`;export let PATH=`p`;export let PATH_START=`s`;export let PATH_END=`e`;export let URL=`u`;export let PATH_LEN=`l`;export let PARAMS=`q`;export let PREV_PARAM_IDX=`i`;export let CURRENT_PARAM_IDX=`j`;export let TMP=`t`;export let DEP=`f`;export let CTX=`c`;
@@ -1,3 +1,3 @@
1
- import type { Router } from './index.js';
1
+ import type { Router } from "./index.js";
2
2
  declare const _default: (router: Router<string>, methodInput: string, parsePath: string, startIndex: 0 | 1) => string;
3
3
  export default _default;
@@ -1 +1 @@
1
- export default (router,methodInput,parsePath,startIndex)=>{let str="switch("+methodInput+"){";let all="";for(let key in router){if(key==="")all=parsePath+compilePath(router[""],startIndex);else str+='case"'+key+'":{'+parsePath+compilePath(router[key],startIndex)+"break}"}return str+"}"+all};import compilePath from"../path/compiler.js";
1
+ import compilePath from"../path/compiler.js";export default (router,methodInput,parsePath,startIndex)=>{let str=`switch(`+methodInput+`){`;let all=``;for(let key in router)if(key===``)all=parsePath+compilePath(router[``],startIndex);else str+=`case"`+key+`":{`+parsePath+compilePath(router[key],startIndex)+`break}`;return str+`}`+all};
package/method/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import type { Router as PathRouter } from '../path/index.js';
1
+ import type { Router as PathRouter } from "../path/index.js";
2
2
  export type Router<T = unknown> = Partial<Record<string, PathRouter<T>>>;
3
3
  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 var 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 insertItem=(router,method,path,item)=>{insertItemToPath(router[method]??=createPathRouter(),path,item)};
package/package.json CHANGED
@@ -1,39 +1,23 @@
1
1
  {
2
- "name": "@mapl/router",
3
- "version": "0.5.2",
4
- "main": "./index.js",
5
- "repository": {
6
- "type": "git",
7
- "url": "git+https://github.com/mapljs/router.git"
8
- },
9
- "devDependencies": {
10
- "@stylistic/eslint-plugin": "latest",
11
- "@types/bun": "latest",
12
- "eslint": "latest",
13
- "eslint-plugin-jsdoc": "latest",
14
- "mitata": "^1.0.34",
15
- "tsx": "^4.19.3",
16
- "typescript": "latest",
17
- "typescript-eslint": "latest"
18
- },
19
- "description": "Fast compiled router for all runtimes",
20
- "keywords": [
21
- "fast",
22
- "compiled",
23
- "router",
24
- "node",
25
- "bun",
26
- "deno"
27
- ],
28
- "license": "MIT",
29
- "scripts": {
30
- "build:test": "bun scripts/build.ts && bun test",
31
- "bench": "bun scripts/bench.ts",
32
- "build:publish": "bun build:test && bun scripts/publish.ts",
33
- "lint": "eslint ./src",
34
- "lint:fix": "eslint ./src --fix"
35
- },
36
- "sideEffects": false,
37
- "type": "module",
38
- "types": "./index.d.ts"
2
+ "name": "@mapl/router",
3
+ "version": "0.5.3",
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
+ "main": "./index.js",
12
+ "types": "./index.d.ts",
13
+ "packageManager": "pnpm@10.12.1+sha512.f0dda8580f0ee9481c5c79a1d927b9164f2c478e90992ad268bbb2465a736984391d6333d2c327913578b2804af33474ca554ba29c04a8b13060a717675ae3ac",
14
+ "exports": {
15
+ "./constants": "./constants.js",
16
+ "./method/compiler": "./method/compiler.js",
17
+ "./method": "./method/index.js",
18
+ "./path/compiler": "./path/compiler.js",
19
+ "./tree/compiler": "./tree/compiler.js",
20
+ "./tree/node": "./tree/node.js",
21
+ "./path": "./path/index.js"
39
22
  }
23
+ }
@@ -1,3 +1,3 @@
1
- import type { Router } from './index.js';
1
+ import type { Router } from "./index.js";
2
2
  declare const _default: (router: Router<string>, startIndex: 0 | 1) => string;
3
3
  export default _default;
package/path/compiler.js CHANGED
@@ -1 +1 @@
1
- export default (router,startIndex)=>{let str="";for(let i=0,pairs=router[0];i<pairs.length;i++)str+='if(p==="'+pairs[i][0].slice(startIndex)+'"){'+pairs[i][1]+"}";return str+(router[1]===null?"":"let l=p.length;"+compile(router[1],0,-startIndex,""))};import{compile}from"../tree/compiler.js";
1
+ import{compile}from"../tree/compiler.js";export default (router,startIndex)=>{let str=``;for(let i=0,pairs=router[0];i<pairs.length;i++)str+=`if(p==="`+pairs[i][0].slice(startIndex)+`"){`+pairs[i][1]+`}`;return str+(router[1]===null?``:`let l=p.length;`+compile(router[1],0,-startIndex,``))};
package/path/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { type Node } from '../tree/node.js';
1
+ import { type Node } from "../tree/node.js";
2
2
  export type Router<T = unknown> = [staticMap: [path: string, item: T][], root: Node<T> | null];
3
3
  export declare const createRouter: <T>() => Router<T>;
4
4
  export declare const insertItem: <T>(router: Router, path: string, item: T) => void;
package/path/index.js CHANGED
@@ -1 +1 @@
1
- import{createNode,insertItem as nodeInsertItem}from"../tree/node.js";export var createRouter=()=>[[],null];export var insertItem=(router,path,item)=>{if(path.includes("*"))nodeInsertItem(router[1]??=createNode("/"),path,item);else router[0].push([path,item])};export var countParams=(path)=>{let cnt=0;for(let i=-1;(i=path.indexOf("*",i+1))!==-1;cnt++);return cnt-(path.endsWith("**")?1:0)};
1
+ import{createNode,insertItem as nodeInsertItem}from"../tree/node.js";export let createRouter=()=>[[],null];export let insertItem=(router,path,item)=>{if(path.includes(`*`))nodeInsertItem(router[1]??=createNode(`/`),path,item);else router[0].push([path,item])};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,2 +1,2 @@
1
- import type { Node } from './node.js';
1
+ import type { Node } from "./node.js";
2
2
  export declare const compile: (node: Node<string>, paramCount: number, startIndexValue: number, startIndexPrefix: string) => string;
package/tree/compiler.js CHANGED
@@ -1 +1 @@
1
- let toChar=(c)=>String.fromCharCode(+c[0]);export var compile=(node,paramCount,startIndexValue,startIndexPrefix)=>{let builder="";if(node[0]!=="/"){let part=node[0];let start=startIndexPrefix+(startIndexValue+1);builder+="if(p"+(part.length===2?"["+start+']==="'+part[1]+'"':'.startsWith("'+part.slice(1)+'",'+start+")")+"){";startIndexValue+=part.length}else startIndexValue++;let currentIndex=startIndexPrefix+startIndexValue;if(node[1]!==null)builder+="if(l==="+currentIndex+"){"+node[1]+"}";if(node[2]!==null){let children=node[2];let childrenEntries=Object.entries(children);if(childrenEntries.length===1){builder+="if(p["+currentIndex+']==="'+toChar(childrenEntries[0])+'"){'+compile(childrenEntries[0][1],paramCount,startIndexValue,startIndexPrefix)+"}"}else{builder+="switch(p["+currentIndex+"]){";for(let i=0;i<childrenEntries.length;i++){builder+='case"'+toChar(childrenEntries[i])+'":{'+compile(childrenEntries[i][1],paramCount,startIndexValue,startIndexPrefix)+"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="+currentIndex+";";currentIndex="i"}let slashIndex='p.indexOf("/"'+(currentIndex==="0"?"":","+currentIndex)+")";if(hasChild||!hasStore){builder+=(paramCount>0?"":"let ")+"j="+slashIndex+";";slashIndex="j"}if(hasStore){let paramsVal=currentIndex==="0"?"p":"p.slice("+currentIndex+")";builder+="if("+slashIndex+"===-1){let q"+paramCount+"="+paramsVal+";"+params[1]+"}"}if(hasChild){let paramsVal="p.slice("+currentIndex+",j)";builder+="if(j>"+currentIndex+"){let q"+paramCount+"="+paramsVal+";"+compile(params[0],paramCount+1,0,"j+")+"}"}}if(node[4]!==null){let noStore=node[1]===null;let paramsVal=currentIndex==="0"?"p":"p.slice("+currentIndex+")";let body="let q"+paramCount+"="+paramsVal+";"+node[4];builder+=noStore?"if(l>"+currentIndex+"){"+body+"}":body}return node[0]!=="/"?builder+"}":builder};
1
+ let toChar=c=>String.fromCharCode(+c[0]);export let compile=(node,paramCount,startIndexValue,startIndexPrefix)=>{let builder=``;if(node[0]!==`/`){let part=node[0];let start=startIndexPrefix+(startIndexValue+1);builder+=`if(p`+(part.length===2?`[`+start+`]==="`+part[1]+`"`:`.startsWith("`+part.slice(1)+`",`+start+`)`)+`){`;startIndexValue+=part.length}else startIndexValue++;let currentIndex=startIndexPrefix+startIndexValue;if(node[1]!==null)builder+=`if(l===`+currentIndex+`){`+node[1]+`}`;if(node[2]!==null){let childrenEntries=Object.entries(node[2]);if(childrenEntries.length===1)builder+=`if(p[`+currentIndex+`]==="`+toChar(childrenEntries[0])+`"){`+compile(childrenEntries[0][1],paramCount,startIndexValue,startIndexPrefix)+`}`;else{builder+=`switch(p[`+currentIndex+`]){`;for(let i=0;i<childrenEntries.length;i++)builder+=`case"`+toChar(childrenEntries[i])+`":{`+compile(childrenEntries[i][1],paramCount,startIndexValue,startIndexPrefix)+`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=`+currentIndex+`;`;currentIndex=`i`}let slashIndex=`p.indexOf("/"`+(currentIndex===`0`?``:`,`+currentIndex)+`)`;if(hasChild||!hasStore){builder+=(paramCount>0?``:`let `)+`j=`+slashIndex+`;`;slashIndex=`j`}if(hasStore)builder+=`if(`+slashIndex+`===-1){let q`+paramCount+`=`+(currentIndex===`0`?`p`:`p.slice(`+currentIndex+`)`)+`;`+params[1]+`}`;if(hasChild)builder+=`if(j>`+currentIndex+`){let q`+paramCount+`=p.slice(`+currentIndex+`,j);`+compile(params[0],paramCount+1,0,`j+`)+`}`}if(node[4]!==null){let body=`let q`+paramCount+`=`+(currentIndex===`0`?`p`:`p.slice(`+currentIndex+`)`)+`;`+node[4];builder+=node[1]===null?`if(l>`+currentIndex+`){`+body+`}`:body}return node[0]!==`/`?builder+`}`:builder};
package/tree/node.d.ts CHANGED
@@ -1,14 +1,5 @@
1
- export type Node<T = unknown> = [
2
- part: string,
3
- store: T | null,
4
- children: Node<T>[] | null,
5
- params: ParamNode<T> | null,
6
- wildcardStore: T | null
7
- ];
8
- export type ParamNode<T = unknown> = [
9
- child: Node<T> | null,
10
- store: T | null
11
- ];
1
+ export type Node<T = unknown> = [part: string, store: T | null, children: Node<T>[] | null, params: ParamNode<T> | null, wildcardStore: T | null];
2
+ export type ParamNode<T = unknown> = [child: Node<T> | null, store: T | null];
12
3
  export declare const createNode: <T>(part: string) => Node<T>;
13
4
  export declare const createParamNode: (nextNode: ParamNode[0]) => ParamNode;
14
5
  export declare const cloneNode: (node: Node, part: string) => Node;
package/tree/node.js CHANGED
@@ -1 +1 @@
1
- export var createNode=(part)=>[part,null,null,null,null];export var createParamNode=(nextNode)=>[nextNode,null];export var cloneNode=(node,part)=>[part,node[1],node[2],node[3],node[4]];export var resetNode=(node,part,children)=>{node[0]=part;node[2]=children;node[1]=null;node[3]=null;node[4]=null};export var 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 var insertItem=(node,path,item)=>{if(path[path.length-1]==="*"){if(path[path.length-2]==="*")visitNode(node,path.slice(0,-2).split("*"))[4]=item;else(visitNode(node,path.slice(0,-1).split("*"))[3]??=createParamNode(null))[1]=item}else visitNode(node,path.split("*"))[1]=item};
1
+ 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]=null;node[3]=null;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[path.length-1]===`*`)if(path[path.length-2]===`*`)visitNode(node,path.slice(0,-2).split(`*`))[4]=item;else (visitNode(node,path.slice(0,-1).split(`*`))[3]??=createParamNode(null))[1]=item;else visitNode(node,path.split(`*`))[1]=item};