ja-char-check 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Changelog
2
2
 
3
- ## 0.1.1 — Unreleased
3
+ ## 0.2.0 — Unreleased
4
+
5
+ - Report `basis: "width_variant"` for the `¥` → `¥` replacement and label it as a fullwidth yen sign in the demo. Consumers that branch on `ReplacementCandidate.basis` should handle the new value.
6
+ - Show the Shift_JIS and CP932 backslash byte counts in both READMEs and the demo. Document how `truncateByBytes` reports unsupported characters beyond the byte limit.
7
+
8
+ ## 0.1.1
4
9
 
5
10
  - Link all seven related projects to their public repositories and live demos from the Japanese and English READMEs and the demo.
6
11
  - Link the published ja-char-check demo from both READMEs and update the CDN examples for 0.1.1.
package/README.en.md CHANGED
@@ -8,7 +8,7 @@ TypeScript checks for Japanese character repertoires, encoding compatibility, an
8
8
 
9
9
  After npm publication, install with `npm install ja-char-check`. To try the project before publication, run `npm ci`, `npm run build`, and `npm pack` in this repository, then install the resulting `.tgz` in your project.
10
10
 
11
- After npm publication, a browser can import the ESM build from `https://unpkg.com/ja-char-check@0.1.1/dist/index.js` or `https://cdn.jsdelivr.net/npm/ja-char-check@0.1.1/dist/index.js` in a `type="module"` script. Pin the published version you use in the URL.
11
+ After npm publication, a browser can import the ESM build from `https://unpkg.com/ja-char-check@0.2.0/dist/index.js` or `https://cdn.jsdelivr.net/npm/ja-char-check@0.2.0/dist/index.js` in a `type="module"` script. Pin the published version you use in the URL.
12
12
 
13
13
  ## Usage
14
14
 
