@mapl/router 0.2.1 → 0.2.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/constants.d.ts +1 -0
- package/constants.js +1 -1
- package/fast-compile.d.ts +7 -0
- package/fast-compile.js +1 -0
- package/package.json +4 -4
- package/tree/compiler.js +1 -1
- package/tree/matcher.d.ts +1 -1
- package/tree/matcher.js +1 -1
- package/match.d.ts +0 -4
- package/match.js +0 -1
package/constants.d.ts
CHANGED
package/constants.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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";
|
|
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";export var TMP="t";
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Router } from './index.js';
|
|
2
|
+
export declare const injectMatcher: (deps: any[]) => number;
|
|
3
|
+
/**
|
|
4
|
+
* Compile the router but with no pattern matching code
|
|
5
|
+
*/
|
|
6
|
+
declare const _default: (router: Router, decls: string[], deps: any[], matcherId: number, captures: string) => string;
|
|
7
|
+
export default _default;
|
package/fast-compile.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default (router,decls,deps,matcherId,captures)=>{let builder=router[0].length===0?"":`switch(p){${router[0].map((pair)=>`case "${pair[0].slice(1).replace(/"/g,"\\\"")}":{${pair[1]}break;}`).join("")}}`;if(router[1]===null)return builder;let fns=[];f(router[1],fns);let args=`(t,q${captures})`;return`${builder}let q=[],t=f${matcherId}(f${deps.push(router[1])},p,q,-1,p.length);if(t!==null){t=d${decls.push(`${args}=>{${fns.map((fn,i)=>`if(t===${i+1}){${fn}}`).join("")}}`)}${args};if(t!=null)return t;}`};import matcher from"./tree/matcher.js";let f=(node,fns)=>{if(node[1]!==null)node[1]=fns.push(node[1]);if(node[2]!==null){let children=node[2];for(let key in children)f(children[key],fns)}if(node[3]!==null){let params=node[3];if(params[1]!==null)params[1]=fns.push(params[1]);if(params[0]!==null)f(params[0],fns)}if(node[4]!==null)node[4]=fns.push(node[4])};export var injectMatcher=(deps)=>deps.push(matcher);
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mapl/router",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"main": "./index.js",
|
|
5
5
|
"devDependencies": {
|
|
6
6
|
"@stylistic/eslint-plugin": "latest",
|
|
7
|
+
"@types/bun": "latest",
|
|
7
8
|
"eslint": "latest",
|
|
8
|
-
"typescript-eslint": "latest",
|
|
9
9
|
"eslint-plugin-jsdoc": "latest",
|
|
10
|
-
"@types/bun": "latest",
|
|
11
10
|
"mitata": "latest",
|
|
12
|
-
"typescript": "latest"
|
|
11
|
+
"typescript": "latest",
|
|
12
|
+
"typescript-eslint": "latest"
|
|
13
13
|
},
|
|
14
14
|
"description": "Fast compiled router for all runtimes",
|
|
15
15
|
"keywords": [
|
package/tree/compiler.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let f=(node,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix)=>{let builder="";let part=node[0];let
|
|
1
|
+
let f=(node,hasParam,hasMultipleParams,startIndexValue,startIndexPrefix)=>{let builder="";if(node[0].length!==1){let part=node[0];let len=part.length;builder+=`if(l>${startIndexPrefix}${startIndexValue+len-1})`;for(let i=1;i<len;i++)builder+=`if(p.charCodeAt(${startIndexPrefix}${startIndexValue+i})===${part.charCodeAt(i)})`;builder+="{";startIndexValue+=len}else startIndexValue++;if(node[1]!==null)builder+=`if(l===${startIndexPrefix}${startIndexValue}){${node[1]}}`;if(node[2]!==null){let children=node[2];let childrenEntries=Object.entries(children);if(childrenEntries.length===1){builder+=`if(p.charCodeAt(${startIndexPrefix}${startIndexValue})===${childrenEntries[0][0]}){${f(childrenEntries[0][1],hasParam,hasMultipleParams,startIndexValue,startIndexPrefix)}}`}else{builder+=`switch(p.charCodeAt(${startIndexPrefix}${startIndexValue})){`;for(let i=0,l=childrenEntries.length;i<l;i++){builder+=`case ${childrenEntries[i][0]}:${f(childrenEntries[i][1],hasParam,hasMultipleParams,startIndexValue,startIndexPrefix)}break;`}builder+="}"}}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+="{";if(hasParam)builder+=`${hasMultipleParams?"":"let "}i=${startIndexPrefix}${startIndexValue};`;let currentIndex=hasParam?"i":startIndexPrefix+startIndexValue;let slashIndex=`p.indexOf('/'${currentIndex==="0"?"":","+currentIndex})`;if(hasChild||!hasStore)builder+=`${hasParam?"":"let "}j=${slashIndex};`;if(hasStore){let paramsVal=currentIndex==="0"?"p":`p.slice(${currentIndex})`;builder+=`if(${hasChild?"j":slashIndex}===-1){${hasParam?`q.push(${paramsVal})`:`let q=[${paramsVal}]`};${params[1]}}`}if(hasChild){let paramsVal=`p.substring(${currentIndex},j)`;builder+=`if(j${hasStore?"!==":">"}${currentIndex}){${hasParam?`q.push(${paramsVal})`:`let q=[${paramsVal}]`};${f(params[0],true,hasParam,0,"j+")}${requireAllocation?"":"q.pop();"}}`}if(requireAllocation)builder+="}"}if(node[4]!==null){let noStore=node[1]===null;let currentIndex=startIndexPrefix+startIndexValue;let paramsVal=currentIndex==="0"?"p":`p.slice(${currentIndex})`;let body=`${hasParam?`q.push(${paramsVal})`:`let q=[${paramsVal}]`};${node[4]}`;builder+=noStore?`if(l!==${currentIndex}){${body}}`:body}return node[0].length===1?builder:builder+"}"};export default f;
|
package/tree/matcher.d.ts
CHANGED
|
@@ -2,5 +2,5 @@ import type { Node } from './node.js';
|
|
|
2
2
|
/**
|
|
3
3
|
* Match a pathname and returns the result
|
|
4
4
|
*/
|
|
5
|
-
declare const f: (node: Node<unknown>, path: string, params: string[], start: number) => unknown;
|
|
5
|
+
declare const f: (node: Node<unknown>, path: string, params: string[], start: number, len: number) => unknown;
|
|
6
6
|
export default f;
|
package/tree/matcher.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let f=(node,path,params,start)=>{let part=node[0];let tmp=part.length;if(tmp
|
|
1
|
+
let f=(node,path,params,start,len)=>{let part=node[0];let tmp=part.length;if(tmp!==1&&(start+tmp>len||path.indexOf(part,start)!==start))return null;start+=tmp;if(start===len&&node[1]!==null)return node[1];if(node[2]!==null){tmp=node[2][path.charCodeAt(start)];if(typeof tmp!=="undefined"){tmp=f(tmp,path,params,start,len);if(tmp!==null)return tmp}}if(node[3]!==null){tmp=path.indexOf("/",start);if(tmp===-1){if(node[3][1]!==null){params.push(path.substring(start));return node[3][1]}}else if(tmp!==start&&node[3][0]!==null){params.push(path.substring(start,tmp));tmp=f(node[3][0],path,params,tmp,len);if(tmp!==null)return tmp;params.pop()}}if(node[4]!==null){params.push(path.substring(start));return node[4]}return null};export default f;
|
package/match.d.ts
DELETED
package/match.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default (router)=>{let staticMap={};router[0].forEach((item)=>{staticMap[item[0].slice(1)]=item[1]});let rootNode=router[1];return rootNode===null?(path)=>staticMap[path]??null:(path,params)=>staticMap[path]??matchNode(rootNode,path,params,-1)};import matchNode from"./tree/matcher.js";
|