Skip to content

Commit bfe4c9e

Browse files
authored
Merge pull request #4 from barrytra/bytes-to-utf8
added function bytes to Utf8
2 parents c0a0a04 + 8449d4f commit bfe4c9e

File tree

8 files changed

+42
-5
lines changed

8 files changed

+42
-5
lines changed

src/cjs/browser.cjs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.compare = exports.fromHex = exports.toHex = void 0;
3+
exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
44
const HEX_STRINGS = "0123456789abcdefABCDEF";
55
const HEX_CODES = HEX_STRINGS.split("").map((c) => c.codePointAt(0));
66
const HEX_CODEPOINTS = Array(256)
@@ -12,7 +12,11 @@ const HEX_CODEPOINTS = Array(256)
1212
return index < 0 ? undefined : index < 16 ? index : index - 6;
1313
});
1414
const ENCODER = new TextEncoder();
15-
const DECODER = new TextDecoder("ascii");
15+
const DECODER = new TextDecoder();
16+
function toUtf8(bytes) {
17+
return DECODER.decode(bytes);
18+
}
19+
exports.toUtf8 = toUtf8;
1620
// There are two implementations.
1721
// One optimizes for length of the bytes, and uses TextDecoder.
1822
// One optimizes for iteration count, and appends strings.

src/cjs/index.cjs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
3-
exports.compare = exports.fromHex = exports.toHex = void 0;
3+
exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
4+
function toUtf8(bytes) {
5+
return Buffer.from(bytes || []).toString();
6+
}
7+
exports.toUtf8 = toUtf8;
48
function toHex(bytes) {
59
return Buffer.from(bytes || []).toString("hex");
610
}

src/cjs/index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export declare function toUtf8(bytes: Uint8Array): string;
12
export declare function toHex(bytes: Uint8Array): string;
23
export declare function fromHex(hexString: string): Uint8Array;
34
export declare type CompareResult = -1 | 0 | 1;

src/mjs/browser.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ const HEX_CODEPOINTS = Array(256)
99
return index < 0 ? undefined : index < 16 ? index : index - 6;
1010
});
1111
const ENCODER = new TextEncoder();
12-
const DECODER = new TextDecoder("ascii");
12+
const DECODER = new TextDecoder();
13+
export function toUtf8(bytes) {
14+
return DECODER.decode(bytes);
15+
}
1316
// There are two implementations.
1417
// One optimizes for length of the bytes, and uses TextDecoder.
1518
// One optimizes for iteration count, and appends strings.

src/mjs/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
export function toUtf8(bytes) {
2+
return Buffer.from(bytes || []).toString();
3+
}
14
export function toHex(bytes) {
25
return Buffer.from(bytes || []).toString("hex");
36
}

ts_src/browser.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ const HEX_CODEPOINTS: (number | undefined)[] = Array(256)
99
return index < 0 ? undefined : index < 16 ? index : index - 6;
1010
});
1111
const ENCODER = new TextEncoder();
12-
const DECODER = new TextDecoder("ascii");
12+
const DECODER = new TextDecoder();
13+
14+
export function toUtf8(bytes: Uint8Array): string {
15+
return DECODER.decode(bytes);
16+
}
1317

1418
// There are two implementations.
1519
// One optimizes for length of the bytes, and uses TextDecoder.

ts_src/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
export function toUtf8(bytes: Uint8Array): string {
2+
return Buffer.from(bytes || []).toString();
3+
}
4+
15
export function toHex(bytes: Uint8Array): string {
26
return Buffer.from(bytes || []).toString("hex");
37
}

ts_src/tests.spec.ts

+14
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ const bytes2Larger = f([0xff, 0x01, 0x00]);
1414
const bytes2LargerLeft = f([0x00, 0xff, 0x01]);
1515
const longBytes = new Uint8Array(513).fill(0xfa);
1616
const longHex = "fa".repeat(513);
17+
const bytes3 = f([0x21, 0x7e]);
18+
const utf8 = "!~";
19+
const longBytes2 = new Uint8Array(513).fill(0x61);
20+
const longUtf8 = "a".repeat(513);
21+
const testBytes = f([
22+
227, 129, 147, 227, 130, 147, 227, 129, 171, 227, 129, 161, 227, 129, 175,
23+
]);
24+
const str = "こんにちは";
1725

1826
const brokenHexes = [
1927
[" ff00", f([]), "leading space"],
@@ -39,6 +47,12 @@ describe(`Uint8Array tools`, () => {
3947
expect(tools.toHex(longBytes)).toEqual(longHex);
4048
expect((tools.toHex as any)()).toEqual("");
4149
});
50+
it(`should output utf8 with toUtf8`, () => {
51+
expect(tools.toUtf8(bytes3)).toEqual(utf8);
52+
expect(tools.toUtf8(testBytes)).toEqual(str);
53+
expect(tools.toUtf8(longBytes2)).toEqual(longUtf8);
54+
expect((tools.toUtf8 as any)()).toEqual("");
55+
});
4256
it(`should compare Uint8Arrays`, () => {
4357
expect(tools.compare(bytes, bytes2)).toBe(-1);
4458
expect(tools.compare(bytes, bytes)).toBe(0);

0 commit comments

Comments
 (0)