add hexToUtf8, utf8ToHex

This commit is contained in:
CounterFire2023 2024-01-18 11:51:14 +08:00
parent 1bbbd4f389
commit 104626bf72
9 changed files with 58 additions and 9 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -24,11 +24,13 @@ __export(string_util_exports, {
compressUuid: () => compressUuid,
hexToBase32: () => hexToBase32,
hexToBase58: () => hexToBase58,
hexToUtf8: () => hexToUtf8,
isObjectId: () => isObjectId,
isTrue: () => isTrue,
isUUID: () => isUUID,
string10to62: () => string10to62,
string62to10: () => string62to10
string62to10: () => string62to10,
utf8ToHex: () => utf8ToHex
});
module.exports = __toCommonJS(string_util_exports);
function isTrue(obj) {
@ -125,6 +127,16 @@ function compressHex(e, r) {
function isUUID(uuid) {
return reNormalUUID.test(uuid);
}
function hexToUtf8(hexString) {
let _hexString = hexString.replace(/^0x/, "");
let buffer = Buffer.from(_hexString, "hex");
return buffer.toString("utf8");
}
function utf8ToHex(utf8String) {
const buffer = Buffer.from(utf8String, "utf8");
const hexString = buffer.toString("hex");
return hexString;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
base58ToHex,
@ -132,10 +144,12 @@ function isUUID(uuid) {
compressUuid,
hexToBase32,
hexToBase58,
hexToUtf8,
isObjectId,
isTrue,
isUUID,
string10to62,
string62to10
string62to10,
utf8ToHex
});
//# sourceMappingURL=string.util.cjs.map

File diff suppressed because one or more lines are too long

View File

@ -28,5 +28,7 @@ declare const hexToBase32: (hexString: string) => string;
declare function compressUuid(e: string, t?: boolean): string;
declare function compressHex(e: string, r: number): string;
declare function isUUID(uuid: string): boolean;
declare function hexToUtf8(hexString: any): string;
declare function utf8ToHex(utf8String: any): string;
export { base58ToHex, compressHex, compressUuid, hexToBase32, hexToBase58, isObjectId, isTrue, isUUID, string10to62, string62to10 };
export { base58ToHex, compressHex, compressUuid, hexToBase32, hexToBase58, hexToUtf8, isObjectId, isTrue, isUUID, string10to62, string62to10, utf8ToHex };

View File

@ -28,5 +28,7 @@ declare const hexToBase32: (hexString: string) => string;
declare function compressUuid(e: string, t?: boolean): string;
declare function compressHex(e: string, r: number): string;
declare function isUUID(uuid: string): boolean;
declare function hexToUtf8(hexString: any): string;
declare function utf8ToHex(utf8String: any): string;
export { base58ToHex, compressHex, compressUuid, hexToBase32, hexToBase58, isObjectId, isTrue, isUUID, string10to62, string62to10 };
export { base58ToHex, compressHex, compressUuid, hexToBase32, hexToBase58, hexToUtf8, isObjectId, isTrue, isUUID, string10to62, string62to10, utf8ToHex };

View File

@ -93,16 +93,28 @@ function compressHex(e, r) {
function isUUID(uuid) {
return reNormalUUID.test(uuid);
}
function hexToUtf8(hexString) {
let _hexString = hexString.replace(/^0x/, "");
let buffer = Buffer.from(_hexString, "hex");
return buffer.toString("utf8");
}
function utf8ToHex(utf8String) {
const buffer = Buffer.from(utf8String, "utf8");
const hexString = buffer.toString("hex");
return hexString;
}
export {
base58ToHex,
compressHex,
compressUuid,
hexToBase32,
hexToBase58,
hexToUtf8,
isObjectId,
isTrue,
isUUID,
string10to62,
string62to10
string62to10,
utf8ToHex
};
//# sourceMappingURL=string.util.js.map

File diff suppressed because one or more lines are too long

View File

@ -146,3 +146,22 @@ export function isUUID(uuid: string) {
return reNormalUUID.test(uuid)
}
export function hexToUtf8(hexString) {
// Remove any leading "0x" prefix and split into pairs of characters
let _hexString = hexString.replace(/^0x/, '')
let buffer = Buffer.from(_hexString, 'hex')
return buffer.toString('utf8')
}
export function utf8ToHex(utf8String) {
// Create a Buffer object from the UTF-8 string
const buffer = Buffer.from(utf8String, 'utf8')
// Convert the Buffer object to a hex string
const hexString = buffer.toString('hex')
return hexString
}