@mapl/router 0.1.0 → 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 +6 -6
- package/constants.js +1 -1
- package/index.js +1 -1
- package/package.json +1 -1
- package/tree/compiler.js +1 -1
package/constants.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export declare const REQ = "
|
|
2
|
-
export declare const PATH = "
|
|
3
|
-
export declare const PATH_LEN = "
|
|
4
|
-
export declare const PARAMS = "
|
|
5
|
-
export declare const PREV_PARAM_IDX = "
|
|
6
|
-
export declare const CURRENT_PARAM_IDX = "
|
|
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="
|
|
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(
|
|
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
package/tree/compiler.js
CHANGED
|
@@ -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(
|
|
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;
|