microsoft / microsoft/TypeScript
Capitalize does not work for characters which are 2 utf-16 code units
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.4k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
🔎 Search Terms
Capitalize, utf-16, string, type
🕗 Version & Regression Information
- This is known to not work in 4.1.5 through 5.5.2 and current nightly (impacts all version with
Capitalize)
⏯ Playground Link
💻 Code
/**
* Capitalize a string.
*/
function capitalize<S extends string>(s: S): Capitalize<S> {
// To avoid splitting characters which are made of multiple UTF-16 code units,
// use iteration instead of indexing to separate the first character.
const iterated = s[Symbol.iterator]().next();
if (iterated.done === true) {
// Empty string case.
return "" as Capitalize<S>;
}
return (iterated.value.toUpperCase() + s.slice(iterated.value.length)) as Capitalize<S>;
}
const data: [string, string][] = [
["", ""],
["a", "A"],
["aa", "Aa"],
// Non-ascii
["ñx", "Ñx"],
// Lowercase letter that is 2 UTF-16 code units:
["𞥃", "𞤡"],
["𞥃a", "𞤡a"],
];
for (const [input, expected] of data) {
console.log(`input: "${input}", result: "${capitalize(input)}", expected: "${expected}"`);
}
type Test<Input extends string, Expected extends Capitalize<Input>> = Capitalize<Input>;
type _1 = Test<"", "">;
type _2 = Test<"a", "A">;
type _3 = Test<"aa", "Aa">;
type _4 = Test<"ñx", "Ñx">;
// These cases don't work.
type _5 = Test<"𞥃", "𞤡">;
type _6 = Test<"𞥃a", "𞤡a">;
🙁 Actual behavior
Capitalize appears to capitalize theString[0] which is not a correct implementation due to mishandling of characters which require two utf-16 code units and thus take up two indexes in the string.
For example, Capitalize<"𞥃"> gives "𞥃";
🙂 Expected behavior
When the first character in the string passed to Capitalize is in the Unicode "Lowercase Letter" category, that character should be replaced with its corresponding uppercase version.
For example, Capitalize<"𞥃"> should give"𞤡";
Additional information about the issue
The included code provides a tested correct implementation for runtime use. This implementation is taken from my work here: https://github.com/microsoft/FluidFramework/blob/ddee06b429b26b571b7383d4acd95a62d3b9758c/experimental/dds/tree2/src/util/utils.ts#L394
That code is available under the MIT license and already copyright Microsoft, so feel free to use it in the TypeScript implementation if you find it useful to do so.
This will be a breaking change, but I highly doubt it will impact real use.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground で報告されたケースを再現し、次に組み込みの Capitalize 型のコンパイラー実装と型テストを見つけてください。補助平面の小文字の例に対するリグレッションテストのカバレッジを追加し、空、ASCII、非 ASCII のケースが引き続き期待される型になることを確認してください。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 42/100