@@ -42,6 +42,8 @@ applyReplacements(input, options, (item) => item.utf16Index === index ? "~" :
42
42
 
43
43
  `unicode` checks Unicode scalar values. `jis-x-0208` checks character-set membership. The byte APIs accept `utf-8`, `shift_jis`, `cp932`, and `windows-31j` (an alias of `cp932`). Each legacy profile requires an exact code-point round trip. Byte measurement throws `CharacterEncodingError` for lossy mappings, unsupported characters, or lone surrogates. Truncation respects grapheme clusters by default. Grapheme counting and truncation at a grapheme boundary use `Intl.Segmenter`.
44
44
 
45
+ The backslash `\` (U+005C) takes two bytes under this library's `shift_jis` profile and one byte under `cp932`. Choose a profile that matches your output encoder's mapping. [Mapping sources and profiles](docs/mappings.md)
46
+
45
47
  Character positions are zero-based UTF-16 offsets and code-point indices. Replacements require per-occurrence approval and are limited to documented legacy mapping variants and selected Unicode compatibility forms. Name and place-name ideographs are preserved.
46
48
 
47
49
  ## Related libraries
package/README.md CHANGED
@@ -8,7 +8,7 @@
8
8
 
9
9
  npm公開後は `npm install ja-char-check` で導入できます。公開前に試す場合は、このリポジトリで `npm ci`、`npm run build`、`npm pack` を実行し、利用側のプロジェクトで生成された `.tgz` をインストールしてください。
10
10
 
11
- ブラウザで直接読み込む場合は、npm公開後に `https://unpkg.com/ja-char-check@0.1.1/dist/index.js` または `https://cdn.jsdelivr.net/npm/ja-char-check@0.1.1/dist/index.js` を、`type="module"` のスクリプトからインポートできます。URLのバージョンは利用する公開版に合わせてください。
11
+ ブラウザで直接読み込む場合は、npm公開後に `https://unpkg.com/ja-char-check@0.2.0/dist/index.js` または `https://cdn.jsdelivr.net/npm/ja-char-check@0.2.0/dist/index.js` を、`type="module"` のスクリプトからインポートできます。URLのバージョンは利用する公開版に合わせてください。
12
12
 
13
13
  ## 使い方
14
14
 
@@ -35,6 +35,8 @@ const result = checkCharacters("受付\n①", {
35
35
 
36
36
  `unicode` はUnicodeスカラー値、`jis-x-0208` は文字集合への所属を確認します。`utf-8`、`shift_jis`、`cp932`(別名 `windows-31j`)ではバイト数も計算できます。Shift_JISとCP932は別のマッピングを使います。判定では元のコードポイントへの往復を要求し、未対応文字や不正なサロゲートを含む入力のバイト測定では `CharacterEncodingError` が発生します。切り詰めは既定で書記素クラスタ境界を使います。書記素クラスタを扱う機能には `Intl.Segmenter` が必要です。
37
37
 
38
+ バックスラッシュ `\`(U+005C)は、このライブラリの `shift_jis` では2バイト、`cp932` では1バイトです。連携先のエンコーダーが使うマッピングに合わせて判定プロファイルを選んでください。 [文字コードとマッピング](docs/mappings.md)
39
+
38
40
  ## 置換候補を表示して承認する
39
41
 
40
42
  ```ts
@@ -7,7 +7,7 @@ export type ReplacementCandidate = {
7
7
  } | {
8
8
  replacement: string;
9
9
  kind: "unicode_compatibility";
10
- basis: "unicode_nfkc";
10
+ basis: "unicode_nfkc" | "width_variant";
11
11
  };
12
12
  export type ReplacementKind = ReplacementCandidate["kind"];
13
13
  export interface ReplacementOptions {
@@ -20,10 +20,12 @@ export function suggestReplacements(value, { encoding }) {
20
20
  candidates.push({ replacement, legacyCode, kind: "legacy_mapping_variant", basis: "shared_legacy_code" });
21
21
  }
22
22
  }
23
- // Offer NFKC decompositions for numeric symbols and the yen width pair.
24
- if (codePoint >= 0x2460 && codePoint <= 0x2473 || codePoint >= 0x2160 && codePoint <= 0x217f ||
25
- character === "¥" || character === "¥") {
26
- const replacement = character === "¥" ? "¥" : character.normalize("NFKC");
23
+ if (character === "¥" && isEncodable("¥", encoding)) {
24
+ candidates.push({ replacement: "¥", kind: "unicode_compatibility", basis: "width_variant" });
25
+ }
26
+ if ((codePoint >= 0x2460 && codePoint <= 0x2473) || (codePoint >= 0x2160 && codePoint <= 0x217f) ||
27
+ character === "¥") {
28
+ const replacement = character.normalize("NFKC");
27
29
  if (replacement !== character && isEncodable(replacement, encoding)) {
28
30
  candidates.push({ replacement, kind: "unicode_compatibility", basis: "unicode_nfkc" });
29
31
  }
package/docs/api.md CHANGED
@@ -72,13 +72,13 @@ checkCharacters("A\t", { encoding: "cp932", reportCategories: [], rejectCategori
72
72
  バイト系APIは入力全体を検証します。切り捨てる範囲の文字も含め、最初の変換不可位置は `CharacterEncodingError.issue` で確認できます。`unicode` と `jis-x-0208` は所属判定用のため、バイト系APIへの指定は `RangeError` になります。書記素クラスタの分割には実行環境の `Intl.Segmenter` が必要です。上限内で入力全体を返せる場合や、空の接頭辞を返す場合、切り詰めに書記素分割は発生しません。
73
73
 
74
74
  ```ts
75
- import { CharacterEncodingError, getByteLength, truncateByBytes } from "ja-char-check"
75
+ import { CharacterEncodingError, truncateByBytes } from "ja-char-check"
76
76
 
77
77
  truncateByBytes("Aか\u3099", 4, "utf-8") // "A"
78
78
  truncateByBytes("Aか\u3099", 4, "utf-8", { boundary: "code-point" }) // "Aか"
79
79
 
80
80
  try {
81
- getByteLength("A𠮷", "cp932")
81
+ truncateByBytes("A𠮷", 1, "cp932") // 上限外の「𠮷」で例外
82
82
  } catch (error) {
83
83
  if (error instanceof CharacterEncodingError) console.log(error.issue.utf16Index) // 1
84
84
  }
@@ -91,6 +91,7 @@ try {
91
91
  | `kind` | `basis` | 根拠 |
92
92
  | --- | --- | --- |
93
93
  | `legacy_mapping_variant` | `shared_legacy_code` | Shift_JISとCP932で同じバイト位置に割り当てられた異なる文字。`legacyCode` がその位置を示す。 |
94
- | `unicode_compatibility` | `unicode_nfkc` | 丸数字、ローマ数字のNFKC互換分解、または円記号の全角・半角の組。例では `①` から `1`、`Ⅻ` から `XII`。 |
94
+ | `unicode_compatibility` | `unicode_nfkc` | NFKCによる互換分解。例では `①` から `1`、`Ⅻ` から `XII`。 |
95
+ | `unicode_compatibility` | `width_variant` | 円記号の幅違い。`¥`(U+00A5)に対して `¥`(U+FFE5)を提案。 |
95
96
 
96
97
  `applyReplacements(value, { encoding }, approve)` は各出現箇所の候補を `approve` に渡します。コールバックはその出現箇所で承認した候補の文字列を返します。`undefined` は元の文字をそのまま使います。候補外の値には `RangeError`、コールバック省略には `TypeError` が発生します。位置は元入力上の値です。候補を持たない文字や未承認の文字は保持されるため、変換先への出力可否は置換後に `isEncodable` などで確認できます。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ja-char-check",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Tiny TypeScript utilities for Japanese character checks and legacy encoding byte limits.",
5
5
  "type": "module",
6
6
  "exports": {