@mapl/router 0.0.3 → 0.0.5
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/package.json +2 -1
- package/tree/compiler.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mapl/router",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"description": "Fast compiled router for all runtimes",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fast",
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"type": "module",
|
|
15
15
|
"main": "./index.js",
|
|
16
16
|
"types": "./index.d.ts",
|
|
17
|
+
"sideEffects": false,
|
|
17
18
|
"scripts": {
|
|
18
19
|
"build:test": "bun scripts/build.ts && bun test",
|
|
19
20
|
"build:bench": "bun build:test && bun scripts/bench.ts",
|
package/tree/compiler.js
CHANGED
|
@@ -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(${PATH_LEN}>${startIndexPrefix}${startIndexValue+partLen-2})`);for(let i=1;i<partLen;i++,startIndexValue++)builder.push(`if(${PATH}.charCodeAt(${startIndexPrefix}${startIndexValue})===${part.charCodeAt(i)})`);builder.push("{")}if(node[1]!==null){builder.push(`if(${PATH_LEN}===${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(${PATH}.charCodeAt(${startIndexPrefix}${startIndexValue})===${childrenKeys[0]}){`);compileNode(children[childrenKeys[0]],state,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix);builder.push("}")}else{builder.push(`switch(${PATH}.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 "}${PREV_PARAM_IDX}=${startIndexPrefix}${startIndexValue};`);const currentIndex=hasParam?PREV_PARAM_IDX:`${startIndexPrefix}${startIndexValue}`;const slashIndex=`${PATH}.indexOf('/',${currentIndex})`;if(hasChild||!hasStore)builder.push(`${hasParam?"":"let "}${CURRENT_PARAM_IDX}=${slashIndex};`);if(hasStore){builder.push(`if(${hasChild?CURRENT_PARAM_IDX:slashIndex}===-1){${PARAMS}.push(${
|
|
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(${PATH_LEN}>${startIndexPrefix}${startIndexValue+partLen-2})`);for(let i=1;i<partLen;i++,startIndexValue++)builder.push(`if(${PATH}.charCodeAt(${startIndexPrefix}${startIndexValue})===${part.charCodeAt(i)})`);builder.push("{")}if(node[1]!==null){builder.push(`if(${PATH_LEN}===${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(${PATH}.charCodeAt(${startIndexPrefix}${startIndexValue})===${childrenKeys[0]}){`);compileNode(children[childrenKeys[0]],state,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix);builder.push("}")}else{builder.push(`switch(${PATH}.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 "}${PREV_PARAM_IDX}=${startIndexPrefix}${startIndexValue};`);const currentIndex=hasParam?PREV_PARAM_IDX:`${startIndexPrefix}${startIndexValue}`;const slashIndex=`${PATH}.indexOf('/',${currentIndex})`;if(hasChild||!hasStore)builder.push(`${hasParam?"":"let "}${CURRENT_PARAM_IDX}=${slashIndex};`);if(hasStore){const paramsVal=`${PATH}.slice(${currentIndex})`;builder.push(`if(${hasChild?CURRENT_PARAM_IDX:slashIndex}===-1){${hasParam?`${PARAMS}.push(${paramsVal})`:`const ${PARAMS}=[${paramsVal}]`};`);state.compileItem(params[1],state,true);builder.push("}")}if(hasChild){const paramsVal=`${PATH}.substring(${currentIndex},${CURRENT_PARAM_IDX})`;builder.push(`if(${hasStore?"":`${CURRENT_PARAM_IDX}!==-1&&`}${CURRENT_PARAM_IDX}!==${currentIndex}){${hasParam?`${PARAMS}.push(${paramsVal})`:`const ${PARAMS}=[${paramsVal}]`};`);compileNode(params[0],state,true,hasParam,0,`${CURRENT_PARAM_IDX}+`);builder.push(`${PARAMS}.pop();}`)}if(requireAllocation)builder.push("}")}if(node[4]!==null){const noStore=node[1]===null;if(noStore)builder.push(`if(${PATH_LEN}!==${startIndexPrefix}${startIndexValue}){`);const paramsVal=`${PATH}.slice(${startIndexPrefix}${startIndexValue})`;builder.push(`${hasParam?`${PARAMS}.push(${paramsVal})`:`const ${PARAMS}=[${paramsVal}]`};`);state.compileItem(node[4],state,hasParam);if(noStore)builder.push("}")}if(partLen!==1)builder.push("}")}import{CURRENT_PARAM_IDX,PARAMS,PATH,PATH_LEN,PREV_PARAM_IDX}from"../constants";
|