Adjust isHex with template return (#1103)

* Adjust isHex with template return

* Unneeded assignment

* CHANGELOG

* typo

* Additional HexString types

* Additional HexString inputs

* Use U8aLike as applicable

* typo

* Unneeded type

* HexString outputs

* Export types
This commit is contained in:
Jaco
2021-09-08 14:38:53 +02:00
committed by GitHub
parent 6b9b7fa7f5
commit 9fc57a23e8
71 changed files with 233 additions and 122 deletions
+3 -1
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '../types';
import { u8aToU8a } from './toU8a';
function compare (a: Uint8Array, b: Uint8Array): number {
@@ -46,6 +48,6 @@ function compare (a: Uint8Array, b: Uint8Array): number {
* u8aCmp(new Uint8Array([0x69, 0x65]), new Uint8Array([0x68, 0x65])); // +1
* ```
*/
export function u8aCmp (a: Uint8Array | string, b: Uint8Array | string): number {
export function u8aCmp (a: HexString | Uint8Array | string, b: HexString | Uint8Array | string): number {
return compare(u8aToU8a(a), u8aToU8a(b));
}
+3 -1
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { U8aLike } from '../types';
import { u8aToU8a } from './toU8a';
/**
@@ -20,7 +22,7 @@ import { u8aToU8a } from './toU8a';
* ); // [1, 2, 3, 4, 5, 6]
* ```
*/
export function u8aConcat (...list: (Buffer | Uint8Array | number[] | string)[]): Uint8Array {
export function u8aConcat (...list: U8aLike[]): Uint8Array {
let length = 0;
let offset = 0;
const u8as = new Array<Uint8Array>(list.length);
+3 -1
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '../types';
import { u8aCmp } from './cmp';
import { u8aToU8a } from './toU8a';
@@ -22,6 +24,6 @@ function equals (a: Uint8Array, b: Uint8Array): boolean {
* u8aEq(new Uint8Array([0x68, 0x65]), new Uint8Array([0x68, 0x65])); // true
* ```
*/
export function u8aEq (a: Uint8Array | string, b: Uint8Array | string): boolean {
export function u8aEq (a: HexString | Uint8Array | string, b: HexString | Uint8Array | string): boolean {
return equals(u8aToU8a(a), u8aToU8a(b));
}
+1 -5
View File
@@ -28,11 +28,7 @@ export function u8aFixLength (value: Uint8Array, bitLength = -1, atStart = false
const result = new Uint8Array(byteLength);
if (atStart) {
result.set(value, 0);
} else {
result.set(value, byteLength - value.length);
}
result.set(value, atStart ? 0 : (byteLength - value.length));
return result;
}
+22 -19
View File
@@ -1,13 +1,17 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexDigit, HexString } from '../types';
import { arrayRange } from '../array';
const ALPHABET = arrayRange(256).map((n) => n.toString(16).padStart(2, '0'));
type HexByte = `${HexDigit}${HexDigit}`;
const ALPHABET = arrayRange(256).map((n) => n.toString(16).padStart(2, '0') as HexByte);
/** @internal */
function extract (value: Uint8Array): string {
const result = new Array<string>(value.length);
const result = new Array<HexByte>(value.length);
for (let i = 0; i < value.length; i++) {
result[i] = ALPHABET[value[i]];
@@ -16,9 +20,18 @@ function extract (value: Uint8Array): string {
return result.join('');
}
/** @internal */
function unprefixed (value: Uint8Array, bitLength = -1): string {
const byteLength = Math.ceil(bitLength / 8);
return (byteLength > 0 && value.length > byteLength)
? trim(value, Math.ceil(byteLength / 2))
: extract(value);
}
/** @internal */
function trim (value: Uint8Array, halfLength: number): string {
return `${u8aToHex(value.subarray(0, halfLength), -1, false)}…${u8aToHex(value.subarray(value.length - halfLength), -1, false)}`;
return `${unprefixed(value.subarray(0, halfLength))}…${unprefixed(value.subarray(value.length - halfLength))}`;
}
/**
@@ -35,20 +48,10 @@ function trim (value: Uint8Array, halfLength: number): string {
* u8aToHex(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0xf])); // 0x68656c0f
* ```
*/
export function u8aToHex (value?: Uint8Array | null, bitLength = -1, isPrefixed = true): string {
const prefix = isPrefixed
? '0x'
: '';
if (!value?.length) {
return prefix;
}
const byteLength = Math.ceil(bitLength / 8);
return prefix + (
(byteLength > 0 && value.length > byteLength)
? trim(value, Math.ceil(byteLength / 2))
: extract(value)
);
export function u8aToHex (value?: Uint8Array | null, bitLength = -1, isPrefixed = true): HexString {
return `${isPrefixed ? '0x' : ''}${
!value || !value.length
? ''
: unprefixed(value, bitLength)
}` as HexString;
}
+3 -1
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { U8aLike } from '../types';
import { assert } from '../assert';
import { hexToU8a } from '../hex/toU8a';
import { isBuffer } from '../is/buffer';
@@ -24,7 +26,7 @@ import { stringToU8a } from '../string/toU8a';
* u8aToU8a(0x1234); // => Uint8Array([0x12, 0x34])
* ```
*/
export function u8aToU8a (value?: number[] | Buffer | Uint8Array | string | null): Uint8Array {
export function u8aToU8a (value?: U8aLike | null): Uint8Array {
if (!value) {
return new Uint8Array();
} else if (isHex(value)) {