Registry / serialization / encoding-japanese

encoding-japanese

JSON →
library2.2.0jsnpmunverified

encoding-japanese is a JavaScript library designed for detecting and converting various character encodings, with a strong focus on Japanese encodings like Shift_JIS, EUC-JP, and ISO-2022-JP, alongside common Unicode formats like UTF-8 and UTF-16. Unlike standard JavaScript string handling, which is internally UTF-16, this library processes encodings as arrays of character code values, allowing for conversions between diverse character sets. The current stable version is 2.2.0, with releases occurring infrequently, often driven by feature additions or maintenance updates. Its key differentiator is robust support for a wide range of Japanese encodings and its ability to handle character codes as arrays, making it suitable for binary data manipulation (e.g., with `Uint8Array` or Node.js `Buffer`).

npm install encoding-japanese
INSTALL
IMPORT
SIG · ENCODING-JAPANESE
E
encoding-japanese
serializationjavascriptv2.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Encoding
import * as Encoding from 'encoding-japanese';
import Encoding from 'encoding-japanese';
The library exports a namespace object, not a default export. Use `* as Encoding` for all methods.
Encoding.detect
import { detect } from 'encoding-japanese/lib/encoding';
import { detect } from 'encoding-japanese';
While `* as Encoding` is common, individual methods can be imported directly from their `lib` paths for tree-shaking benefits, though the `lib/encoding` path is less intuitive.
Encoding.convert
const Encoding = require('encoding-japanese'); const convertedData = Encoding.convert(...);
const { convert } = require('encoding-japanese');
For CommonJS, `require('encoding-japanese')` returns the full `Encoding` object. Destructuring individual methods directly from the top-level package is not directly supported without deeper path imports.

Demonstrates converting Shift_JIS byte array to UTF-8 string, detecting encoding, and URL encoding, highlighting core library functionalities.

import * as Encoding from 'encoding-japanese'; // Example: Convert a Shift_JIS string to UTF-8 const shiftJisBytes = new Uint8Array([ 0x82, 0xA0, 0x82, 0xA2, 0x82, 0xA4, 0x82, 0xA6, 0x82, 0xA8 // 'あいうえお' in Shift_JIS ]); const utf8Bytes = Encoding.convert(shiftJisBytes, { to: 'UTF8', from: 'SJIS', type: 'array' }); const utf8String = Encoding.codeToString(utf8Bytes); console.log('Original Shift_JIS (bytes):', shiftJisBytes); console.log('Converted UTF-8 (bytes):', utf8Bytes); console.log('Converted UTF-8 (string):', utf8String); // Example: Detect encoding const detectedEncoding = Encoding.detect(shiftJisBytes); console.log('Detected encoding:', detectedEncoding); // Example: URL encoding const urlEncoded = Encoding.urlEncode('テスト&123'); console.log('URL Encoded:', urlEncoded);
Debug
Known issues
breakingVersion 2.0.0 introduced breaking changes by adding a `fallback` option to `Encoding.convert`. Previously, unrepresentable characters might have been handled differently or implicitly, but now explicit handling through `fallback` options ('html-entity', 'ignore', 'error') is available. While adding functionality, existing code relying on previous implicit behavior for unrepresentable characters might need adjustment.
fix
Review calls to `Encoding.convert` and explicitly define a `fallback` option (e.g., `fallback: 'html-entity'`, `fallback: 'ignore'`, or `fallback: 'error'`) to match desired behavior for unrepresentable characters.
affects: >=2.0.0
gotchaJavaScript strings are internally UTF-16. `encoding-japanese` primarily works with arrays of character codes (e.g., `Uint8Array`). Direct string arguments to `convert` or `detect` are not the primary use case and might lead to unexpected results if not correctly converted to/from character code arrays first using `stringToCode` and `codeToString`.
fix
Always convert JavaScript strings to character code arrays (e.g., `Encoding.stringToCode(myString)`) before passing them to `Encoding.convert` or `Encoding.detect` if you intend to specify a non-UTF-16 'from' encoding. Similarly, convert the resulting code arrays back to strings using `Encoding.codeToString`.
affects: >=1.0.0
gotchaWhen using `Encoding.convert`, the `type` option determines the return format. Omitting it or using `type: 'array'` returns a plain number array, while `type: 'Uint8Array'` returns a TypedArray. Mixing these types in subsequent operations without explicit conversion can lead to errors.
fix
Be explicit about the `type` option in `Encoding.convert` (e.g., `type: 'Uint8Array'`) if you expect a TypedArray. Ensure consistency in data types when chaining operations or explicitly convert between plain arrays and TypedArrays as needed.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Converting circular structure to JSON
Attempting to `JSON.stringify` an array of character codes (especially Uint8Array) without proper serialization, or passing non-serializable objects to functions.
fix
Ensure that if you need to serialize character code arrays, you convert them to a JSON-compatible format first, such as a regular array of numbers (`Array.from(uint8Array)`), or directly to a string via `Encoding.codeToString` before stringifying.
Cannot find module 'encoding-japanese'
Incorrect import path or the package is not installed correctly, especially in environments with strict module resolution or when using specific build tools.
fix
Verify the package is installed (`npm install encoding-japanese`) and ensure your import statement is `import * as Encoding from 'encoding-japanese';` for ESM or `const Encoding = require('encoding-japanese');` for CommonJS. Check your bundler or TypeScript configuration if paths are being resolved incorrectly.
Unrepresentable character error or incorrect character output after conversion.
Occurs when a character from the 'from' encoding cannot be represented in the 'to' encoding, and the `fallback` option is not handled or is set to 'error'.
fix
Use the `fallback` option in `Encoding.convert` (e.g., `fallback: 'html-entity'`, `fallback: 'ignore'`, or `fallback: 'error'`) to explicitly define how unrepresentable characters should be handled. If 'error' is used, wrap the conversion in a `try...catch` block to gracefully handle the error.
Upgrade
Version history
2.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
encoding-japanese — npm install encoding-japanese · libregistry