From 8449d4f77d6f80ebfee02c094504141cd5380c83 Mon Sep 17 00:00:00 2001 From: barrytra Date: Thu, 11 Apr 2024 19:38:00 +0530 Subject: [PATCH] decoder function rectified --- src/cjs/browser.cjs | 2 +- src/mjs/browser.js | 2 +- ts_src/browser.ts | 2 +- ts_src/tests.spec.ts | 5 +++++ 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/cjs/browser.cjs b/src/cjs/browser.cjs index f44586d..607fe23 100644 --- a/src/cjs/browser.cjs +++ b/src/cjs/browser.cjs @@ -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); } diff --git a/src/mjs/browser.js b/src/mjs/browser.js index e1909ae..89379f0 100644 --- a/src/mjs/browser.js +++ b/src/mjs/browser.js @@ -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); } diff --git a/ts_src/browser.ts b/ts_src/browser.ts index 08e801c..57f5a5f 100644 --- a/ts_src/browser.ts +++ b/ts_src/browser.ts @@ -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); diff --git a/ts_src/tests.spec.ts b/ts_src/tests.spec.ts index 5c6557e..427da31 100644 --- a/ts_src/tests.spec.ts +++ b/ts_src/tests.spec.ts @@ -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"], @@ -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(""); });