Files
common/packages/util/src/buffer/toU8a.ts
T
Jaco 785a23433d Flatten Buffer | Uint8Array types (#1816)
* Flatten Buffer | Uint8Array types

* Remove dangling Buffer references

* CHANGELOG

* Adjust
2023-04-24 07:53:31 +03:00

21 lines
626 B
TypeScript

// Copyright 2017-2023 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
/**
* @name bufferToU8a
* @summary Creates a Uint8Array value from a Buffer object.
* @description
* `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.
* @example
* <BR>
*
* ```javascript
* import { bufferToU8a } from '@polkadot/util';
*
* bufferToU8a(Buffer.from([1, 2, 3]));
* ```
*/
export function bufferToU8a (buffer?: Uint8Array | number[] | null): Uint8Array {
return new Uint8Array(buffer || []);
}