@mapl/router 0.0.22 → 0.1.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/constants.d.ts CHANGED
@@ -1,6 +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";
1
+ export declare const REQ = "r";
2
+ export declare const PATH = "p";
3
+ export declare const PATH_LEN = "l";
4
+ export declare const PARAMS = "q";
5
+ export declare const PREV_PARAM_IDX = "i";
6
+ export declare const CURRENT_PARAM_IDX = "j";
package/constants.js CHANGED
@@ -1 +1 @@
1
- export var REQ="__req";export var PATH="__req_p";export var PATH_LEN="__req_pl";export var PARAMS="__req_ps";export var PREV_PARAM_IDX="__req_ppi";export var CURRENT_PARAM_IDX="__req_cpi";
1
+ export var REQ="r";export var PATH="p";export var PATH_LEN="l";export var PARAMS="q";export var PREV_PARAM_IDX="i";export var CURRENT_PARAM_IDX="j";
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]}}`);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("}")}}
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(p==="${key.slice(1).replace(/"/g,"\\\"")}"){${staticMap[key]}}`);hasMultiple=true}}if(router[1]!==null){if(hasStatic)contentBuilder.push("else{");contentBuilder.push("let l=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.22",
3
+ "version": "0.1.1",
4
4
  "main": "./index.js",
5
5
  "devDependencies": {
6
6
  "@stylistic/eslint-plugin": "latest",
@@ -1,3 +1,4 @@
1
1
  import type { Builder } from '@mapl/compiler';
2
2
  import type { Node } from './node.js';
3
- export declare function compileNode(node: Node, builder: Builder<string>, hasParam: boolean, hasMultipleParams: boolean, startIndexValue: number, startIndexPrefix: string): void;
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
- 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==="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}]`};`);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;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("}")}
1
+ let f=(node,builder,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix)=>{let part=node[0];let partLen=part.length;if(partLen!==1){builder.push(`if(l>${startIndexPrefix}${startIndexValue+partLen-1})`);for(let i=1;i<partLen;i++)builder.push(`if(p.charCodeAt(${startIndexPrefix}${startIndexValue+i})===${part.charCodeAt(i)})`);builder.push("{")}startIndexValue+=partLen;if(node[1]!==null)builder.push(`if(l===${startIndexPrefix}${startIndexValue}){${node[1]}}`);if(node[2]!==null){let children=node[2];let childrenEntries=Object.entries(children);if(childrenEntries.length===1){builder.push(`if(p.charCodeAt(${startIndexPrefix}${startIndexValue})===${childrenEntries[0][0]}){`);f(childrenEntries[0][1],builder,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix);builder.push("}")}else{builder.push(`switch(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 "}i=${startIndexPrefix}${startIndexValue};`);let currentIndex=hasParam?"i":startIndexPrefix+startIndexValue;let slashIndex=`p.indexOf('/'${currentIndex==="0"?"":","+currentIndex})`;if(hasChild||!hasStore)builder.push(`${hasParam?"":"let "}j=${slashIndex};`);if(hasStore){let paramsVal=currentIndex==="0"?"p":`p.slice(${currentIndex})`;builder.push(`if(${hasChild?"j":slashIndex}===-1){${hasParam?`q.push(${paramsVal})`:`let q=[${paramsVal}]`};${params[1]}}`)}if(hasChild){let paramsVal=`p.substring(${currentIndex},j)`;builder.push(`if(${hasStore?"":"j!==-1&&"}j!==${currentIndex}){${hasParam?`q.push(${paramsVal})`:`let q=[${paramsVal}]`};`);f(params[0],builder,true,hasParam,0,"j+");if(!requireAllocation)builder.push("q.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(l!==${currentIndex}){`);let paramsVal=currentIndex==="0"?"p":`p.slice(${currentIndex})`;builder.push(`${hasParam?`q.push(${paramsVal})`:`let q=[${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: Record<number, Node> | null,
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 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;
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 function createNode(part){return[part,null,null,null,null]}export function createParamNode(nextNode){return[nextNode,null]}export function cloneNode(node,part){return[part,node[1],node[2],node[3],node[4]]}export function resetNode(node,part,children){node[0]=part;node[2]=children;node[1]=null;node[3]=null;node[4]=null}export function visitNode(node,path){let parts=path.split("*");for(let i=0,{length}=parts;i<length;++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 function 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}
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};