@mapl/router 0.4.1 → 0.4.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/README.md CHANGED
@@ -10,4 +10,4 @@ A fast radix tree router.
10
10
  ```
11
11
 
12
12
  ## Limitation
13
- Patterns cannot include quotes and escape characters.
13
+ Patterns and methods cannot include quotes and escape characters.
@@ -0,0 +1,4 @@
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, startIndex: 0 | 1) => string;
4
+ export default _default;
@@ -0,0 +1 @@
1
+ export default (router,compile,methodInput,startIndex)=>{let str="switch("+methodInput+"){";for(let key in router)str+='case"'+key+'":'+compilePath(router[key],compile,startIndex)+"break;";str+="}";let all=router[ALL];if(all!=null)str+=compilePath(all,compile,startIndex);return str};import{ALL}from"./index.js";import compilePath from"../path/compiler.js";
@@ -0,0 +1,6 @@
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;
@@ -0,0 +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)};
@@ -0,0 +1,5 @@
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;
@@ -0,0 +1 @@
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapl/router",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "main": "./index.js",
5
5
  "devDependencies": {
6
6
  "@stylistic/eslint-plugin": "latest",
@@ -1,4 +1,4 @@
1
1
  import type { Router } from './index.js';
2
- import type { Compiler } from './tree/compiler.ts';
2
+ import type { Compiler } from '../tree/compiler.js';
3
3
  declare const _default: (router: Router<string>, compile: Compiler, startIndex: 0 | 1) => string;
4
4
  export default _default;
@@ -1,4 +1,4 @@
1
- import { type Node } from './tree/node.js';
1
+ import { type Node } from '../tree/node.js';
2
2
  export type Router<T = unknown> = [staticMap: [path: string, item: T][], root: Node<T> | null];
3
3
  export declare const createRouter: <T>() => Router<T>;
4
4
  export declare const insertItem: <T>(router: Router, path: string, item: T) => void;
package/path/index.js ADDED
@@ -0,0 +1 @@
1
+ import{createNode,insertItem as nodeInsertItem,insertItemWithParts as nodeInsertItemWithParts}from"../tree/node.js";export var createRouter=()=>[[],null];export var insertItem=(router,path,item)=>{if(path.includes("*"))nodeInsertItem(router[1]??=createNode("/"),path,item);else router[0].push([path,item])};export var insertItemWithParts=(router,parts,flag,item)=>{if(flag===0)router[0].push([parts[0],item]);else nodeInsertItemWithParts(router[1]??=createNode("/"),parts,flag,item)};export var countParams=(path)=>{let cnt=0;for(let i=-1;(i=path.indexOf("*",i+1))!==-1;cnt++);return cnt-(path.endsWith("**")?1:0)};
@@ -0,0 +1,4 @@
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;
@@ -0,0 +1 @@
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/transform.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ export type PathTransformResult = [
2
+ params: string[],
3
+ parts: string[],
4
+ flag: 0 | 1 | 2
5
+ ];
6
+ export type PathTransformer = (path: string) => PathTransformResult;
7
+ export type InferNormalRoute<T extends string> = T extends `${string}*${infer Next}` ? Next extends '*' ? [string] : [...InferNormalRoute<Next>, string] : [];
8
+ export type InferFSRoute<T extends string> = T extends `${string}[${infer Current}]${infer Rest}` ? Current extends `...${string}` ? [string] : [...InferFSRoute<Rest>, string] : [];
9
+ export declare const transformFSRoute: PathTransformer;
10
+ export type InferRoute<T extends string> = T extends `${string}:${infer Current}` ? Current extends `${string}/${infer Rest}` ? [...InferRoute<Rest>, string] : [string] : T extends '*' ? [string] : [];
11
+ export declare const transformRoute: PathTransformer;
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&&(start+tmp>path.length||path.indexOf(part,start)!==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;
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;
package/index.js DELETED
@@ -1 +0,0 @@
1
- import{createNode,insertItem as nodeInsertItem,insertItemWithParts as nodeInsertItemWithParts}from"./tree/node.js";export var createRouter=()=>[[],null];export var insertItem=(router,path,item)=>{if(path.includes("*"))nodeInsertItem(router[1]??=createNode("/"),path,item);else router[0].push([path,item])};export var insertItemWithParts=(router,parts,flag,item)=>{if(flag===0)router[0].push([parts[0],item]);else nodeInsertItemWithParts(router[1]??=createNode("/"),parts,flag,item)};export var countParams=(path)=>{let cnt=0;for(let i=-1;(i=path.indexOf("*",i+1))!==-1;cnt++);return cnt-(path.endsWith("**")?1:0)};
package/path.d.ts DELETED
@@ -1,12 +0,0 @@
1
- export type PathTransformResult = [
2
- params: string[],
3
- parts: string[],
4
- flag: 0 | 1 | 2
5
- ];
6
- export type PathTransformer = (path: string) => PathTransformResult;
7
- export type InferFSRoute<T extends string> = T extends `${string}[${infer Current}]${infer Rest}` ? Current extends `...${infer Name}` ? Record<Name, string> : Record<Current, string> & InferFSRoute<Rest> : {};
8
- export declare const transformFSRoute: PathTransformer;
9
- export type InferRoute<T extends string> = T extends `${string}:${infer Current}` ? Current extends `${infer Name}/${infer Rest}` ? Record<Name, string> & InferRoute<Rest> : Record<Current, string> : T extends '*' ? {
10
- '*': string;
11
- } : {};
12
- export declare const transformRoute: PathTransformer;
File without changes
File without changes