UNPKG

601 BJavaScriptView Raw
1import { hexHasPrefix } from './hasPrefix.js';
2/**
3 * @name hexAddPrefix
4 * @summary Adds the `0x` prefix to string values.
5 * @description
6 * Returns a `0x` prefixed string from the input value. If the input is already prefixed, it is returned unchanged.
7 * @example
8 * <BR>
9 *
10 * ```javascript
11 * import { hexAddPrefix } from '@polkadot/util';
12 *
13 * console.log('With prefix', hexAddPrefix('0a0b12')); // => 0x0a0b12
14 * ```
15 */
16export function hexAddPrefix(value) {
17 return value && hexHasPrefix(value)
18 ? value
19 : `0x${value && value.length % 2 === 1 ? '0' : ''}${value || ''}`;
20}