Convert and detect character encoding in JavaScript
Installations
npm install encoding-japanese
Developer
polygonplanet
Developer Guide
Module System
CommonJS
Min. Node Version
>=8.10.0
Typescript Support
No
Node Version
18.18.0
NPM Version
9.8.1
Statistics
589 Stars
238 Commits
123 Forks
22 Watching
2 Branches
5 Contributors
Updated on 27 Nov 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
120,880,661
Last day
-4.6%
248,347
Compared to previous day
Last week
3.4%
1,432,767
Compared to previous week
Last month
7.9%
5,919,758
Compared to previous month
Last year
65%
51,986,137
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
encoding.js
Convert and detect character encoding in JavaScript.
Table of contents
- Features
- Installation
- Supported encodings
- Example usage
- Demo
- API
- detect : Detects character encoding
- convert : Converts character encoding
- Specify conversion options to the argument
to
as an object - Specify the return type by the
type
option - Specify handling for unrepresentable characters
- Replacing characters with HTML entities when they cannot be represented
- Ignoring characters when they cannot be represented
- Throwing an Error when they cannot be represented
- Specify BOM in UTF-16
- Specify conversion options to the argument
- urlEncode : Encodes to percent-encoded string
- urlDecode : Decodes from percent-encoded string
- base64Encode : Encodes to Base64 formatted string
- base64Decode : Decodes from Base64 formatted string
- codeToString : Converts character code array to string
- stringToCode : Converts string to character code array
- Japanese Zenkaku/Hankaku conversion
- Other examples
- Contributing
- License
Features
encoding.js is a JavaScript library for converting and detecting character encodings,
supporting both Japanese character encodings (Shift_JIS
, EUC-JP
, ISO-2022-JP
) and Unicode formats (UTF-8
, UTF-16
).
Since JavaScript string values are internally encoded as UTF-16 code units (ref: ECMAScript® 2019 Language Specification - 6.1.4 The String Type), they cannot directly handle other character encodings as strings. However, encoding.js overcomes this limitation by treating these encodings as arrays instead of strings, enabling the conversion between different character sets.
Each character encoding is represented as an array of numbers corresponding to character code values, for example, [130, 160]
represents "あ" in Shift_JIS.
The array of character codes used in its methods can also be utilized with TypedArray objects, such as Uint8Array
, or with Buffer
in Node.js.
How to Use Character Encoding in Strings?
Numeric arrays of character codes can be converted to strings using methods such as Encoding.codeToString
.
However, due to the JavaScript specifications mentioned above, some character encodings may not be handled properly when converted directly to strings.
If you prefer to use strings instead of numeric arrays, you can convert them to percent-encoded strings,
such as '%82%A0'
, using Encoding.urlEncode
and Encoding.urlDecode
for passing to other resources.
Similarly, Encoding.base64Encode
and Encoding.base64Decode
allow for encoding and decoding to and from base64,
which can then be passed as strings.
Installation
npm
encoding.js is published under the package name encoding-japanese
on npm.
1npm install --save encoding-japanese
Using ES6 import
1import Encoding from 'encoding-japanese';
Using CommonJS require
1const Encoding = require('encoding-japanese');
TypeScript
TypeScript type definitions for encoding.js are available at @types/encoding-japanese (thanks to @rhysd).
1npm install --save-dev @types/encoding-japanese
Browser (standalone)
To use encoding.js in a browser environment, you can either install it via npm or download it directly from the release list.
The package includes both encoding.js
and encoding.min.js
.
Note: Cloning the repository via git clone
might give you access to the master (or main) branch, which could still be in a development state.
1<!-- To include the full version --> 2<script src="encoding.js"></script> 3 4<!-- Or, to include the minified version for production --> 5<script src="encoding.min.js"></script>
When the script is loaded, the object Encoding
is defined in the global scope (i.e., window.Encoding
).
CDN
You can use encoding.js (package name: encoding-japanese
) directly from a CDN via a script tag:
1<script src="https://unpkg.com/encoding-japanese@2.2.0/encoding.min.js"></script>
In this example we use unpkg, but you can use any CDN that provides npm packages, for example cdnjs or jsDelivr.
Supported encodings
Value in encoding.js | detect() | convert() | MIME Name (Note) |
---|---|---|---|
ASCII | ✓ | US-ASCII (Code point range: 0-127 ) | |
BINARY | ✓ | (Binary string. Code point range: 0-255 ) | |
EUCJP | ✓ | ✓ | EUC-JP |
JIS | ✓ | ✓ | ISO-2022-JP |
SJIS | ✓ | ✓ | Shift_JIS |
UTF8 | ✓ | ✓ | UTF-8 |
UTF16 | ✓ | ✓ | UTF-16 |
UTF16BE | ✓ | ✓ | UTF-16BE (big-endian) |
UTF16LE | ✓ | ✓ | UTF-16LE (little-endian) |
UTF32 | ✓ | UTF-32 | |
UNICODE | ✓ | ✓ | (JavaScript string. *See About UNICODE below) |
About UNICODE
In encoding.js, UNICODE
is defined as the internal character encoding that JavaScript strings (JavaScript string objects) can handle directly.
As mentioned in the Features section, JavaScript strings are internally encoded using UTF-16 code units.
This means that other character encodings cannot be directly handled without conversion.
Therefore, when converting to a character encoding that is properly representable in JavaScript, you should specify UNICODE
.
(Note: Even if the HTML file's encoding is UTF-8, you should specify UNICODE
instead of UTF8
when processing the encoding in JavaScript.)
When using Encoding.convert
, if you specify a character encoding other than UNICODE
(such as UTF8
or SJIS
), the values in the returned character code array will range from 0-255
.
However, if you specify UNICODE
, the values will range from 0-65535
, which corresponds to the range of values returned by String.prototype.charCodeAt()
(Code Units).
Example usage
Convert character encoding from JavaScript string (UNICODE
) to SJIS
.
1const unicodeArray = Encoding.stringToCode('こんにちは'); // Convert string to code array 2const sjisArray = Encoding.convert(unicodeArray, { 3 to: 'SJIS', 4 from: 'UNICODE' 5}); 6console.log(sjisArray); 7// [130, 177, 130, 241, 130, 201, 130, 191, 130, 205] ('こんにちは' array in SJIS)
Convert character encoding from SJIS
to UNICODE
.
1const sjisArray = [ 2 130, 177, 130, 241, 130, 201, 130, 191, 130, 205 3]; // 'こんにちは' array in SJIS 4 5const unicodeArray = Encoding.convert(sjisArray, { 6 to: 'UNICODE', 7 from: 'SJIS' 8}); 9const str = Encoding.codeToString(unicodeArray); // Convert code array to string 10console.log(str); // 'こんにちは'
Detect character encoding.
1const data = [ 2 227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175 3]; // 'こんにちは' array in UTF-8 4 5const detectedEncoding = Encoding.detect(data); 6console.log(`Character encoding is ${detectedEncoding}`); // 'Character encoding is UTF8'
(Node.js) Example of reading a text file written in SJIS
.
1const fs = require('fs'); 2const Encoding = require('encoding-japanese'); 3 4const sjisBuffer = fs.readFileSync('./sjis.txt'); 5const unicodeArray = Encoding.convert(sjisBuffer, { 6 to: 'UNICODE', 7 from: 'SJIS' 8}); 9console.log(Encoding.codeToString(unicodeArray));
Demo
- Playground for testing character encoding conversion and detection
- Test run for reading sample files and converting character encodings
- Demo for converting and detecting character encoding by specifying a file
API
- detect
- convert
- urlEncode
- urlDecode
- base64Encode
- base64Decode
- codeToString
- stringToCode
- Japanese Zenkaku/Hankaku conversion
Encoding.detect (data, [encodings])
Detects the character encoding of the given data.
Parameters
- data (Array<number>|TypedArray|Buffer|string) : The code array or string to detect character encoding.
- [encodings] (string|Array<string>|Object) : (Optional) Specifies a specific character encoding,
or an array of encodings to limit the detection. Detects automatically if this argument is omitted or
AUTO
is specified. Supported encoding values can be found in the "Supported encodings" section.
Return value
(string|boolean): Returns a string representing the detected encoding (e.g., SJIS
, UTF8
) listed in the "Supported encodings" section, or false
if the encoding cannot be detected.
If the encodings
argument is provided, it returns the name of the detected encoding if the data
matches any of the specified encodings, or false
otherwise.
Examples
Example of detecting character encoding.
1const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS 2const detectedEncoding = Encoding.detect(sjisArray); 3console.log(`Encoding is ${detectedEncoding}`); // 'Encoding is SJIS'
Example of using the encodings
argument to specify the character encoding to be detected.
This returns a string detected encoding if the specified encoding matches, or false
otherwise:
1const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS 2const detectedEncoding = Encoding.detect(sjisArray, 'SJIS'); 3if (detectedEncoding) { 4 console.log('Encoding is SJIS'); 5} else { 6 console.log('Encoding does not match SJIS'); 7}
Example of specifying multiple encodings:
1const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS 2const detectedEncoding = Encoding.detect(sjisArray, ['UTF8', 'SJIS']); 3if (detectedEncoding) { 4 console.log(`Encoding is ${detectedEncoding}`); // 'Encoding is SJIS' 5} else { 6 console.log('Encoding does not match UTF8 and SJIS'); 7}
Encoding.convert (data, to[, from])
Converts the character encoding of the given data.
Parameters
- data (Array<number>|TypedArray|Buffer|string) : The code array or string to convert character encoding.
- to (string|Object) : The character encoding name of the conversion destination as a string, or conversion options as an object.
- [from] (string|Array<string>) : (Optional) The character encoding name of the conversion source as a string,
or an array of encoding names. Detects automatically if this argument is omitted or
AUTO
is specified. Supported encoding values can be found in the "Supported encodings" section.
Return value
(Array<number>|TypedArray|string) : Returns a numeric character code array of the converted character encoding if data
is an array or a buffer,
or returns the converted string if data
is a string.
Examples
Example of converting a character code array to Shift_JIS from UTF-8:
1const utf8Array = [227, 129, 130]; // 'あ' in UTF-8 2const sjisArray = Encoding.convert(utf8Array, 'SJIS', 'UTF8'); 3console.log(sjisArray); // [130, 160] ('あ' in SJIS)
TypedArray such as Uint8Array
, and Buffer
of Node.js can be converted in the same usage:
1const utf8Array = new Uint8Array([227, 129, 130]); 2const sjisArray = Encoding.convert(utf8Array, 'SJIS', 'UTF8');
Converts character encoding by auto-detecting the encoding name of the source:
1// The character encoding is automatically detected when the argument `from` is omitted 2const utf8Array = [227, 129, 130]; 3let sjisArray = Encoding.convert(utf8Array, 'SJIS'); 4// Or explicitly specify 'AUTO' to auto-detecting 5sjisArray = Encoding.convert(utf8Array, 'SJIS', 'AUTO');
Specify conversion options to the argument to
as an object
You can pass the second argument to
as an object for improving readability.
Also, the following options such as type
, fallback
, and bom
must be specified with an object.
1const utf8Array = [227, 129, 130]; 2const sjisArray = Encoding.convert(utf8Array, { 3 to: 'SJIS', 4 from: 'UTF8' 5});
Specify the return type by the type
option
convert
returns an array by default, but you can change the return type by specifying the type
option.
Also, if the argument data
is passed as a string and the type
option is not specified, then type
='string' is assumed (returns as a string).
1const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS 2const unicodeString = Encoding.convert(sjisArray, { 3 to: 'UNICODE', 4 from: 'SJIS', 5 type: 'string' // Specify 'string' to return as string 6}); 7console.log(unicodeString); // 'おはよ'
The following type
options are supported.
- string : Return as a string.
- arraybuffer : Return as an ArrayBuffer (Actually returns a
Uint16Array
due to historical reasons). - array : Return as an Array. (default)
type: 'string'
can be used as a shorthand for converting a code array to a string,
as performed by Encoding.codeToString
.
Note: Specifying type: 'string'
may not handle conversions properly, except when converting to UNICODE
.
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 html-entity
as the fallback
option, you can replace them with HTML entities (Numeric character references), such as 🍣
.
Example of specifying { fallback: 'html-entity' }
option:
1const unicodeArray = Encoding.stringToCode('寿司🍣ビール🍺'); 2// No fallback specified 3let sjisArray = Encoding.convert(unicodeArray, { 4 to: 'SJIS', 5 from: 'UNICODE' 6}); 7console.log(sjisArray); // Converted to a code array of '寿司?ビール?' 8 9// Specify `fallback: html-entity` 10sjisArray = Encoding.convert(unicodeArray, { 11 to: 'SJIS', 12 from: 'UNICODE', 13 fallback: 'html-entity' 14}); 15console.log(sjisArray); // Converted to a code array of '寿司🍣ビール🍺'
Example of specifying { fallback: 'html-entity-hex' }
option:
1const unicodeArray = Encoding.stringToCode('ホッケの漢字は𩸽'); 2const sjisArray = Encoding.convert(unicodeArray, { 3 to: 'SJIS', 4 from: 'UNICODE', 5 fallback: 'html-entity-hex' 6}); 7console.log(sjisArray); // Converted to a code array of 'ホッケの漢字は𩸽'
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:
1const unicodeArray = Encoding.stringToCode('寿司🍣ビール🍺'); 2// No fallback specified 3let sjisArray = Encoding.convert(unicodeArray, { 4 to: 'SJIS', 5 from: 'UNICODE' 6}); 7console.log(sjisArray); // Converted to a code array of '寿司?ビール?' 8 9// Specify `fallback: ignore` 10sjisArray = Encoding.convert(unicodeArray, { 11 to: 'SJIS', 12 from: 'UNICODE', 13 fallback: 'ignore' 14}); 15console.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:
1const unicodeArray = Encoding.stringToCode('おにぎり🍙ラーメン🍜'); 2try { 3 const sjisArray = Encoding.convert(unicodeArray, { 4 to: 'SJIS', 5 from: 'UNICODE', 6 fallback: 'error' // Specify 'error' to throw an exception 7 }); 8} catch (e) { 9 console.error(e); // Error: Character cannot be represented: [240, 159, 141, 153] 10}
Specify BOM in UTF-16
You can add a BOM (byte order mark) by specifying the bom
option when converting to UTF16
.
The default is no BOM.
1const utf16Array = Encoding.convert(utf8Array, { 2 to: 'UTF16', 3 from: 'UTF8', 4 bom: true // Specify to add the BOM 5});
UTF16
byte order is big-endian by default.
If you want to convert as little-endian, specify the { bom: 'LE' }
option.
1const utf16leArray = Encoding.convert(utf8Array, { 2 to: 'UTF16', 3 from: 'UTF8', 4 bom: 'LE' // Specify to add the BOM as little-endian 5});
If you do not need BOM, use UTF16BE
or UTF16LE
.
UTF16BE
is big-endian, and UTF16LE
is little-endian, and both have no BOM.
1const utf16beArray = Encoding.convert(utf8Array, { 2 to: 'UTF16BE', 3 from: 'UTF8' 4});
Encoding.urlEncode (data)
Encodes a numeric character code array into a percent-encoded string formatted as a URI component in %xx
format.
urlEncode escapes all characters except the following, just like encodeURIComponent()
.
A-Z a-z 0-9 - _ . ! ~ * ' ( )
Parameters
- data (Array<number>|TypedArray|Buffer|string) : The numeric character code array or string that will be encoded into a percent-encoded URI component.
Return value
(string) : Returns a percent-encoded string formatted as a URI component in %xx
format.
Examples
Example of URL encoding a Shift_JIS array:
1const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS 2const encoded = Encoding.urlEncode(sjisArray); 3console.log(encoded); // '%82%A8%82%CD%82%E6'
Encoding.urlDecode (string)
Decodes a percent-encoded string formatted as a URI component in %xx
format to a numeric character code array.
Parameters
- string (string) : The string to decode.
Return value
(Array<number>) : Returns a numeric character code array.
Examples
Example of decoding a percent-encoded Shift_JIS string:
1const encoded = '%82%A8%82%CD%82%E6'; // 'おはよ' encoded as percent-encoded SJIS string 2const sjisArray = Encoding.urlDecode(encoded); 3console.log(sjisArray); // [130, 168, 130, 205, 130, 230]
Encoding.base64Encode (data)
Encodes a numeric character code array into a Base64 encoded string.
Parameters
- data (Array<number>|TypedArray|Buffer|string) : The numeric character code array or string to encode.
Return value
(string) : Returns a Base64 encoded string.
Examples
Example of Base64 encoding a Shift_JIS array:
1const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS 2const encodedStr = Encoding.base64Encode(sjisArray); 3console.log(encodedStr); // 'gqiCzYLm'
Encoding.base64Decode (string)
Decodes a Base64 encoded string to a numeric character code array.
Parameters
- string (string) : The Base64 encoded string to decode.
Return value
(Array<number>) : Returns a Base64 decoded numeric character code array.
Examples
Example of base64Encode
and base64Decode
:
1const sjisArray = [130, 177, 130, 241, 130, 201, 130, 191, 130, 205]; // 'こんにちは' array in SJIS 2const encodedStr = Encoding.base64Encode(sjisArray); 3console.log(encodedStr); // 'grGC8YLJgr+CzQ==' 4 5const decodedArray = Encoding.base64Decode(encodedStr); 6console.log(decodedArray); // [130, 177, 130, 241, 130, 201, 130, 191, 130, 205]
Encoding.codeToString (code)
Converts a numeric character code array to string.
Parameters
- code (Array<number>|TypedArray|Buffer) : The numeric character code array to convert.
Return value
(string) : Returns a converted string.
Examples
Example of converting a character code array to a string:
1const sjisArray = [130, 168, 130, 205, 130, 230]; // 'おはよ' array in SJIS 2const unicodeArray = Encoding.convert(sjisArray, { 3 to: 'UNICODE', 4 from: 'SJIS' 5}); 6const unicodeStr = Encoding.codeToString(unicodeArray); 7console.log(unicodeStr); // 'おはよ'
Encoding.stringToCode (string)
Converts a string to a numeric character code array.
Parameters
- string (string) : The string to convert.
Return value
(Array<number>) : Returns a numeric character code array converted from the string.
Examples
Example of converting a string to a character code array:
1const unicodeArray = Encoding.stringToCode('おはよ'); 2console.log(unicodeArray); // [12362, 12399, 12424]
Japanese Zenkaku/Hankaku conversion
The following methods convert Japanese full-width (zenkaku) and half-width (hankaku) characters,
suitable for use with UNICODE
strings or numeric character code arrays of UNICODE
.
Returns a converted string if the argument data
is a string.
Returns a numeric character code array if the argument data
is a code array.
- Encoding.toHankakuCase (data) : Converts full-width (zenkaku) symbols and alphanumeric characters to their half-width (hankaku) equivalents.
- Encoding.toZenkakuCase (data) : Converts half-width (hankaku) symbols and alphanumeric characters to their full-width (zenkaku) equivalents.
- Encoding.toHiraganaCase (data) : Converts full-width katakana to full-width hiragana.
- Encoding.toKatakanaCase (data) : Converts full-width hiragana to full-width katakana.
- Encoding.toHankanaCase (data) : Converts full-width katakana to half-width katakana.
- Encoding.toZenkanaCase (data) : Converts half-width katakana to full-width katakana.
- Encoding.toHankakuSpace (data) : Converts the em space (U+3000) to the single space (U+0020).
- Encoding.toZenkakuSpace (data) : Converts the single space (U+0020) to the em space (U+3000).
Parameters
- data (Array<number>|TypedArray|Buffer|string) : The string or numeric character code array to convert.
Return value
(Array<number>|string) : Returns a converted string or numeric character code array.
Examples
Example of converting zenkaku and hankaku strings:
1console.log(Encoding.toHankakuCase('abcDEF123@!#*=')); // 'abcDEF123@!#*=' 2console.log(Encoding.toZenkakuCase('abcDEF123@!#*=')); // 'abcDEF123@!#*=' 3console.log(Encoding.toHiraganaCase('アイウエオァィゥェォヴボポ')); // 'あいうえおぁぃぅぇぉゔぼぽ' 4console.log(Encoding.toKatakanaCase('あいうえおぁぃぅぇぉゔぼぽ')); // 'アイウエオァィゥェォヴボポ' 5console.log(Encoding.toHankanaCase('アイウエオァィゥェォヴボポ')); // 'アイウエオァィゥェォヴボポ' 6console.log(Encoding.toZenkanaCase('アイウエオァィゥェォヴボポ')); // 'アイウエオァィゥェォヴボポ' 7console.log(Encoding.toHankakuSpace('あいうえお abc 123')); // 'あいうえお abc 123' 8console.log(Encoding.toZenkakuSpace('あいうえお abc 123')); // 'あいうえお abc 123'
Example of converting zenkaku and hankaku code arrays:
1const unicodeArray = Encoding.stringToCode('abc123!# あいうアイウ ABCアイウ'); 2console.log(Encoding.codeToString(Encoding.toHankakuCase(unicodeArray))); 3// 'abc123!# あいうアイウ ABCアイウ' 4console.log(Encoding.codeToString(Encoding.toZenkakuCase(unicodeArray))); 5// 'abc123!# あいうアイウ ABCアイウ' 6console.log(Encoding.codeToString(Encoding.toHiraganaCase(unicodeArray))); 7// 'abc123!# あいうあいう ABCアイウ' 8console.log(Encoding.codeToString(Encoding.toKatakanaCase(unicodeArray))); 9// 'abc123!# アイウアイウ ABCアイウ' 10console.log(Encoding.codeToString(Encoding.toHankanaCase(unicodeArray))); 11// 'abc123!# あいうアイウ ABCアイウ' 12console.log(Encoding.codeToString(Encoding.toZenkanaCase(unicodeArray))); 13// 'abc123!# あいうアイウ ABCアイウ' 14console.log(Encoding.codeToString(Encoding.toHankakuSpace(unicodeArray))); 15// 'abc123!# あいうアイウ ABCアイウ' 16console.log(Encoding.codeToString(Encoding.toZenkakuSpace(unicodeArray))); 17// 'abc123!# あいうアイウ ABCアイウ'
Other examples
Example using the Fetch API
and Typed Arrays (Uint8Array)
This example reads a text file encoded in Shift_JIS as binary data, and displays it as a string after converting it to Unicode using Encoding.convert.
1(async () => { 2 try { 3 const response = await fetch('shift_jis.txt'); 4 const buffer = await response.arrayBuffer(); 5 6 // Code array with Shift_JIS file contents 7 const sjisArray = new Uint8Array(buffer); 8 9 // Convert encoding to UNICODE (JavaScript Code Units) from Shift_JIS 10 const unicodeArray = Encoding.convert(sjisArray, { 11 to: 'UNICODE', 12 from: 'SJIS' 13 }); 14 15 // Convert to string from code array for display 16 const unicodeString = Encoding.codeToString(unicodeArray); 17 console.log(unicodeString); 18 } catch (error) { 19 console.error('Error loading the file:', error); 20 } 21})();
XMLHttpRequest version of this example
1const req = new XMLHttpRequest(); 2req.open('GET', 'shift_jis.txt', true); 3req.responseType = 'arraybuffer'; 4 5req.onload = (event) => { 6 const buffer = req.response; 7 if (buffer) { 8 // Code array with Shift_JIS file contents 9 const sjisArray = new Uint8Array(buffer); 10 11 // Convert encoding to UNICODE (JavaScript Code Units) from Shift_JIS 12 const unicodeArray = Encoding.convert(sjisArray, { 13 to: 'UNICODE', 14 from: 'SJIS' 15 }); 16 17 // Convert to string from code array for display 18 const unicodeString = Encoding.codeToString(unicodeArray); 19 console.log(unicodeString); 20 } 21}; 22 23req.send(null);
Convert encoding for file using the File APIs
This example uses the File API to read the content of a selected file, detects its character encoding,
and converts the file content to UNICODE from any character encoding such as Shift_JIS
or EUC-JP
.
The converted content is then displayed in a textarea.
1<input type="file" id="file"> 2<div id="encoding"></div> 3<textarea id="content" rows="5" cols="80"></textarea> 4 5<script> 6function onFileSelect(event) { 7 const file = event.target.files[0]; 8 9 const reader = new FileReader(); 10 reader.onload = function(e) { 11 const codes = new Uint8Array(e.target.result); 12 13 const detectedEncoding = Encoding.detect(codes); 14 const encoding = document.getElementById('encoding'); 15 encoding.textContent = `Detected encoding: ${detectedEncoding}`; 16 17 // Convert encoding to UNICODE 18 const unicodeString = Encoding.convert(codes, { 19 to: 'UNICODE', 20 from: detectedEncoding, 21 type: 'string' 22 }); 23 document.getElementById('content').value = unicodeString; 24 }; 25 26 reader.readAsArrayBuffer(file); 27} 28 29document.getElementById('file').addEventListener('change', onFileSelect); 30</script>
Contributing
We welcome contributions from everyone. For bug reports and feature requests, please create an issue on GitHub.
Pull requests
Before submitting a pull request, please run npm run test
to ensure there are no errors.
We only accept pull requests that pass all tests.
License
This project is licensed under the terms of the MIT license. See the LICENSE file for details.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
GitHub workflow tokens follow principle of least privilege
Details
- Info: topLevel 'contents' permission set to 'read': .github/workflows/ci.yml:4
- Info: no jobLevel write permissions found
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
6 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 5
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/polygonplanet/encoding.js/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/polygonplanet/encoding.js/ci.yml/master?enable=pin
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 1 out of 1 npmCommand dependencies pinned
Reason
Found 2/17 approved changesets -- score normalized to 1
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 17 are checked with a SAST tool
Reason
10 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-fvqr-27wr-82fm
- Warn: Project is vulnerable to: GHSA-4xc9-xhrj-v574
- Warn: Project is vulnerable to: GHSA-x5rq-j2xg-h7qm
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
Score
4.2
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More