From e8ab20ea2f99f6b9a9eddb32c295ab1abfc6d8f4 Mon Sep 17 00:00:00 2001 From: Taesu Date: Sat, 25 Jul 2026 07:26:20 +0900 Subject: [PATCH] fix(binary): bind TextEncoder when encoding --- src/binary.test.ts | 14 ++++++++++++++ src/binary.ts | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 src/binary.test.ts diff --git a/src/binary.test.ts b/src/binary.test.ts new file mode 100644 index 0000000..ab7c2a7 --- /dev/null +++ b/src/binary.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, it } from "vitest"; +import { binary } from "./binary"; + +describe("binary", () => { + it("encodes a string", () => { + expect(binary.encode("hi")).toEqual(new Uint8Array([104, 105])); + }); + + it("round-trips a string", () => { + const input = "Hello World!"; + + expect(binary.decode(binary.encode(input))).toBe(input); + }); +}); diff --git a/src/binary.ts b/src/binary.ts index d3fdada..0ffa7f9 100644 --- a/src/binary.ts +++ b/src/binary.ts @@ -1,3 +1,5 @@ +import type { Uint8Array_ } from "./type"; + type Encoding = "utf-8" | "utf-16" | "iso-8859-1"; type BinaryData = ArrayBuffer | ArrayBufferView; @@ -13,5 +15,5 @@ export const binary = { const decoder = decoders.get(encoding)!; return decoder.decode(data); }, - encode: encoder.encode, + encode: (input?: string): Uint8Array_ => encoder.encode(input), };