0.1.0 • Published 6 months ago

route-path-match v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

route-path-match - 路由路径匹配

安装

npm i -D route-path-match
# or
pnpm add -D route-path-match

示例

import parsePathTplToMatch from "route-path-match";
const matchParams = parsePathTplToMatch("/:group/:id");
const params = matchParams("/items/aaa");
if (params) {
  console.log("path matched", params);
  // params -> { group: 'items', id: 'aaa' }
} else {
  console.log("path unmatch");
}

路径模板规则

  • 其中 ( ) ? 等 3 个字符为正则的含义及用法,即分组与可选
  • 变量使用形如 :id::filePath 即以 :开头的变量名,前者匹配除 / 之外的字符,后者匹配任意字符
  • 指定变量的正则形如 :id<[0-9]+|all> 其中 < > 包裹的内容会被识别为该变量的正则表达式。其中的反斜杠需要转义,如 \\d+
  • 变量名只支持 驼峰形式,比如 :groupId
  • 路径模板使用变量时,会转换为正则,否则作字符串全等匹配

路径模板示例

  • /assets/::filePath
  • /:group/:id
  • /:group/:id<0-9+>
  • /:group/:id<0-9+>(:extname<.html|.xml>)?
  • /:group/:id?
  • /:group(/:id)?
  • /:group<items|users|images>(/:id)?
0.1.0

6 months ago