UNPKG

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