@mapl/router 0.0.5 → 0.0.7

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.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { type Node } from './tree/node';
2
- import type { RouterCompilerState } from './types';
1
+ import { type Node } from './tree/node.js';
2
+ import type { RouterCompilerState } from './types.js';
3
3
  export type Router = [staticMap: Record<string, unknown> | null, root: Node | null];
4
4
  export declare function createRouter(): Router;
5
5
  export declare function insertItem(router: Router, path: string, item: any): void;
package/index.js CHANGED
@@ -1 +1 @@
1
- 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){if(router[0]!==null){const staticMap=router[0];const contentBuilder=state.contentBuilder;for(const key in staticMap){contentBuilder.push(`if(${PATH}===${JSON.stringify(key.slice(1))}){`);state.compileItem(staticMap[key],state,false);contentBuilder.push("}")}}if(router[1]!==null)compileNode(router[1],state,false,false,-1,"")}import{PATH}from"./constants";import{compileNode}from"./tree/compiler";import{createNode,insertItem as nodeInsertItem}from"./tree/node";
1
+ 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;if(router[0]!==null){const staticMap=router[0];for(const key in staticMap){contentBuilder.push(`if(${PATH}===${JSON.stringify(key.slice(1))}){`);state.compileItem(staticMap[key],state,false);contentBuilder.push("}")}}contentBuilder.push(`const ${PATH_LEN}=${PATH}.length;`);if(router[1]!==null)compileNode(router[1],state,false,false,-1,"")}import{PATH,PATH_LEN}from"./constants.js";import{compileNode}from"./tree/compiler.js";import{createNode,insertItem as nodeInsertItem}from"./tree/node.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapl/router",
3
- "version": "0.0.5",
3
+ "version": "0.0.7",
4
4
  "description": "Fast compiled router for all runtimes",
5
5
  "keywords": [
6
6
  "fast",
@@ -1,3 +1,3 @@
1
- import type { RouterCompilerState } from '../types';
2
- import type { Node } from './node';
1
+ import type { RouterCompilerState } from '../types.js';
2
+ import type { Node } from './node.js';
3
3
  export declare function compileNode(node: Node, state: RouterCompilerState, hasParam: boolean, hasMultipleParams: boolean, startIndexValue: number, startIndexPrefix: string): void;
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){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";
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.js";
package/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import type { CompilerState } from '@mapl/compiler';
2
2
  export interface RouterCompilerState extends CompilerState {
3
- compileItem: (item: unknown, state: RouterCompilerState, hasParam: boolean) => void;
3
+ compileItem: (item: any, state: RouterCompilerState, hasParam: boolean) => void;
4
4
  }