Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[Feature] Allow optional permutations when exclusive matchFilters are present

未关闭
#355 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
25/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
typescript
领域
frontend

调研方向

从 issue 中展示的 expandOptionals 函数开始,跟踪其生成的路径如何与 matchFilters 组合。将当前的有序展开与示例中显式排列的路径数组进行比较。当互斥的 matchFilters 能够启用等效的可选排列,而无需调用方手动列出每条路径时,即视为完成。

由索引模型根据 Issue 内容生成。

描述

enhancement
Describe the bug

I was having issues with a routing setup where I have multiple optionals in a row in a path, but each has exclusive matchFilters to differentiate them. I looked through the code and found:

export function expandOptionals(pattern: string): string[] {
  let match = /(\/?\:[^\/]+)\?/.exec(pattern);
  if (!match) return [pattern];

  let prefix = pattern.slice(0, match.index);
  let suffix = pattern.slice(match.index + match[0].length);
  const prefixes: string[] = [prefix, (prefix += match[1])];

  // This section handles adjacent optional params. We don't actually want all permuations since
  // that will lead to equivalent routes which have the same number of params. For example
  // `/:a?/:b?/:c`? only has the unique expansion: `/`, `/:a`, `/:a/:b`, `/:a/:b/:c` and we can
  // discard `/:b`, `/:c`, `/:b/:c` by building them up in order and not recursing. This also helps
  // ensure predictability where earlier params have precidence.
  while ((match = /^(\/\:[^\/]+)\?/.exec(suffix))) {
    prefixes.push((prefix += match[1]));
    suffix = suffix.slice(match[0].length);
  }

  return expandOptionals(suffix).reduce<string[]>(
    (results, expansion) => [...results, ...prefixes.map(p => p + expansion)],
    []
  );
}

And while I understand the sentiment in the comment, I believe this design should be rethought for routes where there are exclusive matchFilters for each of the optionals. For example, the case where you have:

const matchFilters = { 
  a: ["hello","world"],
  b: ["foo","bar"],
  c: /^\d+$/
}
// ...
<Route path="/:a?/:b?/:c?" matchFilters={matchFilters} /* ... */ />
// ...

There is no reason why all permutations of this route shouldn't be available, as the matchFilters for all three routes are mutually exclusive. A workaround for this is to use an array of paths with only one optional per path. When used with matchFilters this technique allows the functionality, but requires manually permuting the list of optionals to create the array -- for example the above becomes:

<Route path={["/:a/:b/:c?", "/:a/:c?", "/:b/:c?", "/:c?"]} matchFilters={matchFilters} />

which works as described.

Your Example Website or App

n/a

Steps to Reproduce the Bug or Issue

nothing to reproduce -- current functionality is working as intended, would just be nice if matchFilters were taken into account when generating optional routes

Expected behavior

When matchFilters are present, allow permutations of optional parameters as long as the matchFilters for all parameters are mutually exclusive

Screenshots or Videos

No response

Platform

n/a

Additional context

No response

主要语言
TypeScript
星标
1.3k
派生
180
平均合并
1 天 13 小时
30 天内合并 PR
19

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

solidjs/solid-router 的其他 Issue

查看 solidjs/solid-router 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。