@mapl/router 0.4.0 → 0.4.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/README.md CHANGED
@@ -6,5 +6,8 @@ A fast radix tree router.
6
6
 
7
7
  ```ts
8
8
  '/*/info' // Capture the value of a segment
9
- '/**' // Capture the entire path after
9
+ '/**' // Capture everything after the slash
10
10
  ```
11
+
12
+ ## Limitation
13
+ Patterns cannot include quotes and escape characters.
package/compile.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  import type { Router } from './index.js';
2
- declare const _default: (router: Router, startIndex: number) => string;
2
+ import type { Compiler } from './tree/compiler.ts';
3
+ declare const _default: (router: Router<string>, compile: Compiler, startIndex: 0 | 1) => string;
3
4
  export default _default;
package/compile.js CHANGED
@@ -1 +1 @@
1
- export default (router,startIndex)=>router[0].map((pair)=>'if(p==="'+pair[0].slice(-startIndex)+'"){'+pair[1]+"}").join("")+(router[1]===null?"":"let l=p.length;"+compileNode(router[1],0,startIndex,""));import compileNode from"./tree/compiler.js";
1
+ export default (router,compile,startIndex)=>router[0].map((pair)=>'if(p==="'+pair[0].slice(startIndex)+'"){'+pair[1]+"}").join("")+(router[1]===null?"":"let l=p.length;"+compile(router[1],0,-startIndex,""));
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
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
- export declare const createRouter: () => Router;
4
- export declare const insertItem: (router: Router, path: string, item: string) => void;
3
+ export declare const createRouter: <T>() => Router<T>;
4
+ export declare const insertItem: <T>(router: Router, path: string, item: T) => void;
5
+ export declare const insertItemWithParts: <T>(router: Router<T>, parts: string[], flag: 0 | 1 | 2, item: T) => void;
5
6
  export declare const countParams: (path: string) => number;
package/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,insertItemWithParts as nodeInsertItemWithParts}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 insertItemWithParts=(router,parts,flag,item)=>{if(flag===0)router[0].push([parts[0],item]);else nodeInsertItemWithParts(router[1]??=createNode("/"),parts,flag,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)};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapl/router",
3
- "version": "0.4.0",
3
+ "version": "0.4.1",
4
4
  "main": "./index.js",
5
5
  "devDependencies": {
6
6
  "@stylistic/eslint-plugin": "latest",
@@ -8,6 +8,7 @@
8
8
  "eslint": "latest",
9
9
  "eslint-plugin-jsdoc": "latest",
10
10
  "mitata": "^1.0.34",
11
+ "tsx": "^4.19.3",
11
12
  "typescript": "latest",
12
13
  "typescript-eslint": "latest"
13
14
  },
@@ -23,7 +24,7 @@
23
24
  "license": "MIT",
