route-path-match
Version:
compile route path template, match route path params
44 lines (32 loc) • 1.26 kB
TypeScript
/**
匹配规则
- 其中 ( ) ? 等3个字符为正则的含义及用法,即分组与可选
- 变量使用形如 :id 或 ::id 前者匹配除/之外的字符,后者匹配任意字符
- 指定变量的正则形如 :id<[0-9]+|all> 其中 < > 字符会被替换为 ( ) 其中包裹的内容会被识别为该变量的正则表达式。其中的反斜杠需要转义,如 \\d+
- 变量名只支持使用驼峰形式,比如 :groupId
使用示例
- /assets/::filePath
- /:lang/:group/:gid
- /:lang/:group/:gid.json
- (/:lang)?/:group(/:gid)?
- (/:lang)?/:group/:gid(:ext<.js|.json>)?
*/
// 路径模板字符串 -> 路径匹配规则对象
export declare function parseToRule(pathTpl: string): PathMatchRule;
// 路径匹配规则对象 -> 路径参数对象
export declare function matchByRule(
pathMatchRule: PathMatchRule,
path: string
): PathParams | undefined;
// 路径模板字符串 -> 路径参数匹配方法
export default function (pathTpl: string): MatchPathParams;
export type MatchPathParams = (path: string) => PathParams | undefined;
export type PathMatchRule = {
raw: string;
flag: 'r' | '^' | '=';
value: RegExp | string;
names: string[];
};
export type PathParams = {
[key: string]: string | undefined;
};