@@ -29,6 +29,7 @@ Convert and detect character encoding in JavaScript.
29
29
+ [ Specify the return type by the ` type ` option] ( #specify-the-return-type-by-the-type-option )
30
30
+ [ Replacing characters with HTML entities when they cannot be represented] ( #replacing-characters-with-html-entities-when-they-cannot-be-represented )
31
31
+ [ 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 )
32
33
+ [ Specify BOM in UTF-16] ( #specify-bom-in-utf-16 )
33
34
* [ urlEncode : Encodes to percent-encoded string] ( #encodingurlencode-data )
34
35
* [ urlDecode : Decodes from percent-encoded string] ( #encodingurldecode-string )
@@ -430,6 +431,24 @@ sjisArray = Encoding.convert(unicodeArray, {
430
431
console .log (sjisArray); // Converted to a code array of '寿司ビール'
431
432
```
432
433
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
+
433
452
#### Specify BOM in UTF-16
434
453
435
454
You can add a BOM (byte order mark) by specifying the ` bom ` option when converting to ` UTF16 ` .
0 commit comments