From d9fb3e28dd9226cd8fada435667c794f0eff7e7f Mon Sep 17 00:00:00 2001 From: wjaaaaaaat <04amid.foyer@icloud.com> Date: Mon, 17 Feb 2025 14:52:18 -0500 Subject: [PATCH] consistency with ../Base64.mjs removeNonAlphChars and strictMode params --- src/core/lib/Hex.mjs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/core/lib/Hex.mjs b/src/core/lib/Hex.mjs index 63d8c7eb..1f4bd423 100644 --- a/src/core/lib/Hex.mjs +++ b/src/core/lib/Hex.mjs @@ -100,10 +100,13 @@ export function toHexFast(data) { * // returns [10,20,30] * fromHex("0a:14:1e", "Colon"); */ -export function fromHex(data, delim="Auto", byteLen=2, strict=False) { +export function fromHex(data, delim="Auto", byteLen=2, removeNonAlphChars=false, strictMode=false) { if (byteLen < 1 || Math.round(byteLen) !== byteLen) throw new OperationError("Byte length must be a positive integer"); + if (removeNonAlphChars) + data = data.replace(/[^\da-fA-F]/g, ''); + if (delim !== "None") { const delimRegex = delim === "Auto" ? /\s+|0x/gi : Utils.regexRep(delim); data = data.split(delimRegex); @@ -113,7 +116,7 @@ export function fromHex(data, delim="Auto", byteLen=2, strict=False) { const output = []; for (let i = 0; i < data.length; i++) { - if (/[^a-f\d\s]/.test(data[i]) && strict) + if (/[^a-f\d\s]/.test(data[i]) && strictMode) throw new OperationError("Hex input must only contain hex digits"); for (let j = 0; j < data[i].length; j += byteLen) { output.push(parseInt(data[i].substr(j, byteLen), 16));