Skip to content

Commit

Permalink
decoder function rectified
Browse files Browse the repository at this point in the history
  • Loading branch information
barrytra committed Apr 11, 2024
1 parent 9465d54 commit 8449d4f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/cjs/browser.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const HEX_CODEPOINTS = Array(256)
return index < 0 ? undefined : index < 16 ? index : index - 6;
});
const ENCODER = new TextEncoder();
const DECODER = new TextDecoder("ascii");
const DECODER = new TextDecoder();
function toUtf8(bytes) {
return DECODER.decode(bytes);
}
Expand Down
2 changes: 1 addition & 1 deletion src/mjs/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const HEX_CODEPOINTS = Array(256)
return index < 0 ? undefined : index < 16 ? index : index - 6;
});
const ENCODER = new TextEncoder();
const DECODER = new TextDecoder("ascii");
const DECODER = new TextDecoder();
export function toUtf8(bytes) {
return DECODER.decode(bytes);
}
Expand Down
2 changes: 1 addition & 1 deletion ts_src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const HEX_CODEPOINTS: (number | undefined)[] = Array(256)
return index < 0 ? undefined : index < 16 ? index : index - 6;
});
const ENCODER = new TextEncoder();
const DECODER = new TextDecoder("ascii");
const DECODER = new TextDecoder();

export function toUtf8(bytes: Uint8Array): string {
return DECODER.decode(bytes);
Expand Down
5 changes: 5 additions & 0 deletions ts_src/tests.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const bytes3 = f([0x21, 0x7e]);
const utf8 = "!~";
const longBytes2 = new Uint8Array(513).fill(0x61);
const longUtf8 = "a".repeat(513);
const testBytes = f([
227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175,
]);
const str = "こんにちは";

const brokenHexes = [
[" ff00", f([]), "leading space"],
Expand Down Expand Up @@ -45,6 +49,7 @@ describe(`Uint8Array tools`, () => {
});
it(`should output utf8 with toUtf8`, () => {
expect(tools.toUtf8(bytes3)).toEqual(utf8);
expect(tools.toUtf8(testBytes)).toEqual(str);
expect(tools.toUtf8(longBytes2)).toEqual(longUtf8);
expect((tools.toUtf8 as any)()).toEqual("");
});
Expand Down

0 comments on commit 8449d4f

Please sign in to comment.