Skip to content

Commit

Permalink
fix: update HEX_REGEX to allow spaces and new lines
Browse files Browse the repository at this point in the history
  • Loading branch information
microshine committed Nov 22, 2024
1 parent ba25aa9 commit 8331891
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ declare function atob(data: string): string;

const STRING_TYPE = "string";

const HEX_REGEX = /^[0-9a-f]+$/i;
const HEX_REGEX = /^[0-9a-f\s]+$/i;
const BASE64_REGEX = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
const BASE64URL_REGEX = /^[a-zA-Z0-9-_]+$/;

Expand Down
7 changes: 7 additions & 0 deletions test/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ describe("Convert", () => {
const hex = Convert.ToHex(buf);
assert.strictEqual(hex, "010203");
});
it("decode with space and new line chars", () => {
const buf = Convert.FromHex(" 01\n02\r03 04\n");
assert.strictEqual(buf.byteLength, 4);
});
});

context("utf16", () => {
Expand Down Expand Up @@ -129,6 +133,9 @@ describe("Convert", () => {
it("wrong", () => {
assert.strictEqual(Convert.isHex("1234567890ABCDEF!"), false);
});
it("spaces and new lines", () => {
assert.strictEqual(Convert.isHex("1234\n5678 90AB\rCDEF"), true);
});
});

context("isBase64", () => {
Expand Down

0 comments on commit 8331891

Please sign in to comment.