@mapl/router 0.1.1 → 0.1.3
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 +1 -2
- package/index.js +1 -1
- package/package.json +1 -4
- package/tree/compiler.d.ts +1 -2
- package/tree/compiler.js +1 -1
package/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { Builder } from '@mapl/compiler';
|
|
2
1
|
import { type Node } from './tree/node.js';
|
|
3
2
|
export type Router = [staticMap: Record<string, string> | null, root: Node | null];
|
|
4
3
|
export declare function createRouter(): Router;
|
|
5
4
|
export declare function insertItem(router: Router, path: string, item: string): void;
|
|
6
|
-
export declare function compileRouter(router: Router, contentBuilder:
|
|
5
|
+
export declare function compileRouter(router: Router, contentBuilder: string[]): void;
|
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){
|
|
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){if(router[0]!==null){let staticMap=router[0];contentBuilder.push("switch(p){");for(let key in staticMap)contentBuilder.push(`case "${key.slice(1).replace(/"/g,"\\\"")}":{${staticMap[key]}break;}`);contentBuilder.push("}")}if(router[1]!==null){contentBuilder.push("let l=p.length;");compileNode(router[1],contentBuilder,false,false,-1,"")}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mapl/router",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"main": "./index.js",
|
|
5
5
|
"devDependencies": {
|
|
6
6
|
"@stylistic/eslint-plugin": "latest",
|
|
@@ -11,9 +11,6 @@
|
|
|
11
11
|
"mitata": "latest",
|
|
12
12
|
"typescript": "latest"
|
|
13
13
|
},
|
|
14
|
-
"peerDependencies": {
|
|
15
|
-
"@mapl/compiler": "0.0.x"
|
|
16
|
-
},
|
|
17
14
|
"description": "Fast compiled router for all runtimes",
|
|
18
15
|
"keywords": [
|
|
19
16
|
"fast",
|
package/tree/compiler.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { Builder } from '@mapl/compiler';
|
|
2
1
|
import type { Node } from './node.js';
|
|
3
|
-
declare const f: (node: Node, builder:
|
|
2
|
+
declare const f: (node: Node, builder: string[], hasParam: boolean, hasMultipleParams: boolean, startIndexValue: number, startIndexPrefix: string) => void;
|
|
4
3
|
export default f;
|
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(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?"":"
|
|
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(j${hasStore?"!==":">"}${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;
|