UNPKG

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