@mapl/router 0.0.13 → 0.0.14
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.js +1 -1
- package/index.js +1 -1
- package/package.json +1 -1
- package/tree/compiler.js +1 -1
package/constants.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const REQ="__req";export const PATH
|
|
1
|
+
export const REQ="__req";export const PATH="__req_p";export const PATH_LEN="__req_pl";export const PARAMS="__req_ps";export const PREV_PARAM_IDX="__req_ppi";export const CURRENT_PARAM_IDX="__req_cpi";
|
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,state){const contentBuilder=state.contentBuilder;const hasStatic=router[0]!==null;if(hasStatic){const staticMap=router[0];let hasMultiple=false;for(const key in staticMap){contentBuilder.push(`${hasMultiple?"else ":""}if(__req_p==="${key.slice(1).replace(/"/g,'\\"')}"){`);state.compileItem(staticMap[key],state,false);contentBuilder.push("}");hasMultiple=true}}if(hasStatic)contentBuilder.push("else{");contentBuilder.push("let __req_pl=__req_p.length;");if(router[1]!==null)compileNode(router[1],state,false,false,-1,"");if(hasStatic)contentBuilder.push("}")}
|
package/package.json
CHANGED
package/tree/compiler.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export function compileNode(node,state,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix){const builder=state.contentBuilder;const part=node[0];const 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}){`);state.compileItem(node[1],state,hasParam);builder.push("}")}if(node[2]!==null){const children=node[2];const childrenKeys=Object.keys(children);if(childrenKeys.length===1){builder.push(`if(__req_p.charCodeAt(${startIndexPrefix}${startIndexValue})===${childrenKeys[0]}){`);compileNode(children[childrenKeys[0]],state,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]],state,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix);builder.push("break;")}builder.push("}")}}if(node[3]!==null){const params=node[3];const hasStore=params[1]!==null;const hasChild=params[0]!==null;const requireAllocation=hasParam?hasMultipleParams:hasChild||!hasStore;if(requireAllocation)builder.push("{");if(hasParam)builder.push(`${hasMultipleParams?"":"let "}__req_ppi=${startIndexPrefix}${startIndexValue};`);const currentIndex=hasParam?"__req_ppi":startIndexPrefix+startIndexValue;const slashIndex=`__req_p.indexOf('/',${currentIndex})`;if(hasChild||!hasStore)builder.push(`${hasParam?"":"let "}__req_cpi=${slashIndex};`);if(hasStore){const paramsVal=`__req_p.slice(${currentIndex})`;builder.push(`if(${hasChild?"__req_cpi":slashIndex}===-1){${hasParam?`__req_ps.push(${paramsVal})`:`let __req_ps=[${paramsVal}]`};`);state.compileItem(params[1],state,true);builder.push("}")}if(hasChild){const 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],state,true,hasParam,0,"__req_cpi+");builder.push("__req_ps.pop();}")}if(requireAllocation)builder.push("}")}if(node[4]!==null){const noStore=node[1]===null;if(noStore)builder.push(`if(__req_pl!==${startIndexPrefix}${startIndexValue}){`);const paramsVal=`__req_p.slice(${startIndexPrefix}${startIndexValue})`;builder.push(`${hasParam?`__req_ps.push(${paramsVal})`:`let __req_ps=[${paramsVal}]`};`);state.compileItem(node[4],state,true);if(noStore)builder.push("}")}if(partLen!==1)builder.push("}")}
|