Skip to content

Commit f6422f7

Browse files
committed
docs: add fallback: 'error' option
1 parent 0891915 commit f6422f7

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Convert and detect character encoding in JavaScript.
2929
+ [Specify the return type by the `type` option](#specify-the-return-type-by-the-type-option)
3030
+ [Replacing characters with HTML entities when they cannot be represented](#replacing-characters-with-html-entities-when-they-cannot-be-represented)
3131
+ [Ignoring characters when they cannot be represented](#ignoring-characters-when-they-cannot-be-represented)
32+
+ [Raising an Error when they cannot be represented](#raising-an-error-when-they-cannot-be-represented)
3233
+ [Specify BOM in UTF-16](#specify-bom-in-utf-16)
3334
* [urlEncode : Encodes to percent-encoded string](#encodingurlencode-data)
3435
* [urlDecode : Decodes from percent-encoded string](#encodingurldecode-string)
@@ -430,6 +431,24 @@ sjisArray = Encoding.convert(unicodeArray, {
430431
console.log(sjisArray); // Converted to a code array of '寿司ビール'
431432
```
432433

434+
#### Raising an Error when they cannot be represented
435+
436+
If you need to throw an error when a character cannot be represented in the target character encoding,
437+
specify `error` as a `fallback` option. This will cause an exception to be thrown.
438+
439+
```javascript
440+
const unicodeArray = Encoding.stringToCode('おにぎり🍙ラーメン🍜');
441+
try {
442+
const sjisArray = Encoding.convert(unicodeArray, {
443+
to: 'SJIS',
444+
from: 'UNICODE',
445+
fallback: 'error' // Specify 'error' to throw an exception
446+
});
447+
} catch (e) {
448+
console.error(e); // Error: Character cannot be represented: [240, 159, 141, 153]
449+
}
450+
```
451+
433452
#### Specify BOM in UTF-16
434453

435454
You can add a BOM (byte order mark) by specifying the `bom` option when converting to `UTF16`.

README_ja.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ JavaScript で文字コードの変換や判定をします。
2929
+ [`type` オプションで戻り値の型を指定する](#type-オプションで戻り値の型を指定する)
3030
+ [変換できない文字を HTML エンティティ (HTML 数値文字参照) に置き換える](#変換できない文字を-html-エンティティ-html-数値文字参照-に置き換える)
3131
+ [変換できない文字を無視する](#変換できない文字を無視する)
32+
+ [変換できない文字が含まれている場合にエラーを発生させる](#変換できない文字が含まれている場合にエラーを発生させる)
3233
+ [UTF-16 に BOM をつける](#utf-16-に-bom-をつける)
3334
* [urlEncode : 文字コードの配列をURLエンコードする](#encodingurlencode-data)
3435
* [urlDecode : 文字コードの配列にURLデコードする](#encodingurldecode-string)
@@ -420,6 +421,23 @@ sjisArray = Encoding.convert(unicodeArray, {
420421
console.log(sjisArray); // '寿司ビール' の数値配列に変換されます
421422
```
422423

424+
#### 変換できない文字が含まれている場合にエラーを発生させる
425+
426+
`fallback` オプションに `error` を指定すると、変換先の文字コードで表現できない文字が含まれている場合にエラーが発生し、例外が投げられます。
427+
428+
```javascript
429+
const unicodeArray = Encoding.stringToCode('おにぎり🍙ラーメン🍜');
430+
try {
431+
const sjisArray = Encoding.convert(unicodeArray, {
432+
to: 'SJIS',
433+
from: 'UNICODE',
434+
fallback: 'error' // 'error'を指定
435+
});
436+
} catch (e) {
437+
console.error(e); // Error: Character cannot be represented: [240, 159, 141, 153]
438+
}
439+
```
440+
423441
#### UTF-16 に BOM をつける
424442

425443
`UTF16` に変換する際に `bom` オプションを指定すると BOM (byte order mark) の付加を指定できます。

0 commit comments

Comments
 (0)