Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add error fallback option in Encoding.convert #44

Merged
merged 3 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Convert and detect character encoding in JavaScript.
+ [Specify the return type by the `type` option](#specify-the-return-type-by-the-type-option)
+ [Replacing characters with HTML entities when they cannot be represented](#replacing-characters-with-html-entities-when-they-cannot-be-represented)
+ [Ignoring characters when they cannot be represented](#ignoring-characters-when-they-cannot-be-represented)
+ [Raising an Error when they cannot be represented](#raising-an-error-when-they-cannot-be-represented)
+ [Specify BOM in UTF-16](#specify-bom-in-utf-16)
* [urlEncode : Encodes to percent-encoded string](#encodingurlencode-data)
* [urlDecode : Decodes from percent-encoded string](#encodingurldecode-string)
Expand Down Expand Up @@ -430,6 +431,24 @@ sjisArray = Encoding.convert(unicodeArray, {
console.log(sjisArray); // Converted to a code array of '寿司ビール'
```

#### Raising an Error when they cannot be represented

If you need to throw an error when a character cannot be represented in the target character encoding,
specify `error` as a `fallback` option. This will cause an exception to be thrown.

```javascript
const unicodeArray = Encoding.stringToCode('おにぎり🍙ラーメン🍜');
try {
const sjisArray = Encoding.convert(unicodeArray, {
to: 'SJIS',
from: 'UNICODE',
fallback: 'error' // Specify 'error' to throw an exception
});
} catch (e) {
console.error(e); // Error: Character cannot be represented: [240, 159, 141, 153]
}
```

#### Specify BOM in UTF-16

You can add a BOM (byte order mark) by specifying the `bom` option when converting to `UTF16`.
Expand Down
18 changes: 18 additions & 0 deletions README_ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ JavaScript で文字コードの変換や判定をします。
+ [`type` オプションで戻り値の型を指定する](#type-オプションで戻り値の型を指定する)
+ [変換できない文字を HTML エンティティ (HTML 数値文字参照) に置き換える](#変換できない文字を-html-エンティティ-html-数値文字参照-に置き換える)
+ [変換できない文字を無視する](#変換できない文字を無視する)
+ [変換できない文字が含まれている場合にエラーを発生させる](#変換できない文字が含まれている場合にエラーを発生させる)
+ [UTF-16 に BOM をつける](#utf-16-に-bom-をつける)
* [urlEncode : 文字コードの配列をURLエンコードする](#encodingurlencode-data)
* [urlDecode : 文字コードの配列にURLデコードする](#encodingurldecode-string)
Expand Down Expand Up @@ -420,6 +421,23 @@ sjisArray = Encoding.convert(unicodeArray, {
console.log(sjisArray); // '寿司ビール' の数値配列に変換されます
```

#### 変換できない文字が含まれている場合にエラーを発生させる

`fallback` オプションに `error` を指定すると、変換先の文字コードで表現できない文字が含まれている場合にエラーが発生し、例外が投げられます。

```javascript
const unicodeArray = Encoding.stringToCode('おにぎり🍙ラーメン🍜');
try {
const sjisArray = Encoding.convert(unicodeArray, {
to: 'SJIS',
from: 'UNICODE',
fallback: 'error' // 'error'を指定
});
} catch (e) {
console.error(e); // Error: Character cannot be represented: [240, 159, 141, 153]
}
```

#### UTF-16 に BOM をつける

`UTF16` に変換する際に `bom` オプションを指定すると BOM (byte order mark) の付加を指定できます。
Expand Down
2 changes: 2 additions & 0 deletions src/encoding-convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,8 @@ function handleFallback(results, bytes, fallbackOption) {
results[results.length] = 0x3B; // ;
}
break;
case 'error':
throw new Error('Character cannot be represented: [' + bytes.join(', ') + ']');
case 'ignore':
break;
}
Expand Down
35 changes: 35 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,41 @@ describe('encoding', function() {
assert.deepEqual(decoded, '寿司ビール');
});
});

describe('Raise an Error when characters cannot be represented', function() {
it('SJIS', function() {
var fn = function() {
encoding.convert(utf8, {
to: 'sjis',
from: 'utf-8',
fallback: 'error'
});
};
assert.throws(fn, Error, 'Character cannot be represented: [240, 159, 141, 163]');
});

it('EUC-JP', function() {
var fn = function() {
encoding.convert(utf8, {
to: 'euc-jp',
from: 'utf-8',
fallback: 'error'
});
};
assert.throws(fn, Error, 'Character cannot be represented: [240, 159, 141, 163]');
});

it('JIS', function() {
var fn = function() {
encoding.convert(utf8, {
to: 'jis',
from: 'utf-8',
fallback: 'error'
});
};
assert.throws(fn, Error, 'Character cannot be represented: [240, 159, 141, 163]');
});
});
});
});

Expand Down