UNPKG

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