Skip to content

shared: alphabeticalComparator が percent-encode された文字の大文字小文字を畳まず、エンコードの有無で並びが変わる #976

Open

Description

@YusukeHirao

As is

@d-zero/shared の alphabeticalComparator(packages/@d-zero/shared/src/sort/alphabetical.ts、公開版 0.23.0)は、大文字小文字を区別せずに比べる。ただし、percent-encode された文字だけは大文字小文字が畳まれない。

再現(ビルド済みの dist/sort/alphabetical.js を Node 26 で読み込み):

入力 結果 期待
alphabeticalComparator('A', 'a') 0 0
alphabeticalComparator('%41', 'a') -1 0
alphabeticalComparator('Ä', 'ä') 0 0
alphabeticalComparator('%C3%84', 'ä') -1 0

同じ文字列でも、エンコードされているかどうかで比較結果が変わる。

To be

エンコードの有無にかかわらず、デコード後の文字列を大文字小文字を区別せずに比べる。alphabeticalComparator('%41', 'a') と alphabeticalComparator('%C3%84', 'ä') が 0 を返す。

Why

  • 関数は、比較前に小文字化とデコードの両方を行っている。ここから、意図は「デコード後の文字列を、大文字小文字を区別せずに比べる」ことだと読める。今の順序(小文字化 → デコード)では、エスケープ(%41 の 41)だけが小文字化され、デコードで現れた文字(A)は小文字化されない
  • decodeURI は予約文字以外のエスケープ(%41 → A など)をデコードする(ECMAScript decodeURI: https://tc39.es/ecma262/#sec-decodeuri-encodeduri )。そのため、URL の表記揺れ(/%41bout と /about)がそのまま並び順の違いになる
  • 利用箇所は sort/path.ts(pathComparator。ホスト名・プロトコル・パスの比較)と sort/numerical.ts。sortUrl などの URL 並べ替えを使う利用側(Nitpicker のクロール時のページ順など)に波及する

How

  • packages/@d-zero/shared/src/sort/alphabetical.ts: decodeURISafely(a.toLowerCase()) を decodeURISafely(a).toLowerCase() に変える(b も同じ)
  • packages/@d-zero/shared/src/sort/alphabetical.spec.ts: 上の表のケースを追加する。現在の「lowercases the escapes, then compares the decoded form」のテストは、新しい順序に合わせて書き直す
  • 並び順が変わるので、sort/path.spec.ts と、sortUrl / pathComparator を使う利用側のテストの期待値に影響がないか確認する。挙動の変更として、コミット本文に before → after を書く

Activity

  1. added
    bug
    on Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions