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