24
25
  "scripts": {
25
26
  "build:test": "bun scripts/build.ts && bun test",
26
- "build:bench": "bun build:test && bun scripts/bench.ts",
27
+ "bench": "bun scripts/bench.ts",
27
28
  "build:publish": "bun build:test && bun scripts/publish.ts",
28
29
  "lint": "eslint ./src",
29
30
  "lint:fix": "eslint ./src --fix"
@@ -1,3 +1,3 @@
1
1
  import type { Node } from './node.js';
2
- declare const f: (node: Node, paramCount: number, startIndexValue: number, startIndexPrefix: string) => string;
3
- export default f;
2
+ export type Compiler = (node: Node<string>, paramCount: number, startIndexValue: number, startIndexPrefix: string) => string;
3
+ export declare const o2: Compiler;
package/tree/compiler.js CHANGED
@@ -1 +1 @@
1
- let f=(node,paramCount,startIndexValue,startIndexPrefix)=>{let builder="";if(node[0]!=="/"){let part=node[0];let len=part.length;builder+="if(l>"+startIndexPrefix+(startIndexValue+len-1)+")";for(let i=1;i<len;i++)builder+="if(p.charCodeAt("+startIndexPrefix+(startIndexValue+i)+")==="+part.charCodeAt(i)+")";builder+="{";startIndexValue+=len}else startIndexValue++;if(node[1]!==null)builder+="if(l==="+startIndexPrefix+startIndexValue+"){"+node[1]+"}";if(node[2]!==null){let children=node[2];let childrenEntries=Object.entries(children);if(childrenEntries.length===1){builder+="if(p.charCodeAt("+startIndexPrefix+startIndexValue+")==="+childrenEntries[0][0]+"){"+f(childrenEntries[0][1],paramCount,startIndexValue,startIndexPrefix)+"}"}else{builder+="switch(p.charCodeAt("+startIndexPrefix+startIndexValue+")){";for(let i=0;i<childrenEntries.length;i++){builder+="case "+childrenEntries[i][0]+":"+f(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;let requireAllocation=paramCount>1||hasChild||!hasStore;if(requireAllocation)builder+="{";if(paramCount>0)builder+=(paramCount>1?"":"let ")+"i="+startIndexPrefix+startIndexValue+";";let currentIndex=paramCount>0?"i":startIndexPrefix+startIndexValue;let slashIndex="p.indexOf('/'"+(currentIndex==="0"?"":","+currentIndex)+")";if(hasChild||!hasStore)builder+=(paramCount>0?"":"let ")+"j="+slashIndex+";";if(hasStore){let paramsVal=currentIndex==="0"?"p":"p.slice("+currentIndex+")";builder+="if("+(hasChild?"j":slashIndex)+"===-1){let q"+paramCount+"="+paramsVal+";"+params[1]+"}"}if(hasChild){let paramsVal="p.substring("+currentIndex+",j)";builder+="if(j"+(hasStore?"!==":">")+currentIndex+"){let q"+paramCount+"="+paramsVal+";"+f(params[0],paramCount+1,0,"j+")+"}"}if(requireAllocation)builder+="}"}if(node[4]!==null){let noStore=node[1]===null;let currentIndex=startIndexPrefix+startIndexValue;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+"}"};export default f;
1
+ let toChar=(c)=>String.fromCharCode(+c[0]);export var o2=(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++;if(node[1]!==null)builder+="if(l==="+startIndexPrefix+startIndexValue+"){"+node[1]+"}";if(node[2]!==null){let children=node[2];let childrenEntries=Object.entries(children);if(childrenEntries.length===1){builder+="if(p["+startIndexPrefix+startIndexValue+']==="'+toChar(childrenEntries[0])+'"){'+o2(childrenEntries[0][1],paramCount,startIndexValue,startIndexPrefix)+"}"}else{builder+="switch(p["+startIndexPrefix+startIndexValue+"]){";for(let i=0;i<childrenEntries.length;i++){builder+='case"'+toChar(childrenEntries[i])+'":'+o2(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;builder+="{";if(paramCount>0)builder+=(paramCount>1?"":"let ")+"i="+startIndexPrefix+startIndexValue+";";let currentIndex=paramCount>0?"i":startIndexPrefix+startIndexValue;let slashIndex='p.indexOf("/"'+(currentIndex==="0"?"":","+currentIndex)+")";if(hasChild||!hasStore)builder+=(paramCount>0?"":"let ")+"j="+slashIndex+";";if(hasStore){let paramsVal=currentIndex==="0"?"p":"p.slice("+currentIndex+")";builder+="if("+(hasChild?"j":slashIndex)+"===-1){let q"+paramCount+"="+paramsVal+";"+params[1]+"}"}if(hasChild){let paramsVal="p.slice("+currentIndex+",j)";builder+="if(j>"+currentIndex+"){let q"+paramCount+"="+paramsVal+";"+o2(params[0],paramCount+1,0,"j+")+"}"}builder+="}"}if(node[4]!==null){let noStore=node[1]===null;let currentIndex=startIndexPrefix+startIndexValue;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+"}"};
package/tree/matcher.d.ts CHANGED
@@ -2,5 +2,5 @@ import type { Node } from './node.js';
2
2
  /**
3
3
  * Match a pathname and returns the result
4
4
  */
5
- declare const f: (node: Node<unknown>, path: string, params: string[], start: number) => unknown;
5
+ declare const f: <T>(node: Node<T>, path: string, params: string[], start: number) => T | null;
6
6
  export default f;
package/tree/node.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export type Node<T = unknown> = [
2
2
  part: string,
3
3
  store: T | null,
4
- children: Node[] | null,
4
+ children: Node<T>[] | null,
5
5
  params: ParamNode<T> | null,
6
6
  wildcardStore: T | null
7
7
  ];
@@ -9,7 +9,7 @@ export type ParamNode<T = unknown> = [
9
9
  child: Node<T> | null,
10
10
  store: T | null
11
11
  ];
12
- export declare const createNode: (part: string) => Node;
12
+ export declare const createNode: <T>(part: string) => Node<T>;
13
13
  export declare const createParamNode: (nextNode: ParamNode[0]) => ParamNode;
14
14
  export declare const cloneNode: (node: Node, part: string) => Node;
15
15
  export declare const resetNode: (node: Node, part: string, children: Node[2]) => void;