Skip to content

Commit 9beeb9c

Browse files
Merge pull request #44 from polygonplanet/feature/add-fallback-error-option
feat: add `error` fallback option in Encoding.convert
2 parents e9edbed + f6422f7 commit 9beeb9c

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-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) の付加を指定できます。

src/encoding-convert.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,6 +1673,8 @@ function handleFallback(results, bytes, fallbackOption) {
16731673
results[results.length] = 0x3B; // ;
16741674
}
16751675
break;
1676+
case 'error':
1677+
throw new Error('Character cannot be represented: [' + bytes.join(', ') + ']');
16761678
case 'ignore':
16771679
break;
16781680
}

tests/test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,41 @@ describe('encoding', function() {
680680
assert.deepEqual(decoded, '寿司ビール');
681681
});
682682
});
683+
684+
describe('Raise an Error when characters cannot be represented', function() {
685+
it('SJIS', function() {
686+
var fn = function() {
687+
encoding.convert(utf8, {
688+
to: 'sjis',
689+
from: 'utf-8',
690+
fallback: 'error'
691+
});
692+
};
693+
assert.throws(fn, Error, 'Character cannot be represented: [240, 159, 141, 163]');
694+
});
695+
696+
it('EUC-JP', function() {
697+
var fn = function() {
698+
encoding.convert(utf8, {
699+
to: 'euc-jp',
700+
from: 'utf-8',
701+
fallback: 'error'
702+
});
703+
};
704+
assert.throws(fn, Error, 'Character cannot be represented: [240, 159, 141, 163]');
705+
});
706+
707+
it('JIS', function() {
708+
var fn = function() {
709+
encoding.convert(utf8, {
710+
to: 'jis',
711+
from: 'utf-8',
712+
fallback: 'error'
713+
});
714+
};
715+
assert.throws(fn, Error, 'Character cannot be represented: [240, 159, 141, 163]');
716+
});
717+
});
683718
});
684719
});
685720

0 commit comments

Comments
 (0)