Skip to content

Commit

Permalink
feat(common): add hexSize method
Browse files Browse the repository at this point in the history
  • Loading branch information
capt-nemo429 committed Mar 27, 2023
1 parent b18b6e8 commit 78f724b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/common/src/utils/stringUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isHex } from "./stringUtils";
import { hexSize, isHex } from "./stringUtils";

describe("isHex() test", () => {
it("Should pass with VALID hex strings", () => {
Expand All @@ -18,4 +18,10 @@ describe("isHex() test", () => {
expect(isHex("")).toBeFalsy();
expect(isHex(undefined)).toBeFalsy();
});

it("Should return the byte size of a hex string", () => {
expect(
hexSize("0008cd026dc059d64a50d0dbf07755c2c4a4e557e3df8afa7141868b3ab200643d437ee7")
).toBe(36);
});
});
9 changes: 9 additions & 0 deletions packages/common/src/utils/stringUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ export function isHex(value?: string) {

return HEX_PATTERN.test(value);
}

/**
* Get hex string size in bytes
* @param hex
* @returns the byte size if the hex string
*/
export function hexSize(hex: string): number {
return hex.length / 2;
}

0 comments on commit 78f724b

Please sign in to comment.