Minor readability improvements (#952)
* Minor readability improvements * trigger * Additional * Additional
This commit is contained in:
@@ -23,11 +23,10 @@ import { u8aToU8a } from './toU8a';
|
||||
export function u8aConcat (...list: (Buffer | Uint8Array | number[] | string)[]): Uint8Array {
|
||||
let length = 0;
|
||||
let offset = 0;
|
||||
const u8as = new Array(list.length) as Uint8Array[];
|
||||
const u8as = new Array<Uint8Array>(list.length);
|
||||
|
||||
for (let i = 0; i < list.length; i++) {
|
||||
u8as[i] = u8aToU8a(list[i]);
|
||||
|
||||
length += u8as[i].length;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,5 @@
|
||||
* ```
|
||||
*/
|
||||
export function u8aToBuffer (value?: Uint8Array | null): Buffer {
|
||||
return !value
|
||||
? Buffer.from([])
|
||||
: Buffer.from(value);
|
||||
return Buffer.from(value || []);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ const ALPHABET = new Array(256).fill(0).map((_, n) => n.toString(16).padStart(2,
|
||||
|
||||
/** @internal */
|
||||
function extract (value: Uint8Array): string {
|
||||
const result = new Array(value.length) as string[];
|
||||
const result = new Array<string>(value.length);
|
||||
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
result[i] = ALPHABET[value[i]];
|
||||
@@ -44,8 +44,9 @@ export function u8aToHex (value?: Uint8Array | null, bitLength = -1, isPrefixed
|
||||
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
|
||||
return prefix + ((byteLength > 0 && value.length > byteLength)
|
||||
? trim(value, Math.ceil(byteLength / 2))
|
||||
: extract(value)
|
||||
return prefix + (
|
||||
(byteLength > 0 && value.length > byteLength)
|
||||
? trim(value, Math.ceil(byteLength / 2))
|
||||
: extract(value)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -40,6 +40,14 @@ describe('u8aToU8a', (): void => {
|
||||
);
|
||||
});
|
||||
|
||||
it('creates from a Buffer', (): void => {
|
||||
expect(
|
||||
u8aToU8a(Buffer.from([1, 2, 3, 128, 0, 10, 11, 12]))
|
||||
).toEqual(
|
||||
new Uint8Array([1, 2, 3, 128, 0, 10, 11, 12])
|
||||
);
|
||||
});
|
||||
|
||||
it('returns a Uint8Array as-is (u8a input)', (): void => {
|
||||
expect(
|
||||
u8aToU8a(new Uint8Array([128, 0, 10]))
|
||||
|
||||
@@ -1,25 +1,12 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { bufferToU8a } from '../buffer/toU8a';
|
||||
import { hexToU8a } from '../hex/toU8a';
|
||||
import { isBuffer } from '../is/buffer';
|
||||
import { isHex } from '../is/hex';
|
||||
import { isString } from '../is/string';
|
||||
import { stringToU8a } from '../string/toU8a';
|
||||
|
||||
function convertArray (value: number[] | Uint8Array): Uint8Array {
|
||||
return Array.isArray(value)
|
||||
? Uint8Array.from(value)
|
||||
: value;
|
||||
}
|
||||
|
||||
function convertString (value: string): Uint8Array {
|
||||
return isHex(value)
|
||||
? hexToU8a(value)
|
||||
: stringToU8a(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aToU8a
|
||||
* @summary Creates a Uint8Array value from a Uint8Array, Buffer, string or hex input.
|
||||
@@ -29,7 +16,7 @@ function convertString (value: string): Uint8Array {
|
||||
* <BR>
|
||||
*
|
||||
* ```javascript
|
||||
* import { { u8aToU8a } from '@polkadot/util';
|
||||
* import { u8aToU8a } from '@polkadot/util';
|
||||
*
|
||||
* u8aToU8a(new Uint8Array([0x12, 0x34]); // => Uint8Array([0x12, 0x34])
|
||||
* u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34])
|
||||
@@ -38,11 +25,13 @@ function convertString (value: string): Uint8Array {
|
||||
export function u8aToU8a (value?: number[] | Buffer | Uint8Array | string | null): Uint8Array {
|
||||
if (!value) {
|
||||
return new Uint8Array();
|
||||
} else if (isBuffer(value)) {
|
||||
return bufferToU8a(value);
|
||||
} else if (isString(value)) {
|
||||
return convertString(value);
|
||||
return isHex(value)
|
||||
? hexToU8a(value)
|
||||
: stringToU8a(value);
|
||||
}
|
||||
|
||||
return convertArray(value);
|
||||
return Array.isArray(value) || isBuffer(value)
|
||||
? new Uint8Array(value)
|
||||
: value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user