UNPKG

@tetcoin/util

Version:
50 lines (42 loc) 1.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = stringToU8a; // Copyright 2017-2019 @polkadot/util authors & contributors // This software may be modified and distributed under the terms let encoder; function polyfilledEncode(value) { const u8a = new Uint8Array(value.length); for (let i = 0; i < value.length; i++) { u8a[i] = value.charCodeAt(i); } return u8a; } try { encoder = new TextEncoder(); } catch (error) { encoder = { encode: polyfilledEncode }; } /** * @name stringToU8a * @summary Creates a Uint8Array object from a utf-8 string. * @description * String input values return the actual encoded `UInt8Array`. `null` or `undefined` values returns an empty encoded array. * @example * <BR> * * ```javascript * import { stringToU8a } from '@tetcoin/util'; * * stringToU8a('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f] * ``` */ function stringToU8a(value) { if (!value) { return new Uint8Array([]); } return encoder.encode(value); }