Skip to content

Commit d9fb3e2

Browse files
authored
consistency with ../Base64.mjs
removeNonAlphChars and strictMode params
1 parent 7acd9a3 commit d9fb3e2

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/core/lib/Hex.mjs

+5-2
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,13 @@ export function toHexFast(data) {
100100
* // returns [10,20,30]
101101
* fromHex("0a:14:1e", "Colon");
102102
*/
103-
export function fromHex(data, delim="Auto", byteLen=2, strict=False) {
103+
export function fromHex(data, delim="Auto", byteLen=2, removeNonAlphChars=false, strictMode=false) {
104104
if (byteLen < 1 || Math.round(byteLen) !== byteLen)
105105
throw new OperationError("Byte length must be a positive integer");
106106

107+
if (removeNonAlphChars)
108+
data = data.replace(/[^\da-fA-F]/g, '');
109+
107110
if (delim !== "None") {
108111
const delimRegex = delim === "Auto" ? /\s+|0x/gi : Utils.regexRep(delim);
109112
data = data.split(delimRegex);
@@ -113,7 +116,7 @@ export function fromHex(data, delim="Auto", byteLen=2, strict=False) {
113116

114117
const output = [];
115118
for (let i = 0; i < data.length; i++) {
116-
if (/[^a-f\d\s]/.test(data[i]) && strict)
119+
if (/[^a-f\d\s]/.test(data[i]) && strictMode)
117120
throw new OperationError("Hex input must only contain hex digits");
118121
for (let j = 0; j < data[i].length; j += byteLen) {
119122
output.push(parseInt(data[i].substr(j, byteLen), 16));

0 commit comments

Comments
 (0)