Nonviolent Pigeon Manifestation

    @i-xi-dev/ms932-encoder
    TypeScript icon, indicating that this package has built-in type declarations

    2.0.4 • Public • Published

    @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
    • Safari 14.1+
    • Deno
    • Node.js 16.5.0+

    Installation

    npm

    $ npm i @i-xi-dev/ms932-encoder
    import { Ms932 } from "@i-xi-dev/ms932-encoder";

    CDN

    Example for Skypack

    import { Ms932 } from "https://cdn.skypack.dev/@i-xi-dev/ms932-encoder";

    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 Error.

    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);

    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");

    Install

    npm i @i-xi-dev/ms932-encoder

    DownloadsWeekly Downloads

    63

    Version

    2.0.4

    License

    MIT

    Unpacked Size

    6.64 MB

    Total Files

    94

    Last publish

    Collaborators

    • i-xi-dev