@mapl/router 0.5.3 → 0.5.6

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/README.md CHANGED
@@ -1,7 +1,6 @@
1
1
  A fast radix tree router.
2
2
 
3
3
  # Patterns
4
-
5
4
  `@mapl/router` supports two basic patterns:
6
5
 
7
6
  ```ts
@@ -9,5 +8,16 @@ A fast radix tree router.
9
8
  '/**' // Capture everything after the slash
10
9
  ```
11
10
 
12
- ## Limitation
13
- Patterns and methods cannot include quotes and escape characters.
11
+ # Usage
12
+ These links mentioned below only works on [Github](https://github.com/mapljs/router).
13
+
14
+ See [jit.ts](./bench/routers/src/mapl/jit.ts) for jit compilation usage.
15
+
16
+ You can view example compiler outputs in [snapshots](./snapshots).
17
+
18
+ For jitless examples see:
19
+ - [regexp.ts](./bench/routers/src/mapl/regexp.ts): A RegExp router based on `@mapl/router` tree structure.
20
+ - [tree.ts](./bench/routers/src/mapl/tree.ts): A jitless tree router based on `@mapl/router` tree structure.
21
+
22
+ # Benchmark
23
+ See [routers](./bench/routers) guide for routers benchmarking.
@@ -1 +1 @@
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};
1
+ import compilePath from"../path/compiler.js";export default (router,methodInput,parsePath,startIndex)=>{let allRouter=router[``];let str=``;for(let key in router)if(key!==``)str+=(str===``?`if(`:`else if(`)+methodInput+`==="`+key+`"){`+(allRouter==null?parsePath:``)+compilePath(router[key],startIndex)+`}`;return allRouter==null?str:parsePath+str+compilePath(allRouter,startIndex)};
package/method/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import type { Router as PathRouter } from "../path/index.js";
2
- export type Router<T = unknown> = Partial<Record<string, PathRouter<T>>>;
2
+ export type Router<T = unknown> = Record<string, PathRouter<T>>;
3
+ export declare const createRouter: <T>() => Router<T>;
3
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 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)=>{insertItemToPath(router[method]??=createPathRouter(),path,item)};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapl/router",
3
- "version": "0.5.3",
3
+ "version": "0.5.6",
4
4
  "description": "The fastest possible JS router",
5
5
  "repository": {
6
6
  "type": "git",
@@ -13,11 +13,11 @@
13
13
  "packageManager": "pnpm@10.12.1+sha512.f0dda8580f0ee9481c5c79a1d927b9164f2c478e90992ad268bbb2465a736984391d6333d2c327913578b2804af33474ca554ba29c04a8b13060a717675ae3ac",
14
14
  "exports": {
15
15
  "./constants": "./constants.js",
16
- "./method/compiler": "./method/compiler.js",
17
16
  "./method": "./method/index.js",
18
- "./path/compiler": "./path/compiler.js",
17
+ "./method/compiler": "./method/compiler.js",
18
+ "./path": "./path/index.js",
19
19
  "./tree/compiler": "./tree/compiler.js",
20
20
  "./tree/node": "./tree/node.js",
21
- "./path": "./path/index.js"
21
+ "./path/compiler": "./path/compiler.js"
22
22
  }
23
23
  }
package/path/compiler.js CHANGED
@@ -1 +1 @@
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,``))};
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+=(str===``?`if(`:`else 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,5 +1,5 @@
1
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
- export declare const insertItem: <T>(router: Router, path: string, item: T) => void;
4
+ export declare const insertItem: <T>(router: Router<T>, path: string, item: T) => void;
5
5
  export declare const countParams: (path: string) => number;
@@ -1,2 +1,2 @@
1
1
  import type { Node } from "./node.js";
2
- export declare const compile: (node: Node<string>, paramCount: number, startIndexValue: number, startIndexPrefix: string) => string;
2
+ export declare const compile: (node: Node<string>, paramCount: number, idx: number, idxPrefix: string) => string;
package/tree/compiler.js CHANGED
@@ -1 +1 @@
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};
1
+ let toChar=c=>String.fromCharCode(+c[0]);export let compile=(node,paramCount,idx,idxPrefix)=>{let builder=`{`;let noStore=node[1]===null;let partLen=node[0].length;let currentIdx=idxPrefix+(idx+partLen);if(partLen>1){let start=idxPrefix+(idx+1);builder=partLen===2?`if(l`+(noStore?`>`:`>=`)+currentIdx+`)if(p[`+start+`]==="`+node[0][1]+`"){`:(noStore?`if(l>`+currentIdx+`)if(`:`if(`)+`p.startsWith("`+node[0].slice(1)+`",`+start+`)){`}else if(noStore)builder=`if(l>`+currentIdx+`){`;idx+=partLen;if(!noStore)builder+=`if(l===`+currentIdx+`){`+node[1]+`}`;if(node[2]!==null){let childrenEntries=Object.entries(node[2]);if(childrenEntries.length===1)builder+=`if(p[`+currentIdx+`]==="`+toChar(childrenEntries[0])+`"){`+compile(childrenEntries[0][1],paramCount,idx,idxPrefix)+`}`;else{builder+=`switch(p[`+currentIdx+`]){`;for(let i=0;i<childrenEntries.length;i++)builder+=`case"`+toChar(childrenEntries[i])+`":`+compile(childrenEntries[i][1],paramCount,idx,idxPrefix)+`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=`+currentIdx+`;`;currentIdx=`i`}let slashIndex=`p.indexOf("/"`+(currentIdx===`0`?``:`,`+currentIdx)+`)`;if(hasChild||!hasStore){builder+=(paramCount>0?``:`let `)+`j=`+slashIndex+`;`;slashIndex=`j`}if(hasStore)builder+=`if(`+slashIndex+`===-1){let q`+paramCount+`=`+(currentIdx===`0`?`p`:`p.slice(`+currentIdx+`)`)+`;`+params[1]+`}`;if(hasChild)builder+=(hasStore?`else if(`:`if(`)+`j>`+currentIdx+`){let q`+paramCount+`=p.slice(`+currentIdx+`,j);`+compile(params[0],paramCount+1,0,`j+`)+`}`}if(node[4]!==null)builder+=`let q`+paramCount+`=`+(currentIdx===`0`?`p`:`p.slice(`+currentIdx+`)`)+`;`+node[4];return builder+`}`};