mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-24 08:58:00 +00:00
chore: update to version 14.0.11 and align website URLs
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { hexToU8a } from '../hex/toU8a.js';
|
||||
import { isBuffer } from '../is/buffer.js';
|
||||
import { isHex } from '../is/hex.js';
|
||||
import { isU8a } from '../is/u8a.js';
|
||||
import { stringToU8a } from '../string/toU8a.js';
|
||||
/**
|
||||
* @name u8aToU8a
|
||||
* @summary Creates a Uint8Array value from a Uint8Array, Buffer, string or hex input.
|
||||
* @description
|
||||
* `null` or `undefined` inputs returns a `[]` result, Uint8Array values returns the value, hex strings returns a Uint8Array representation.
|
||||
* If `strict` is true, `null` or `undefined` will throw an error instead of returning an empty array.
|
||||
* Supports input types: Uint8Array, Buffer, hex string, string, or number array.
|
||||
* @example
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { u8aToU8a } from '@pezkuwi/util';
|
||||
*
|
||||
* u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34])
|
||||
* u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34])
|
||||
* ```
|
||||
*/
|
||||
export function u8aToU8a(value, strict = false) {
|
||||
if (strict && (value === null || value === undefined)) {
|
||||
throw new Error('u8aToU8a: Expected non-null, non-undefined value');
|
||||
}
|
||||
return isU8a(value)
|
||||
// NOTE isBuffer needs to go here since it actually extends
|
||||
// Uint8Array on Node.js environments, so all Buffer are Uint8Array,
|
||||
// but Uint8Array is not Buffer
|
||||
? isBuffer(value)
|
||||
? new Uint8Array(value)
|
||||
: value
|
||||
: isHex(value)
|
||||
? hexToU8a(value)
|
||||
: Array.isArray(value)
|
||||
? new Uint8Array(value)
|
||||
: stringToU8a(value);
|
||||
}
|
||||
Reference in New Issue
Block a user