UNPKG

744 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { TextEncoder } from '@polkadot/x-textencoder';
4const encoder = new TextEncoder();
5/**
6 * @name stringToU8a
7 * @summary Creates a Uint8Array object from a utf-8 string.
8 * @description
9 * String input values return the actual encoded `UInt8Array`. `null` or `undefined` values returns an empty encoded array.
10 * @example
11 * <BR>
12 *
13 * ```javascript
14 * import { stringToU8a } from '@polkadot/util';
15 *
16 * stringToU8a('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
17 * ```
18 */
19// eslint-disable-next-line @typescript-eslint/ban-types
20
21export function stringToU8a(value) {
22 return value ? encoder.encode(value.toString()) : new Uint8Array();
23}
\No newline at end of file