@mapl/router 0.0.21 → 0.1.0
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 +1 -1
- package/tree/compiler.d.ts +2 -1
- package/tree/compiler.js +1 -1
- package/tree/node.d.ts +2 -7
- package/tree/node.js +1 -1
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
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/compiler.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import type { Builder } from '@mapl/compiler';
|
|
2
2
|
import type { Node } from './node.js';
|
|
3
|
-
|
|
3
|
+
declare const f: (node: Node, builder: Builder<string>, hasParam: boolean, hasMultipleParams: boolean, startIndexValue: number, startIndexPrefix: string) => void;
|
|
4
|
+
export default f;
|
package/tree/compiler.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
let f=(node,builder,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix)=>{let part=node[0];let partLen=part.length;if(partLen!==1){builder.push(`if(__req_pl>${startIndexPrefix}${startIndexValue+partLen-1})`);for(let i=1;i<partLen;i++)builder.push(`if(__req_p.charCodeAt(${startIndexPrefix}${startIndexValue+i})===${part.charCodeAt(i)})`);builder.push("{")}startIndexValue+=partLen;if(node[1]!==null)builder.push(`if(__req_pl===${startIndexPrefix}${startIndexValue}){${node[1]}}`);if(node[2]!==null){let children=node[2];let childrenEntries=Object.entries(children);if(childrenEntries.length===1){builder.push(`if(__req_p.charCodeAt(${startIndexPrefix}${startIndexValue})===${childrenEntries[0][0]}){`);f(childrenEntries[0][1],builder,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix);builder.push("}")}else{builder.push(`switch(__req_p.charCodeAt(${startIndexPrefix}${startIndexValue})){`);for(let i=0,l=childrenEntries.length;i<l;i++){builder.push(`case ${childrenEntries[i][0]}:`);f(childrenEntries[i][1],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==="0"?"":","+currentIndex})`;if(hasChild||!hasStore)builder.push(`${hasParam?"":"let "}__req_cpi=${slashIndex};`);if(hasStore){let paramsVal=currentIndex==="0"?"__req_p":`__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}]`};`);f(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;let currentIndex=startIndexPrefix+startIndexValue;if(noStore)builder.push(`if(__req_pl!==${currentIndex}){`);let paramsVal=currentIndex==="0"?"__req_p":`__req_p.slice(${currentIndex})`;builder.push(`${hasParam?`__req_ps.push(${paramsVal})`:`let __req_ps=[${paramsVal}]`};${node[4]}`);if(noStore)builder.push("}")}if(partLen!==1)builder.push("}")};export default f;
|
package/tree/node.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export type Node = [
|
|
2
2
|
part: string,
|
|
3
3
|
store: string | null,
|
|
4
|
-
children:
|
|
4
|
+
children: Node[] | null,
|
|
5
5
|
params: ParamNode | null,
|
|
6
6
|
wildcardStore: string | null
|
|
7
7
|
];
|
|
@@ -9,9 +9,4 @@ export type ParamNode = [
|
|
|
9
9
|
child: Node | null,
|
|
10
10
|
store: string | null
|
|
11
11
|
];
|
|
12
|
-
export declare
|
|
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;
|
|
12
|
+
export declare const createNode: (part: string) => Node, createParamNode: (nextNode: ParamNode[0]) => ParamNode, cloneNode: (node: Node, part: string) => Node, resetNode: (node: Node, part: string, children: Node[2]) => void, visitNode: (node: Node, path: string) => Node, insertItem: (node: Node, path: string, item: string) => void;
|
package/tree/node.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export var createNode=(part)=>[part,null,null,null,null];export var createParamNode=(nextNode)=>[nextNode,null];export var cloneNode=(node,part)=>[part,node[1],node[2],node[3],node[4]];export var resetNode=(node,part,children)=>{node[0]=part;node[2]=children;node[1]=null;node[3]=null;node[4]=null};export var visitNode=(node,path)=>{for(let i=0,parts=path.split("*"),l=parts.length;i<l;++i){if(i!==0){if(node[3]===null){let nextNode=createNode(parts[i]);node[3]=createParamNode(nextNode);node=nextNode}else node=node[3][0]??=createNode(parts[i])}for(let j=0,pathPart=parts[i];;++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(typeof nextNode!=="undefined"){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.charCodeAt(j)!==nodePart.charCodeAt(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 var insertItem=(node,path,item)=>{if(path.charCodeAt(path.length-1)===42){if(path.charCodeAt(path.length-2)===42)visitNode(node,path.substring(0,path.length-2))[4]=item;else(visitNode(node,path.substring(0,path.length-1))[3]??=createParamNode(null))[1]=item}else visitNode(node,path)[1]=item};
|