Skip to content

Commit

Permalink
Enhance documentation in source files and README. (#36)
Browse files Browse the repository at this point in the history
* Enhance documentation in source files and README.

* Update Biome.js config for ignoring
  • Loading branch information
sciencesakura authored Feb 17, 2025
1 parent 461ba07 commit 4b75ca7
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 33 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 sciencesakura
Copyright (c) 2025 sciencesakura

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
39 changes: 32 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
# mutf-8: Encoder/Decoder for Modified UTF-8
# mutf-8: An Encoder and Decoder for Modified UTF-8

![](https://github.com/sciencesakura/mutf-8/actions/workflows/build.yaml/badge.svg) [![npm version](https://badge.fury.io/js/mutf-8.svg)](https://badge.fury.io/js/mutf-8)

## What is Modified UTF-8?

Modified UTF-8 (MUTF-8) is used in the Java platform, such as the class file format and object serialization.
Modified UTF-8 is a variant of UTF-8 used in the Java platform, such as the class file format and object serialization.

See [The Java Virtual Machine Specification, Java SE 21 Edition, section 4.4.7](https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html#jvms-4.4.7) for details.
See [The Java Virtual Machine Specification, Java SE 21 Edition, Section 4.4.7](https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html#jvms-4.4.7) for details.

## Installation

```sh
npm install mutf-8
```

If you want to use the stream API, install the `mutf-8-stream` module instead.

```sh
npm install mutf-8-stream
```

## Usage

The APIs are similar to [WHATWG TextEncoder/TextDecoder](https://encoding.spec.whatwg.org/).
The APIs are similar to [WHATWG Encoding](https://encoding.spec.whatwg.org/).

```javascript
import { MUtf8Decoder, MUtf8Encoder } from "mutf-8"; // ES Modules
// const { MUtf8Decoder, MUtf8Encoder } = require("mutf-8"); // CommonJS
import { MUtf8Decoder, MUtf8Encoder } from "mutf-8";

const encoder = new MUtf8Encoder();
const code = encoder.encode("Hello 世界! Santé🍻");
Expand All @@ -36,7 +41,27 @@ const text = decoder.decode(code);
// Hello 世界! Santé🍻
```

See [API reference](https://sciencesakura.github.io/mutf-8/) for details.
```javascript
import { MUtf8DecoderStream, MUtf8EncoderStream } from "mutf-8-stream";

await getLargeTextAsStream()
.pipeThrough(new MUtf8EncoderStream())
.pipeTo(new WritableStream({
write(chunk) {
// Handle the encoded chunk
}
}));

await getLargeBinaryAsStream()
.pipeThrough(new MUtf8DecoderStream())
.pipeTo(new WritableStream({
write(chunk) {
// Handle the decoded chunk
}
}));
```

See [API Reference](https://sciencesakura.github.io/mutf-8/) for details.

## License

Expand Down
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
},
"files": {
"ignoreUnknown": false,
"ignore": ["**/testdata-*"]
"ignore": ["**/testdata.*"]
},
"formatter": {
"enabled": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/mutf-8-stream/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mutf-8-stream",
"version": "1.2.0",
"description": "An encoder/decoder for Modified UTF-8 (MUTF-8) which is used in the Java platform such as the class file format and the object serialization.",
"description": "An encoder and decoder for Modified UTF-8 which is used in the Java platform such as the class file format and object serialization.",
"keywords": [
"charset",
"encoding",
Expand Down
32 changes: 18 additions & 14 deletions packages/mutf-8-stream/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
// SPDX-License-Identifier: MIT

import { type AllowSharedBufferSource, MUtf8Decoder, MUtf8Encoder, type TextDecoderOptions } from "mutf-8";

/**
* The decoder for Modified UTF-8. It is the streaming equivalent of {@link MUtf8Decoder}.
*
* @example
* ```ts
* // Convert Modified UTF-8 stream to UTF-8 stream.
* await mutf8Stream.pipeThrough(new MUtf8DecoderStream())
* .pipeThrough(new TextEncoderStream())
* .pipeTo(destination);
* await getLargeBinaryAsStream()
* .pipeThrough(new MUtf8DecoderStream())
* .pipeTo(new WritableStream({
* write(chunk) {
* // Handle the decoded chunk
* }
* }));
* ```
*
* @see {@link https://encoding.spec.whatwg.org/}
* @see {@link https://encoding.spec.whatwg.org/#interface-textdecoderstream}
* @since v1.2.0
*/
export class MUtf8DecoderStream extends TransformStream<AllowSharedBufferSource, string> {
#decoder: MUtf8Decoder;
Expand All @@ -39,8 +44,6 @@ export class MUtf8DecoderStream extends TransformStream<AllowSharedBufferSource,
}

/**
* Creates an instance of `MUtf8DecoderStream`.
*
* @param label - The label of the decoder. Must be `"mutf-8"` or `"mutf8"`.
* @param options - The options for the decoder.
* @throws `RangeError` If the `label` is an invalid value.
Expand All @@ -61,13 +64,17 @@ export class MUtf8DecoderStream extends TransformStream<AllowSharedBufferSource,
*
* @example
* ```ts
* // Convert UTF-8 stream to Modified UTF-8 stream.
* await utf8Stream.pipeThrough(new TextDecoderStream())
* await getLargeTextAsStream()
* .pipeThrough(new MUtf8EncoderStream())
* .pipeTo(destination);
* .pipeTo(new WritableStream({
* write(chunk) {
* // Handle the encoded chunk
* }
* }));
* ```
*
* @see {@link https://encoding.spec.whatwg.org/}
* @see {@link https://encoding.spec.whatwg.org/#interface-textencoderstream}
* @since v1.2.0
*/
export class MUtf8EncoderStream extends TransformStream<string, Uint8Array> {
/**
Expand All @@ -77,9 +84,6 @@ export class MUtf8EncoderStream extends TransformStream<string, Uint8Array> {
return "mutf-8";
}

/**
* Creates an instance of `MUtf8EncoderStream`.
*/
constructor() {
const encoder = new MUtf8Encoder();
super({
Expand Down
2 changes: 1 addition & 1 deletion packages/mutf-8/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mutf-8",
"version": "1.2.0",
"description": "An encoder/decoder for Modified UTF-8 (MUTF-8) which is used in the Java platform such as the class file format and the object serialization.",
"description": "An encoder and decoder for Modified UTF-8 which is used in the Java platform such as the class file format and object serialization.",
"keywords": [
"charset",
"encoding",
Expand Down
10 changes: 4 additions & 6 deletions packages/mutf-8/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export type AllowSharedBufferSource = ArrayBuffer | SharedArrayBuffer | ArrayBuf
* // Hello 世界!
* ```
*
* @see {@link https://encoding.spec.whatwg.org/}
* @see {@link https://encoding.spec.whatwg.org/#interface-textdecoder}
*/
export class MUtf8Decoder {
#fatal: boolean;
Expand Down Expand Up @@ -92,11 +92,9 @@ export class MUtf8Decoder {
}

/**
* Creates an instance of MUtf8Decoder.
*
* @param label - The label of the decoder. Must be `"mutf-8"` or `"mutf8"`.
* @param options - The options for the decoder.
* @throws {RangeError} If the `label` is an invalid value.
* @throws `RangeError` If the `label` is an invalid value.
*/
constructor(label = "mutf-8", options: TextDecoderOptions = {}) {
const normalizedLabel = label.toLowerCase();
Expand All @@ -113,7 +111,7 @@ export class MUtf8Decoder {
* @param input - The bytes to be decoded.
* @param options - The options for decoding.
* @returns The resultant string after decoding.
* @throws {TypeError} If {@link fatal} is `true` and the `input` contains invalid bytes.
* @throws `TypeError` If {@link fatal} is `true` and the `input` contains invalid bytes.
*/
decode(input: AllowSharedBufferSource, options: TextDecodeOptions = {}): string {
const stream = options.stream ?? false;
Expand Down Expand Up @@ -215,7 +213,7 @@ export class MUtf8Decoder {
* // ]
* ```
*
* @see {@link https://encoding.spec.whatwg.org/}
* @see {@link https://encoding.spec.whatwg.org/#interface-textencoder}
*/
export class MUtf8Encoder {
/**
Expand Down
1 change: 0 additions & 1 deletion scripts/gen-testdatacode.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ def decode = { binary ->

def generateTestDataCode = { jsonFile ->
def code = new StringBuilder('// This file is generated by scripts/gen-testdatacode.groovy\n\n')
code << '// biome-ignore format:\n'
code << 'export default ['
new JsonSlurper().parse(jsonFile).each {
def binary = encode(it.text)
Expand Down
1 change: 0 additions & 1 deletion testdata.mts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4b75ca7

Please sign in to comment.