UNPKG

581 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3
4/**
5 * @name bufferToU8a
6 * @summary Creates a Uint8Array value from a Buffer object.
7 * @description
8 * `null` inputs returns an empty result, `Buffer` values return the actual value as a `Uint8Array`. Anything that is not a `Buffer` object throws an error.
9 * @example
10 * <BR>
11 *
12 * ```javascript
13 * import { bufferToU8a } from '@polkadot/util';
14 *
15 * bufferToU8a(Buffer.from([1, 2, 3]));
16 * ```
17 */
18export function bufferToU8a(buffer) {
19 return new Uint8Array(buffer || []);
20}
\No newline at end of file