UNPKG

793 BJavaScriptView Raw
1import { REGEX_HEX_NOPREFIX, REGEX_HEX_PREFIXED } from '../is/hex.js';
2/**
3 * @name hexStripPrefix
4 * @summary Strips any leading `0x` prefix.
5 * @description
6 * Tests for the existence of a `0x` prefix, and returns the value without the prefix. Un-prefixed values are returned as-is.
7 * @example
8 * <BR>
9 *
10 * ```javascript
11 * import { hexStripPrefix } from '@polkadot/util';
12 *
13 * console.log('stripped', hexStripPrefix('0x1234')); // => 1234
14 * ```
15 */
16export function hexStripPrefix(value) {
17 if (!value || value === '0x') {
18 return '';
19 }
20 else if (REGEX_HEX_PREFIXED.test(value)) {
21 return value.substring(2);
22 }
23 else if (REGEX_HEX_NOPREFIX.test(value)) {
24 return value;
25 }
26 throw new Error(`Expected hex value to convert, found '${value}'`);
27}
\No newline at end of file