UNPKG

730 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { TextDecoder } from '@polkadot/x-textdecoder';
4const decoder = new TextDecoder('utf-8');
5/**
6 * @name u8aToString
7 * @summary Creates a utf-8 string from a Uint8Array object.
8 * @description
9 * `UInt8Array` input values return the actual decoded utf-8 string. `null` or `undefined` values returns an empty string.
10 * @example
11 * <BR>
12 *
13 * ```javascript
14 * import { u8aToString } from '@polkadot/util';
15 *
16 * u8aToString(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello
17 * ```
18 */
19
20export function u8aToString(value) {
21 return !(value !== null && value !== void 0 && value.length) ? '' : decoder.decode(value);
22}
\No newline at end of file