From 9465d542a964b54edbcb931561c73da0b3e28d55 Mon Sep 17 00:00:00 2001 From: barrytra Date: Thu, 11 Apr 2024 15:17:33 +0530 Subject: [PATCH] added function bytes to Utf8 --- src/cjs/browser.cjs | 6 +++++- src/cjs/index.cjs | 6 +++++- src/cjs/index.d.ts | 1 + src/mjs/browser.js | 3 +++ src/mjs/index.js | 3 +++ ts_src/browser.ts | 4 ++++ ts_src/index.ts | 4 ++++ ts_src/tests.spec.ts | 9 +++++++++ 8 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/cjs/browser.cjs b/src/cjs/browser.cjs index 37ba40d..f44586d 100644 --- a/src/cjs/browser.cjs +++ b/src/cjs/browser.cjs @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.compare = exports.fromHex = exports.toHex = void 0; +exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0; const HEX_STRINGS = "0123456789abcdefABCDEF"; const HEX_CODES = HEX_STRINGS.split("").map((c) => c.codePointAt(0)); const HEX_CODEPOINTS = Array(256) @@ -13,6 +13,10 @@ const HEX_CODEPOINTS = Array(256) }); const ENCODER = new TextEncoder(); const DECODER = new TextDecoder("ascii"); +function toUtf8(bytes) { + return DECODER.decode(bytes); +} +exports.toUtf8 = toUtf8; // There are two implementations. // One optimizes for length of the bytes, and uses TextDecoder. // One optimizes for iteration count, and appends strings. diff --git a/src/cjs/index.cjs b/src/cjs/index.cjs index 7cdc76a..473f705 100644 --- a/src/cjs/index.cjs +++ b/src/cjs/index.cjs @@ -1,6 +1,10 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.compare = exports.fromHex = exports.toHex = void 0; +exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0; +function toUtf8(bytes) { + return Buffer.from(bytes || []).toString(); +} +exports.toUtf8 = toUtf8; function toHex(bytes) { return Buffer.from(bytes || []).toString("hex"); } diff --git a/src/cjs/index.d.ts b/src/cjs/index.d.ts index da9a248..2a1717a 100644 --- a/src/cjs/index.d.ts +++ b/src/cjs/index.d.ts @@ -1,3 +1,4 @@ +export declare function toUtf8(bytes: Uint8Array): string; export declare function toHex(bytes: Uint8Array): string; export declare function fromHex(hexString: string): Uint8Array; export declare type CompareResult = -1 | 0 | 1; diff --git a/src/mjs/browser.js b/src/mjs/browser.js index 76ba33a..e1909ae 100644 --- a/src/mjs/browser.js +++ b/src/mjs/browser.js @@ -10,6 +10,9 @@ const HEX_CODEPOINTS = Array(256) }); const ENCODER = new TextEncoder(); const DECODER = new TextDecoder("ascii"); +export function toUtf8(bytes) { + return DECODER.decode(bytes); +} // There are two implementations. // One optimizes for length of the bytes, and uses TextDecoder. // One optimizes for iteration count, and appends strings. diff --git a/src/mjs/index.js b/src/mjs/index.js index efc0382..8d35bf4 100644 --- a/src/mjs/index.js +++ b/src/mjs/index.js @@ -1,3 +1,6 @@ +export function toUtf8(bytes) { + return Buffer.from(bytes || []).toString(); +} export function toHex(bytes) { return Buffer.from(bytes || []).toString("hex"); } diff --git a/ts_src/browser.ts b/ts_src/browser.ts index c719ee1..08e801c 100644 --- a/ts_src/browser.ts +++ b/ts_src/browser.ts @@ -11,6 +11,10 @@ const HEX_CODEPOINTS: (number | undefined)[] = Array(256) const ENCODER = new TextEncoder(); const DECODER = new TextDecoder("ascii"); +export function toUtf8(bytes: Uint8Array): string { + return DECODER.decode(bytes); +} + // There are two implementations. // One optimizes for length of the bytes, and uses TextDecoder. // One optimizes for iteration count, and appends strings. diff --git a/ts_src/index.ts b/ts_src/index.ts index d8185c8..deb373b 100644 --- a/ts_src/index.ts +++ b/ts_src/index.ts @@ -1,3 +1,7 @@ +export function toUtf8(bytes: Uint8Array): string { + return Buffer.from(bytes || []).toString(); +} + export function toHex(bytes: Uint8Array): string { return Buffer.from(bytes || []).toString("hex"); } diff --git a/ts_src/tests.spec.ts b/ts_src/tests.spec.ts index 7bf220b..5c6557e 100644 --- a/ts_src/tests.spec.ts +++ b/ts_src/tests.spec.ts @@ -14,6 +14,10 @@ const bytes2Larger = f([0xff, 0x01, 0x00]); const bytes2LargerLeft = f([0x00, 0xff, 0x01]); const longBytes = new Uint8Array(513).fill(0xfa); const longHex = "fa".repeat(513); +const bytes3 = f([0x21, 0x7e]); +const utf8 = "!~"; +const longBytes2 = new Uint8Array(513).fill(0x61); +const longUtf8 = "a".repeat(513); const brokenHexes = [ [" ff00", f([]), "leading space"], @@ -39,6 +43,11 @@ describe(`Uint8Array tools`, () => { expect(tools.toHex(longBytes)).toEqual(longHex); expect((tools.toHex as any)()).toEqual(""); }); + it(`should output utf8 with toUtf8`, () => { + expect(tools.toUtf8(bytes3)).toEqual(utf8); + expect(tools.toUtf8(longBytes2)).toEqual(longUtf8); + expect((tools.toUtf8 as any)()).toEqual(""); + }); it(`should compare Uint8Arrays`, () => { expect(tools.compare(bytes, bytes2)).toBe(-1); expect(tools.compare(bytes, bytes)).toBe(0);