Skip to content

feat: function to convert Utf8 string to bytes #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion src/cjs/browser.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
exports.compare = exports.fromHex = exports.fromUtf8 = 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)
@@ -42,6 +42,10 @@ function _toHexLengthPerf(bytes) {
}
return DECODER.decode(hexBytes);
}
function fromUtf8(utf8String) {
return ENCODER.encode(utf8String);
}
exports.fromUtf8 = fromUtf8;
// Mimics Buffer.from(x, 'hex') logic
// Stops on first non-hex string and returns
// https://github.com/nodejs/node/blob/v14.18.1/src/string_bytes.cc#L246-L261
6 changes: 5 additions & 1 deletion src/cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.compare = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
exports.compare = exports.fromUtf8 = exports.fromHex = exports.toHex = exports.toUtf8 = void 0;
function toUtf8(bytes) {
return Buffer.from(bytes || []).toString();
}
@@ -13,6 +13,10 @@ function fromHex(hexString) {
return Uint8Array.from(Buffer.from(hexString || "", "hex"));
}
exports.fromHex = fromHex;
function fromUtf8(utf8String) {
return Uint8Array.from(Buffer.from(utf8String || ""));
}
exports.fromUtf8 = fromUtf8;
function compare(v1, v2) {
return Buffer.from(v1).compare(Buffer.from(v2));
}
1 change: 1 addition & 0 deletions src/cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export declare function toUtf8(bytes: Uint8Array): string;
export declare function toHex(bytes: Uint8Array): string;
export declare function fromHex(hexString: string): Uint8Array;
export declare function fromUtf8(utf8String: string): Uint8Array;
export declare type CompareResult = -1 | 0 | 1;
export declare function compare(v1: Uint8Array, v2: Uint8Array): CompareResult;
3 changes: 3 additions & 0 deletions src/mjs/browser.js
Original file line number Diff line number Diff line change
@@ -37,6 +37,9 @@ function _toHexLengthPerf(bytes) {
}
return DECODER.decode(hexBytes);
}
export function fromUtf8(utf8String) {
return ENCODER.encode(utf8String);
}
// Mimics Buffer.from(x, 'hex') logic
// Stops on first non-hex string and returns
// https://github.com/nodejs/node/blob/v14.18.1/src/string_bytes.cc#L246-L261
3 changes: 3 additions & 0 deletions src/mjs/index.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,9 @@ export function toHex(bytes) {
export function fromHex(hexString) {
return Uint8Array.from(Buffer.from(hexString || "", "hex"));
}
export function fromUtf8(utf8String) {
return Uint8Array.from(Buffer.from(utf8String || ""));
}
export function compare(v1, v2) {
return Buffer.from(v1).compare(Buffer.from(v2));
}
4 changes: 4 additions & 0 deletions ts_src/browser.ts
Original file line number Diff line number Diff line change
@@ -40,6 +40,10 @@ function _toHexLengthPerf(bytes: Uint8Array): string {
return DECODER.decode(hexBytes);
}

export function fromUtf8(utf8String: string): Uint8Array {
return ENCODER.encode(utf8String);
}

// Mimics Buffer.from(x, 'hex') logic
// Stops on first non-hex string and returns
// https://github.com/nodejs/node/blob/v14.18.1/src/string_bytes.cc#L246-L261
4 changes: 4 additions & 0 deletions ts_src/index.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,10 @@ export function fromHex(hexString: string): Uint8Array {
return Uint8Array.from(Buffer.from(hexString || "", "hex"));
}

export function fromUtf8(utf8String: string): Uint8Array {
return Uint8Array.from(Buffer.from(utf8String || ""));
}

export type CompareResult = -1 | 0 | 1;
export function compare(v1: Uint8Array, v2: Uint8Array): CompareResult {
return Buffer.from(v1).compare(Buffer.from(v2)) as CompareResult;
4 changes: 4 additions & 0 deletions ts_src/tests.spec.ts
Original file line number Diff line number Diff line change
@@ -42,6 +42,10 @@ describe(`Uint8Array tools`, () => {
expect(tools.fromHex(bhex)).toEqual(result);
});
}
it(`should parse hex with fromUtf8`, () => {
expect(tools.fromUtf8(utf8)).toEqual(bytes3);
expect((tools.fromUtf8 as any)()).toEqual(f([]));
});
it(`should output hex with toHex`, () => {
expect(tools.toHex(bytes)).toEqual(hex);
expect(tools.toHex(longBytes)).toEqual(longHex);