Skip to content

Commit

Permalink
refactor: add method to validate length
Browse files Browse the repository at this point in the history
  • Loading branch information
saraantole committed Jan 30, 2025
1 parent 33ca4f4 commit 337822e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/core/src/vcdm/HexUInt32.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ class HexUInt32 extends HexUInt {
*/
private static readonly BYTE_LENGTH = 32;

/**
* Regular expression for matching 32 bytes hexadecimal strings.
* An empty input is represented as a empty digits.
*
* @type {RegExp}
*/
private static readonly REGEX_HEXUINT32: RegExp = /^(0x)?[0-9a-f]{64}$/i;

/**
* Checks if the given string expression is a valid 32 bytes unsigned hexadecimal value.
*
* @param {string} exp - The string representation of a hexadecimal value.
*
* @return {boolean} - True if the expression is a valid 32 bytes unsigned hexadecimal value, case-insensitive,
* optionally prefixed with `0x`; false otherwise.
*/
public static isValid(exp: string): boolean {
return HexUInt32.REGEX_HEXUINT32.test(exp);
}

/**
* Ensures the hexadecimal string representation is 32 bytes long.
* @param {string} hexString - The hexadecimal string to validate.
Expand Down
7 changes: 7 additions & 0 deletions packages/core/tests/vcdm/HexUInt32.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ describe('HexUInt32 class tests', () => {
expect(hexUInt32.toString()).toEqual(expectedHexUInt32);
});

test('Checks if the provided expression is a valid 32 bytes unsigned hexadecimal value', () => {
const expression =
'0x0000000000000000000000000000000000000000000000000000000000caffee';

expect(HexUInt32.isValid(expression)).toBeTruthy();
});

test('Throw an error if the provided argument is not an unsigned expression', () => {
const exp = '-0xnotUnsigned';
expect(() => HexUInt32.of(exp)).toThrow(InvalidDataType);
Expand Down

0 comments on commit 337822e

Please sign in to comment.