encoding-japanese
Advanced tools
Comparing version 2.1.0 to 2.2.0
{ | ||
"name": "encoding-japanese", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Convert and detect character encoding in JavaScript", | ||
@@ -57,3 +57,3 @@ "main": "src/index.js", | ||
"eslint": "^8.57.0", | ||
"mocha": "^10.3.0", | ||
"mocha": "^10.4.0", | ||
"package-json-versionify": "^1.0.4", | ||
@@ -60,0 +60,0 @@ "power-assert": "^1.6.1", |
@@ -30,3 +30,6 @@ encoding.js | ||
+ [Specify the return type by the `type` option](#specify-the-return-type-by-the-type-option) | ||
+ [Specify handling for unrepresentable characters](#specify-handling-for-unrepresentable-characters) | ||
+ [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) | ||
+ [Throwing an Error when they cannot be represented](#throwing-an-error-when-they-cannot-be-represented) | ||
+ [Specify BOM in UTF-16](#specify-bom-in-utf-16) | ||
@@ -122,3 +125,3 @@ * [urlEncode : Encodes to percent-encoded string](#encodingurlencode-data) | ||
```html | ||
<script src="https://unpkg.com/encoding-japanese@2.1.0/encoding.min.js"></script> | ||
<script src="https://unpkg.com/encoding-japanese@2.2.0/encoding.min.js"></script> | ||
``` | ||
@@ -368,12 +371,17 @@ | ||
#### Specify handling for unrepresentable characters | ||
With the `fallback` option, you can specify how to handle characters that cannot be represented in the target encoding. | ||
The `fallback` option supports the following values: | ||
* **html-entity**: Replace characters with HTML entities (decimal HTML numeric character references). | ||
* **html-entity-hex**: Replace characters with HTML entities (hexadecimal HTML numeric character references). | ||
* **ignore**: Ignore characters that cannot be represented. | ||
* **error**: Throw an error if any character cannot be represented. | ||
#### Replacing characters with HTML entities when they cannot be represented | ||
Characters that cannot be represented in the target character set are replaced with '?' (U+003F) by default, | ||
but by specifying the `fallback` option, you can replace them with HTML entities (Numeric character references), such as `🍣`. | ||
but by specifying `html-entity` as the `fallback` option, you can replace them with HTML entities (Numeric character references), such as `🍣`. | ||
The `fallback` option supports the following values. | ||
* **html-entity** : Replace to HTML entity (decimal HTML numeric character reference). | ||
* **html-entity-hex** : Replace to HTML entity (hexadecimal HTML numeric character reference). | ||
Example of specifying `{ fallback: 'html-entity' }` option: | ||
@@ -411,2 +419,46 @@ | ||
#### Ignoring characters when they cannot be represented | ||
By specifying `ignore` as a `fallback` option, characters that cannot be represented in the target encoding format can be ignored. | ||
Example of specifying `{ fallback: 'ignore' }` option: | ||
```javascript | ||
const unicodeArray = Encoding.stringToCode('寿司🍣ビール🍺'); | ||
// No fallback specified | ||
let sjisArray = Encoding.convert(unicodeArray, { | ||
to: 'SJIS', | ||
from: 'UNICODE' | ||
}); | ||
console.log(sjisArray); // Converted to a code array of '寿司?ビール?' | ||
// Specify `fallback: ignore` | ||
sjisArray = Encoding.convert(unicodeArray, { | ||
to: 'SJIS', | ||
from: 'UNICODE', | ||
fallback: 'ignore' | ||
}); | ||
console.log(sjisArray); // Converted to a code array of '寿司ビール' | ||
``` | ||
#### Throwing 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. | ||
Example of specifying `{ fallback: 'error' }` option: | ||
```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 | ||
@@ -419,5 +471,5 @@ | ||
const utf16Array = Encoding.convert(utf8Array, { | ||
to: 'UTF16', // to_encoding | ||
from: 'UTF8', // from_encoding | ||
bom: true // Add BOM | ||
to: 'UTF16', | ||
from: 'UTF8', | ||
bom: true // Specify to add the BOM | ||
}); | ||
@@ -431,5 +483,5 @@ ``` | ||
const utf16leArray = Encoding.convert(utf8Array, { | ||
to: 'UTF16', // to_encoding | ||
from: 'UTF8', // from_encoding | ||
bom: 'LE' // With BOM (little-endian) | ||
to: 'UTF16', | ||
from: 'UTF8', | ||
bom: 'LE' // Specify to add the BOM as little-endian | ||
}); | ||
@@ -436,0 +488,0 @@ ``` |
@@ -1675,3 +1675,8 @@ var config = require('./config'); | ||
} | ||
break; | ||
case 'error': | ||
throw new Error('Character cannot be represented: [' + bytes.join(', ') + ']'); | ||
case 'ignore': | ||
break; | ||
} | ||
} |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
854283
11324
832