Files
common/packages/util/src/string/toU8a.ts
T
Jaco a4256de444 Small u8aToU8a optimizations (#1498)
* Small u8aToU8a optimizations

* Slight order adjustments

* Adjust
2022-05-24 16:42:58 +03:00

29 lines
774 B
TypeScript

// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
import { TextEncoder } from '@polkadot/x-textencoder';
const encoder = new TextEncoder();
/**
* @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 '@polkadot/util';
*
* stringToU8a('hello'); // [0x68, 0x65, 0x6c, 0x6c, 0x6f]
* ```
*/
export function stringToU8a (value?: AnyString | null): Uint8Array {
return value
? encoder.encode(value.toString())
: new Uint8Array();
}