1 | export const REGEX_HEX_PREFIXED = /^0x[\da-fA-F]+$/;
|
2 | export const REGEX_HEX_NOPREFIX = /^[\da-fA-F]+$/;
|
3 | /**
|
4 | * @name isHex
|
5 | * @summary Tests for a hex string.
|
6 | * @description
|
7 | * Checks to see if the input value is a `0x` prefixed hex string. Optionally (`bitLength` !== -1) checks to see if the bitLength is correct.
|
8 | * @example
|
9 | * <BR>
|
10 | *
|
11 | * ```javascript
|
12 | * import { isHex } from '@polkadot/util';
|
13 | *
|
14 | * isHex('0x1234'); // => true
|
15 | * isHex('0x1234', 8); // => false
|
16 | * ```
|
17 | */
|
18 | export function isHex(value, bitLength = -1, ignoreLength) {
|
19 | return (typeof value === 'string' && (value === '0x' ||
|
20 | REGEX_HEX_PREFIXED.test(value))) && (bitLength === -1
|
21 | ? (ignoreLength || (value.length % 2 === 0))
|
22 | : (value.length === (2 + Math.ceil(bitLength / 4))));
|
23 | }
|