Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/binary.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
4 changes: 3 additions & 1 deletion src/binary.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Uint8Array_ } from "./type";

type Encoding = "utf-8" | "utf-16" | "iso-8859-1";

type BinaryData = ArrayBuffer | ArrayBufferView;
Expand All @@ -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),
};