@mapl/router 0.7.4 → 0.7.7

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapl/router",
3
- "version": "0.7.4",
3
+ "version": "0.7.7",
4
4
  "description": "The fastest possible JS router",
5
5
  "repository": {
6
6
  "type": "git",
@@ -9,13 +9,13 @@
9
9
  "license": "MIT",
10
10
  "type": "module",
11
11
  "exports": {
12
- "./utils": "./utils.js",
13
- "./method/compiler": "./method/compiler.js",
14
12
  "./constants": "./constants.js",
15
13
  "./path/compiler": "./path/compiler.js",
16
- "./path": "./path/index.js",
17
- "./tree/node": "./tree/node.js",
14
+ "./method/compiler": "./method/compiler.js",
15
+ "./utils": "./utils.js",
18
16
  "./method": "./method/index.js",
19
- "./tree/compiler": "./tree/compiler.js"
17
+ "./tree/compiler": "./tree/compiler.js",
18
+ "./path": "./path/index.js",
19
+ "./tree/node": "./tree/node.js"
20
20
  }
21
21
  }
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+=(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,``))};
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(startIndex)+`"){`+router[i+1]+`}`;return str+(isEmptyNode(router[0])?``:`let l=p.length;`+compile(router[0],0,-startIndex,``))};
package/path/index.d.ts CHANGED
@@ -1,4 +1,8 @@
1
1
  import { type Node } from "../tree/node.js";
2
- export type Router<T = unknown> = [staticMap: [path: string, item: T][], root: Node<T> | null];
2
+ /**
3
+ * @example
4
+ * [createNode('/'), '/', 'return new Response("Hi");', '/param/*', 'return new Response(p0)'];
5
+ */
6
+ export type Router<T = unknown> = [root: Node<T>, ...staticMap: any[]];
3
7
  export declare const createRouter: <T>() => Router<T>;
4
8
  export declare const insertItem: <T>(router: Router<T>, path: string, item: T) => void;
package/path/index.js CHANGED
@@ -1 +1 @@
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])};
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)};
package/tree/node.d.ts CHANGED
@@ -1,5 +1,6 @@
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
+ export declare const isEmptyNode: (node: Node<any>) => boolean;
3
4
  export declare const createNode: <T>(part: string) => Node<T>;
4
5
  export declare const createParamNode: (nextNode: ParamNode[0]) => ParamNode;
5
6
  export declare const cloneNode: (node: Node, part: string) => Node;
package/tree/node.js CHANGED
@@ -1 +1 @@
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]=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 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};
package/utils.d.ts CHANGED
@@ -1,8 +1 @@
1
1
  export declare const countParams: (path: string) => number;
2
- declare const _: unique symbol;
3
- export type ArgsCache = string[] & {
4
- [_]: ""
5
- };
6
- export declare const paramsCache: (prefix: string, paramCnt: number) => ArgsCache;
7
- export declare const addArg: (name: string, cache: ArgsCache) => ArgsCache;
8
- export {};
package/utils.js CHANGED
@@ -1 +1 @@
1
- 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};export let paramsCache=(prefix,paramCnt)=>{let arr=[``,prefix+1];prefix=`,`+prefix;for(let i=2;i<=paramCnt;i++)arr.push(arr[i-1]+prefix+i);return arr};export let addArg=(name,cache)=>{let arr=[cache[0]===``?name:cache[0]+`,`+name];name=`,`+name;for(let i=1;i<cache.length;i++)arr.push(cache[i]+name);return arr};
1
+ 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};