@mapl/router 0.0.18 → 0.0.19
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.d.ts +6 -0
- package/index.d.ts +6 -0
- package/index.js +1 -1
- package/package.json +1 -1
- package/tree/compiler.d.ts +3 -0
- package/tree/node.d.ts +17 -0
package/constants.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
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
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Builder } from '@mapl/compiler';
|
|
2
|
+
import { type Node } from './tree/node.js';
|
|
3
|
+
export type Router = [staticMap: Record<string, string> | null, root: Node | null];
|
|
4
|
+
export declare function createRouter(): Router;
|
|
5
|
+
export declare function insertItem(router: Router, path: string, item: string): void;
|
|
6
|
+
export declare function compileRouter(router: Router, contentBuilder: Builder<string>): void;
|
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){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]}}`);
|
|
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]}}`);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
package/tree/node.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type Node = [
|
|
2
|
+
part: string,
|
|
3
|
+
store: string | null,
|
|
4
|
+
children: Record<number, Node> | null,
|
|
5
|
+
params: ParamNode | null,
|
|
6
|
+
wildcardStore: string | null
|
|
7
|
+
];
|
|
8
|
+
export type ParamNode = [
|
|
9
|
+
child: Node | null,
|
|
10
|
+
store: string | null
|
|
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: string): void;
|