UNPKG

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