@tetcoin/util
Version:
A collection of useful utilities for @tetcoin
45 lines (37 loc) • 1.17 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = hexStripPrefix;
var _hasPrefix = _interopRequireDefault(require("./hasPrefix"));
// Copyright 2017-2019 @polkadot/util authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
const UNPREFIX_HEX_REGEX = /^[a-fA-F0-9]+$/;
/**
* @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 '@tetcoin/util';
*
* console.log('stripped', hexStripPrefix('0x1234')); // => 1234
* ```
*/
function hexStripPrefix(value) {
if (!value) {
return '';
}
if ((0, _hasPrefix.default)(value)) {
return value.substr(2);
}
if (UNPREFIX_HEX_REGEX.test(value)) {
return value;
}
throw new Error("Invalid hex ".concat(value, " passed to hexStripPrefix"));
}