@i-xi-dev/ms932-encoder
v2.1.9
Published
A JavaScript Windows-31J encoder, implements Shift_JIS encoder defined in WHATWG Encoding Standard.
Downloads
565
Maintainers
Readme
@i-xi-dev/ms932-encoder
A JavaScript Windows-31J encoder, implements Shift_JIS encoder defined in WHATWG Encoding Standard.
Requirement
Ms932.Encoder
class
| Chrome | Edge | Firefox | Safari | Deno | Node.js | | :---: | :---: | :---: | :---: | :---: | :---: | | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
Ms932.EncoderStream
class
Ms932.EncoderStream
requires
TransformStream
.
| Chrome | Edge | Firefox | Safari | Deno | Node.js | | :---: | :---: | :---: | :---: | :---: | :---: | | ✅ | ✅ | ✅102+ | ✅14.1+ | ✅ | ✅16.5+ |
Installation
npm
$ npm i @i-xi-dev/[email protected]
import { Ms932 } from "@i-xi-dev/ms932-encoder";
CDN
Example for UNPKG
import { Ms932 } from "https://www.unpkg.com/@i-xi-dev/[email protected]/esm/mod.js";
Usage
Ms932.Encoder
class
The Ms932.Encoder
implements the
TextEncoder
interface.
const encoder = new Ms932.Encoder();
encoder.encode("あいうえお");
// → Uint8Array[ 0x82, 0xA0, 0x82, 0xA2, 0x82, 0xA4, 0x82, 0xA6, 0x82, 0xA8 ]
const bytes = new Uint8Array(10);
const { read, written } = encoder.encodeInto("あいうえお", bytes);
// → read: 5
// written: 10
// bytes: Uint8Array[ 0x82, 0xA0, 0x82, 0xA2, 0x82, 0xA4, 0x82, 0xA6, 0x82, 0xA8 ]
Encoding error handling
const encoder = new Ms932.Encoder({ fatal: false }); // default
encoder.encode("𩸽");
// → Uint8Array[ 0x3F ]
const encoder = new Ms932.Encoder({ replacementChar: "■" });
encoder.encode("𩸽");
// → Uint8Array[ 0x81, 0xA1 ]
const encoder = new Ms932.Encoder({ fatal: true });
encoder.encode("𩸽");
// → throws TypeError.
Ms932.EncoderStream
class
The Ms932.EncoderStream
implements
TextEncoderStream
interface.
const encoderStream = new Ms932.EncoderStream();
// readableStream: ReadableStream<string>
// writableStream: WritableStream<Uint8Array>
readableStream.pipeThrough(encoderStream).pipeTo(writableStream);
Examples
FAQ
Q: Where is Ms932.Decoder?
A: Browsers and Node.js implement the
Shift_JIS decoder. Use a
TextDecoder
.
const decoder = new TextDecoder("shift_jis");