UNPKG

677 BJavaScriptView Raw
1import { xglobal } from '@polkadot/x-global';
2import { hasBuffer } from '../has.js';
3/**
4 * @name u8aToBuffer
5 * @summary Creates a Buffer object from a hex string.
6 * @description
7 * `null` inputs returns an empty `Buffer` result. `UInt8Array` input values return the actual bytes value converted to a `Buffer`. Anything that is not a `UInt8Array` throws an error.
8 * @example
9 * <BR>
10 *
11 * ```javascript
12 * import { u8aToBuffer } from '@polkadot/util';
13 *
14 * console.log('Buffer', u8aToBuffer(new Uint8Array([1, 2, 3])));
15 * ```
16 */
17export function u8aToBuffer(value) {
18 return hasBuffer
19 ? xglobal.Buffer.from(value || [])
20 : new Uint8Array(value || []);
21}