@mapl/router 0.4.15 → 0.5.0

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.
@@ -1,4 +1,3 @@
1
- import type { Compiler } from '../tree/compiler.js';
2
- import { type Router } from './index.js';
3
- declare const _default: (router: Router<string>, compile: Compiler, methodInput: string, parsePath: string, startIndex: 0 | 1) => string;
1
+ import type { Router } from './index.js';
2
+ declare const _default: (router: Router<string>, methodInput: string, parsePath: string, startIndex: 0 | 1) => string;
4
3
  export default _default;
@@ -1 +1 @@
1
- export default (router,compile,methodInput,parsePath,startIndex)=>{let str="switch("+methodInput+"){";for(let key in router)str+='case"'+key+'":{'+parsePath+compilePath(router[key],compile,startIndex)+"break}";str+="}";let all=router[ALL];if(all!=null)str+=parsePath+compilePath(all,compile,startIndex);return str};import{ALL}from"./index.js";import compilePath from"../path/compiler.js";
1
+ export default (router,methodInput,parsePath,startIndex)=>{let str="switch("+methodInput+"){";let all="";for(let key in router){if(key==="")all=parsePath+compilePath(router[""],startIndex);else str+='case"'+key+'":{'+parsePath+compilePath(router[key],startIndex)+"break}"}return str+"}"+all};import compilePath from"../path/compiler.js";
package/method/index.d.ts CHANGED
@@ -1,6 +1,3 @@
1
1
  import type { Router as PathRouter } from '../path/index.js';
2
- export declare const ALL: unique symbol;
3
- export type ALL = typeof ALL;
4
- export type Method = string | ALL;
5
- export type Router<T = unknown> = Partial<Record<Method, PathRouter<T>>>;
6
- export declare const insertItem: <T>(router: Router<T>, method: Method, path: string, item: T) => void;
2
+ export type Router<T = unknown> = Partial<Record<string, PathRouter<T>>>;
3
+ export declare const insertItem: <T>(router: Router<T>, method: string, path: string, item: T) => void;
package/method/index.js CHANGED
@@ -1 +1 @@
1
- import{createRouter as createPathRouter,insertItem as insertItemToPath}from"../path/index.js";export var ALL=Symbol();export var insertItem=(router,method,path,item)=>{insertItemToPath(router[method]??=createPathRouter(),path,item)};
1
+ import{createRouter as createPathRouter,insertItem as insertItemToPath}from"../path/index.js";export var insertItem=(router,method,path,item)=>{insertItemToPath(router[method]??=createPathRouter(),path,item)};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapl/router",
3
- "version": "0.4.15",
3
+ "version": "0.5.0",
4
4
  "main": "./index.js",
