@polkadot/util
Version:
A collection of useful utilities for @polkadot
37 lines (32 loc) • 953 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hexStripPrefix = hexStripPrefix;
var _hex = require("../is/hex");
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
/**
* @name hexStripPrefix
* @summary Strips any leading `0x` prefix.
* @description
* Tests for the existence of a `0x` prefix, and returns the value without the prefix. Un-prefixed values are returned as-is.
* @example
* <BR>
*
* ```javascript
* import { hexStripPrefix } from '@polkadot/util';
*
* console.log('stripped', hexStripPrefix('0x1234')); // => 1234
* ```
*/
function hexStripPrefix(value) {
if (!value || value === '0x') {
return '';
} else if (_hex.REGEX_HEX_PREFIXED.test(value)) {
return value.substring(2);
} else if (_hex.REGEX_HEX_NOPREFIX.test(value)) {
return value;
}
throw new Error(`Expected hex value to convert, found '${value}'`);
}