UNPKG

600 BJavaScriptView Raw
1import { TextDecoder } from '@polkadot/x-textdecoder';
2const decoder = new TextDecoder('utf-8');
3/**
4 * @name u8aToString
5 * @summary Creates a utf-8 string from a Uint8Array object.
6 * @description
7 * `UInt8Array` input values return the actual decoded utf-8 string. `null` or `undefined` values returns an empty string.
8 * @example
9 * <BR>
10 *
11 * ```javascript
12 * import { u8aToString } from '@polkadot/util';
13 *
14 * u8aToString(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello
15 * ```
16 */
17export function u8aToString(value) {
18 return value
19 ? decoder.decode(value)
20 : '';
21}