5
5
  "devDependencies": {
6
6
  "@stylistic/eslint-plugin": "latest",
@@ -1,4 +1,3 @@
1
1
  import type { Router } from './index.js';
2
- import type { Compiler } from '../tree/compiler.js';
3
- declare const _default: (router: Router<string>, compile: Compiler, startIndex: 0 | 1) => string;
2
+ declare const _default: (router: Router<string>, startIndex: 0 | 1) => string;
4
3
  export default _default;
package/path/compiler.js CHANGED
@@ -1 +1 @@
1
- export default (router,compile,startIndex)=>router[0].map((pair)=>'if(p==="'+pair[0].slice(startIndex)+'"){'+pair[1]+"}").join("")+(router[1]===null?"":"let l=p.length;"+compile(router[1],0,-startIndex,""));
1
+ export default (router,startIndex)=>{let str="";for(let i=0,pairs=router[0];i<pairs.length;i++)str+='if(p==="'+pairs[i][0].slice(startIndex)+'"){'+pairs[i][1]+"}";return str+(router[1]===null?"":"let l=p.length;"+compile(router[1],0,-startIndex,""))};import{compile}from"../tree/compiler.js";
@@ -1,3 +1,2 @@
1
1
  import type { Node } from './node.js';
2
- export type Compiler = (node: Node<string>, paramCount: number, startIndexValue: number, startIndexPrefix: string) => string;
3
- export declare const o2: Compiler;
2
+ export declare const compile: (node: Node<string>, paramCount: number, startIndexValue: number, startIndexPrefix: string) => string;
package/tree/compiler.js CHANGED
@@ -1 +1 @@
1
- let toChar=(c)=>String.fromCharCode(+c[0]);export var o2=(node,paramCount,startIndexValue,startIndexPrefix)=>{let builder="";if(node[0].length!==1){let part=node[0];let start=startIndexPrefix+(startIndexValue+1);builder+="if(p"+(part.length===2?"["+start+']==="'+part[1]+'"':'.startsWith("'+part.slice(1)+'",'+start+")")+"){";startIndexValue+=part.length}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["+startIndexPrefix+startIndexValue+']==="'+toChar(childrenEntries[0])+'"){'+o2(childrenEntries[0][1],paramCount,startIndexValue,startIndexPrefix)+"}"}else{builder+="switch(p["+startIndexPrefix+startIndexValue+"]){";for(let i=0;i<childrenEntries.length;i++){builder+='case"'+toChar(childrenEntries[i])+'":'+o2(childrenEntries[i][1],paramCount,startIndexValue,startIndexPrefix)+"break;"}builder+="}"}}if(node[3]!==null){let params=node[3];let hasStore=params[1]!==null;let hasChild=params[0]!==null;builder+="{";if(paramCount>0)builder+=(paramCount>1?"":"let ")+"i="+startIndexPrefix+startIndexValue+";";let currentIndex=paramCount>0?"i":startIndexPrefix+startIndexValue;let slashIndex='p.indexOf("/"'+(currentIndex==="0"?"":","+currentIndex)+")";if(hasChild||!hasStore)builder+=(paramCount>0?"":"let ")+"j="+slashIndex+";";if(hasStore){let paramsVal=currentIndex==="0"?"p":"p.slice("+currentIndex+")";builder+="if("+(hasChild?"j":slashIndex)+"===-1){let q"+paramCount+"="+paramsVal+";"+params[1]+"}"}if(hasChild){let paramsVal="p.slice("+currentIndex+",j)";builder+="if(j>"+currentIndex+"){let q"+paramCount+"="+paramsVal+";"+o2(params[0],paramCount+1,0,"j+")+"}"}builder+="}"}if(node[4]!==null){let noStore=node[1]===null;let currentIndex=startIndexPrefix+startIndexValue;let paramsVal=currentIndex==="0"?"p":"p.slice("+currentIndex+")";let body="let q"+paramCount+"="+paramsVal+";"+node[4];builder+=noStore?"if(l>"+currentIndex+"){"+body+"}":body}return node[0].length!==1?builder+"}":builder};
1
+ let toChar=(c)=>String.fromCharCode(+c[0]);export var compile=(node,paramCount,startIndexValue,startIndexPrefix)=>{let builder="";if(node[0].length!==1){let part=node[0];let start=startIndexPrefix+(startIndexValue+1);builder+="if(p"+(part.length===2?"["+start+']==="'+part[1]+'"':'.startsWith("'+part.slice(1)+'",'+start+")")+"){";startIndexValue+=part.length}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["+startIndexPrefix+startIndexValue+']==="'+toChar(childrenEntries[0])+'"){'+compile(childrenEntries[0][1],paramCount,startIndexValue,startIndexPrefix)+"}"}else{builder+="switch(p["+startIndexPrefix+startIndexValue+"]){";for(let i=0;i<childrenEntries.length;i++){builder+='case"'+toChar(childrenEntries[i])+'":'+compile(childrenEntries[i][1],paramCount,startIndexValue,startIndexPrefix)+"break;"}builder+="}"}}if(node[3]!==null){let params=node[3];let hasStore=params[1]!==null;let hasChild=params[0]!==null;builder+="{";if(paramCount>0)builder+=(paramCount>1?"":"let ")+"i="+startIndexPrefix+startIndexValue+";";let currentIndex=paramCount>0?"i":startIndexPrefix+startIndexValue;let slashIndex='p.indexOf("/"'+(currentIndex==="0"?"":","+currentIndex)+")";if(hasChild||!hasStore)builder+=(paramCount>0?"":"let ")+"j="+slashIndex+";";if(hasStore){let paramsVal=currentIndex==="0"?"p":"p.slice("+currentIndex+")";builder+="if("+(hasChild?"j":slashIndex)+"===-1){let q"+paramCount+"="+paramsVal+";"+params[1]+"}"}if(hasChild){let paramsVal="p.slice("+currentIndex+",j)";builder+="if(j>"+currentIndex+"){let q"+paramCount+"="+paramsVal+";"+compile(params[0],paramCount+1,0,"j+")+"}"}builder+="}"}if(node[4]!==null){let noStore=node[1]===null;let currentIndex=startIndexPrefix+startIndexValue;let paramsVal=currentIndex==="0"?"p":"p.slice("+currentIndex+")";let body="let q"+paramCount+"="+paramsVal+";"+node[4];builder+=noStore?"if(l>"+currentIndex+"){"+body+"}":body}return node[0].length!==1?builder+"}":builder};
package/tree/node.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import type { PathTransformResult } from '../transform.js';
2
1
  export type Node<T = unknown> = [
3
2
  part: string,
4
3
  store: T | null,
@@ -15,5 +14,4 @@ export declare const createParamNode: (nextNode: ParamNode[0]) => ParamNode;
15
14
  export declare const cloneNode: (node: Node, part: string) => Node;
16
15
  export declare const resetNode: (node: Node, part: string, children: Node[2]) => void;
17
16
  export declare const visitNode: (node: Node, parts: string[]) => Node;
18
- export declare const insertItemWithParts: <T>(node: Node<T>, result: PathTransformResult, item: T) => void;
19
17
  export declare const insertItem: <T>(node: Node<T>, path: string, item: T) => void;
package/tree/node.js CHANGED
@@ -1 +1 @@
1
- export var createNode=(part)=>[part,null,null,null,null];export var createParamNode=(nextNode)=>[nextNode,null];export var cloneNode=(node,part)=>[part,node[1],node[2],node[3],node[4]];export var resetNode=(node,part,children)=>{node[0]=part;node[2]=children;node[1]=null;node[3]=null;node[4]=null};export var visitNode=(node,parts)=>{for(let i=0;i<parts.length;++i){let pathPart=parts[i];if(i!==0){if(node[3]===null){let nextNode=createNode(pathPart);node[3]=createParamNode(nextNode);node=nextNode}else node=node[3][0]??=createNode(pathPart)}for(let j=0;;++j){let nodePart=node[0];if(j===pathPart.length){if(j<nodePart.length){let children=[];children[nodePart.charCodeAt(j)]=cloneNode(node,nodePart.slice(j));resetNode(node,pathPart,children)}break}if(j===nodePart.length){if(node[2]===null)node[2]=[];else{let nextNode=node[2][pathPart.charCodeAt(j)];if(nextNode!=null){node=nextNode;pathPart=pathPart.slice(j);j=0;continue}}let nextNode=createNode(pathPart.slice(j));node[2][pathPart.charCodeAt(j)]=nextNode;node=nextNode;break}if(pathPart[j]!==nodePart[j]){let children=[];children[nodePart.charCodeAt(j)]=cloneNode(node,nodePart.slice(j));let nextNode=createNode(pathPart.slice(j));children[pathPart.charCodeAt(j)]=nextNode;resetNode(node,nodePart.substring(0,j),children);node=nextNode;break}}}return node};export var insertItemWithParts=(node,result,item)=>{let nextNode=visitNode(node,result[1]);switch(result[2]){case 0:nextNode[1]=item;break;case 1:if(result[1].length===result[0].length)(nextNode[3]??=createParamNode(null))[1]=item;else nextNode[1]=item;break;case 2:nextNode[4]=item}};export var insertItem=(node,path,item)=>{if(path.charCodeAt(path.length-1)===42){if(path.charCodeAt(path.length-2)===42)visitNode(node,path.substring(0,path.length-2).split("*"))[4]=item;else(visitNode(node,path.substring(0,path.length-1).split("*"))[3]??=createParamNode(null))[1]=item}else visitNode(node,path.split("*"))[1]=item};
1
+ export var createNode=(part)=>[part,null,null,null,null];export var createParamNode=(nextNode)=>[nextNode,null];export var cloneNode=(node,part)=>[part,node[1],node[2],node[3],node[4]];export var resetNode=(node,part,children)=>{node[0]=part;node[2]=children;node[1]=null;node[3]=null;node[4]=null};export var visitNode=(node,parts)=>{for(let i=0;i<parts.length;++i){let pathPart=parts[i];if(i!==0){if(node[3]===null){let nextNode=createNode(pathPart);node[3]=createParamNode(nextNode);node=nextNode}else node=node[3][0]??=createNode(pathPart)}for(let j=0;;++j){let nodePart=node[0];if(j===pathPart.length){if(j<nodePart.length){let children=[];children[nodePart.charCodeAt(j)]=cloneNode(node,nodePart.slice(j));resetNode(node,pathPart,children)}break}if(j===nodePart.length){if(node[2]===null)node[2]=[];else{let nextNode=node[2][pathPart.charCodeAt(j)];if(nextNode!=null){node=nextNode;pathPart=pathPart.slice(j);j=0;continue}}let nextNode=createNode(pathPart.slice(j));node[2][pathPart.charCodeAt(j)]=nextNode;node=nextNode;break}if(pathPart[j]!==nodePart[j]){let children=[];children[nodePart.charCodeAt(j)]=cloneNode(node,nodePart.slice(j));let nextNode=createNode(pathPart.slice(j));children[pathPart.charCodeAt(j)]=nextNode;resetNode(node,nodePart.substring(0,j),children);node=nextNode;break}}}return node};export var insertItem=(node,path,item)=>{if(path.charCodeAt(path.length-1)===42){if(path.charCodeAt(path.length-2)===42)visitNode(node,path.substring(0,path.length-2).split("*"))[4]=item;else(visitNode(node,path.substring(0,path.length-1).split("*"))[3]??=createParamNode(null))[1]=item}else visitNode(node,path.split("*"))[1]=item};
@@ -1,5 +0,0 @@
1
- import type { Matcher as PathMatcher } from '../path/matcher.js';
2
- import { type Router } from './index.js';
3
- export type Matcher<T> = [Map<string, PathMatcher<T>>, PathMatcher<T> | null];
4
- declare const _default: <T>(router: Router<T>, startIndex: 0 | -1) => Matcher<T>;
5
- export default _default;
package/method/matcher.js DELETED
@@ -1 +0,0 @@
1
- export default (router,startIndex)=>[new Map(Object.entries(router).map((pair)=>[pair[0],compileMatch(pair[1],startIndex)])),router[ALL]==null?null:compileMatch(router[ALL],startIndex)];import compileMatch from"../path/matcher.js";import{ALL}from"./index.js";
package/path/matcher.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import type { Router } from './index.js';
2
- export type Matcher<T = unknown> = [Map<string, T>, (path: string, params: string[]) => T | null];
3
- declare const _default: <T>(router: Router<T>, startIndex: 0 | -1) => Matcher<T>;
4
- export default _default;
package/path/matcher.js DELETED
@@ -1 +0,0 @@
1
- export default (router,startIndex)=>{let node=router[1];return[new Map(router[0]),node==null?noop:(path,params)=>match(node,path,params,startIndex)]};import match from"../tree/matcher.js";let noop=(_,_1)=>null;
package/quick-match.d.ts DELETED
@@ -1,2 +0,0 @@
1
- declare const _default: (pattern: string, path: string) => string[] | null;
2
- export default _default;
package/quick-match.js DELETED
@@ -1 +0,0 @@
1
- export default (pattern,path)=>{let params=[];let offset=0;for(let i=1,tmp;i<pattern.length;i++){tmp=pattern.charCodeAt(i);if(tmp===42){if(i===pattern.length-1){if(path.includes("/",i+offset))return null;params.push(path.slice(i+offset));return params}if(pattern.charCodeAt(i+1)===47){tmp=path.indexOf("/",i+offset);if(tmp===-1)return null;params.push(path.slice(i+offset,tmp));offset+=tmp-(i+offset)-1;continue}params.push(path.slice(i+offset));return params}if(tmp!==path.charCodeAt(i+offset))return null}return offset+pattern.length===path.length?params:null};
package/tree/matcher.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import type { Node } from './node.js';
2
- /**
3
- * Match a pathname and returns the result
4
- */
5
- declare const f: <T>(node: Node<T>, path: string, params: string[], start: number) => T | null;
6
- export default f;
package/tree/matcher.js DELETED
@@ -1 +0,0 @@
1
- let f=(node,path,params,start)=>{let part=node[0];let tmp=part.length;if(tmp!==1&&(start+tmp>path.length||!path.startsWith(part,start)))return null;start+=tmp;if(start===path.length&&node[1]!==null)return node[1];if(node[2]!==null){tmp=node[2][path.charCodeAt(start)];if(tmp!=null){tmp=f(tmp,path,params,start);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);if(tmp!==null)return tmp;params.pop()}}if(node[4]!==null){params.push(path.substring(start));return node[4]}return null};export default f;