@mapl/router 0.0.17 → 0.0.18
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/index.js +1 -1
- package/package.json +4 -1
- package/tree/compiler.js +1 -1
- package/constants.d.ts +0 -6
- package/index.d.ts +0 -6
- package/tree/compiler.d.ts +0 -3
- package/tree/node.d.ts +0 -17
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{compileNode}from"./tree/compiler.js";import{createNode,insertItem as nodeInsertItem}from"./tree/node.js";export function createRouter(){return[null,null]}export function insertItem(router,path,item){if(path.includes("*"))nodeInsertItem(router[1]??=createNode("/"),path,item);else(router[0]??={})[path]=item}export function compileRouter(router,contentBuilder
|
|
1
|
+
import{compileNode}from"./tree/compiler.js";import{createNode,insertItem as nodeInsertItem}from"./tree/node.js";export function createRouter(){return[null,null]}export function insertItem(router,path,item){if(path.includes("*"))nodeInsertItem(router[1]??=createNode("/"),path,item);else(router[0]??={})[path]=item}export function compileRouter(router,contentBuilder){let hasStatic=router[0]!==null;if(hasStatic){let staticMap=router[0];let hasMultiple=false;for(let key in staticMap){contentBuilder.push(`${hasMultiple?"else ":""}if(__req_p==="${key.slice(1).replace(/"/g,"\\\"")}"){${staticMap[key]}}`);contentBuilder.push("}");hasMultiple=true}}if(router[1]!==null){if(hasStatic)contentBuilder.push("else{");contentBuilder.push("let __req_pl=__req_p.length;");compileNode(router[1],contentBuilder,false,false,-1,"");if(hasStatic)contentBuilder.push("}")}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mapl/router",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.18",
|
|
4
4
|
"main": "./index.js",
|
|
5
5
|
"devDependencies": {
|
|
6
6
|
"@stylistic/eslint-plugin": "latest",
|
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
"mitata": "latest",
|
|
12
12
|
"typescript": "latest"
|
|
13
13
|
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"@mapl/compiler": ">=^0.0.7"
|
|
16
|
+
},
|
|
14
17
|
"description": "Fast compiled router for all runtimes",
|
|
15
18
|
"keywords": [
|
|
16
19
|
"fast",
|
package/tree/compiler.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export function compileNode(node,builder,
|
|
1
|
+
export function compileNode(node,builder,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix){let part=node[0];let partLen=part.length;startIndexValue++;if(partLen!==1){builder.push(`if(__req_pl>${startIndexPrefix}${startIndexValue+partLen-2})`);for(let i=1;i<partLen;i++,startIndexValue++)builder.push(`if(__req_p.charCodeAt(${startIndexPrefix}${startIndexValue})===${part.charCodeAt(i)})`);builder.push("{")}if(node[1]!==null)builder.push(`if(__req_pl===${startIndexPrefix}${startIndexValue}){${node[1]}}`);if(node[2]!==null){let children=node[2];let childrenKeys=Object.keys(children);if(childrenKeys.length===1){builder.push(`if(__req_p.charCodeAt(${startIndexPrefix}${startIndexValue})===${childrenKeys[0]}){`);compileNode(children[childrenKeys[0]],builder,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix);builder.push("}")}else{builder.push(`switch(__req_p.charCodeAt(${startIndexPrefix}${startIndexValue})){`);for(let i=0,l=childrenKeys.length;i<l;i++){builder.push(`case ${childrenKeys[i]}:`);compileNode(children[childrenKeys[i]],builder,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix);builder.push("break;")}builder.push("}")}}if(node[3]!==null){let params=node[3];let hasStore=params[1]!==null;let hasChild=params[0]!==null;let requireAllocation=hasParam?hasMultipleParams:hasChild||!hasStore;if(requireAllocation)builder.push("{");if(hasParam)builder.push(`${hasMultipleParams?"":"let "}__req_ppi=${startIndexPrefix}${startIndexValue};`);let currentIndex=hasParam?"__req_ppi":startIndexPrefix+startIndexValue;let slashIndex=`__req_p.indexOf('/',${currentIndex})`;if(hasChild||!hasStore)builder.push(`${hasParam?"":"let "}__req_cpi=${slashIndex};`);if(hasStore){let paramsVal=`__req_p.slice(${currentIndex})`;builder.push(`if(${hasChild?"__req_cpi":slashIndex}===-1){${hasParam?`__req_ps.push(${paramsVal})`:`let __req_ps=[${paramsVal}]`};${params[1]}}`)}if(hasChild){let paramsVal=`__req_p.substring(${currentIndex},__req_cpi)`;builder.push(`if(${hasStore?"":"__req_cpi!==-1&&"}__req_cpi!==${currentIndex}){${hasParam?`__req_ps.push(${paramsVal})`:`let __req_ps=[${paramsVal}]`};`);compileNode(params[0],builder,true,hasParam,0,"__req_cpi+");if(!requireAllocation)builder.push("__req_ps.pop();");builder.push("}")}if(requireAllocation)builder.push("}")}if(node[4]!==null){let noStore=node[1]===null;if(noStore)builder.push(`if(__req_pl!==${startIndexPrefix}${startIndexValue}){`);let paramsVal=`__req_p.slice(${startIndexPrefix}${startIndexValue})`;builder.push(`${hasParam?`__req_ps.push(${paramsVal})`:`let __req_ps=[${paramsVal}]`};${node[4]}`);if(noStore)builder.push("}")}if(partLen!==1)builder.push("}")}
|
package/constants.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export declare const REQ = "__req";
|
|
2
|
-
export declare const PATH = "__req_p";
|
|
3
|
-
export declare const PATH_LEN = "__req_pl";
|
|
4
|
-
export declare const PARAMS = "__req_ps";
|
|
5
|
-
export declare const PREV_PARAM_IDX = "__req_ppi";
|
|
6
|
-
export declare const CURRENT_PARAM_IDX = "__req_cpi";
|
package/index.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import type { Builder } from '@mapl/compiler';
|
|
2
|
-
import { type Node } from './tree/node.js';
|
|
3
|
-
export type Router = [staticMap: Record<string, unknown> | null, root: Node | null];
|
|
4
|
-
export declare function createRouter(): Router;
|
|
5
|
-
export declare function insertItem(router: Router, path: string, item: any): void;
|
|
6
|
-
export declare function compileRouter(router: Router, contentBuilder: Builder<string>, compileItem: (item: any) => void): void;
|
package/tree/compiler.d.ts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import type { Builder } from '@mapl/compiler';
|
|
2
|
-
import type { Node } from './node.js';
|
|
3
|
-
export declare function compileNode(node: Node, builder: Builder<string>, compileItem: (item: any) => void, hasParam: boolean, hasMultipleParams: boolean, startIndexValue: number, startIndexPrefix: string): void;
|
package/tree/node.d.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export type Node = [
|
|
2
|
-
part: string,
|
|
3
|
-
store: unknown,
|
|
4
|
-
children: Record<number, Node> | null,
|
|
5
|
-
params: ParamNode | null,
|
|
6
|
-
wildcardStore: unknown
|
|
7
|
-
];
|
|
8
|
-
export type ParamNode = [
|
|
9
|
-
child: Node | null,
|
|
10
|
-
store: unknown
|
|
11
|
-
];
|
|
12
|
-
export declare function createNode(part: string): Node;
|
|
13
|
-
export declare function createParamNode(nextNode: ParamNode[0]): ParamNode;
|
|
14
|
-
export declare function cloneNode(node: Node, part: string): Node;
|
|
15
|
-
export declare function resetNode(node: Node, part: string, children: Node[2]): void;
|
|
16
|
-
export declare function visitNode(node: Node, path: string): Node;
|
|
17
|
-
export declare function insertItem(node: Node, path: string, item: unknown): void;
|