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:
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ToBn } from '../types';
|
||||
import type { HexString, ToBn } from '../types';
|
||||
|
||||
import { hexToBn } from '../hex/toBn';
|
||||
import { isBigInt } from '../is/bigInt';
|
||||
@@ -25,7 +25,7 @@ import { BN } from './bn';
|
||||
* bnToBn(new BN(0x1234)); // => BN(0x1234)
|
||||
* ```
|
||||
*/
|
||||
export function bnToBn <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | string | number | null): BN {
|
||||
export function bnToBn <ExtToBn extends ToBn> (value?: HexString | ExtToBn | BN | bigint | string | number | null): BN {
|
||||
if (!value) {
|
||||
return new BN(0);
|
||||
} else if (isHex(value)) {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ToBn, ToBnOptions } from '../types';
|
||||
import type { HexString, ToBn, ToBnOptions } from '../types';
|
||||
import type { BN } from './bn';
|
||||
|
||||
import { isNumber } from '../is/number';
|
||||
@@ -29,9 +29,9 @@ interface Options extends ToBnOptions {
|
||||
* bnToHex(new BN(0x123456)); // => '0x123456'
|
||||
* ```
|
||||
*/
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, options?: Options): string;
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): string;
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | Options = { bitLength: -1, isLe: false, isNegative: false }, arg2?: boolean): string {
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, options?: Options): HexString;
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, bitLength?: number, isLe?: boolean): HexString;
|
||||
function bnToHex <ExtToBn extends ToBn> (value?: ExtToBn | BN | bigint | number | null, arg1: number | Options = { bitLength: -1, isLe: false, isNegative: false }, arg2?: boolean): HexString {
|
||||
if (!value) {
|
||||
return ZERO_STR;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { U8aLike } from '../types';
|
||||
|
||||
import { BN, BN_FOUR } from '../bn';
|
||||
import { u8aToBn, u8aToU8a } from '../u8a';
|
||||
|
||||
@@ -18,7 +20,7 @@ import { u8aToBn, u8aToU8a } from '../u8a';
|
||||
* console.log('value offset=', offset, 'length=', length); // 4, 0xffff
|
||||
* ```
|
||||
*/
|
||||
export function compactFromU8a (_input: Buffer | Uint8Array | string | number[]): [number, BN] {
|
||||
export function compactFromU8a (_input: U8aLike): [number, BN] {
|
||||
const input = u8aToU8a(_input);
|
||||
const flag = input[0] & 0b11;
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types';
|
||||
|
||||
import { hexHasPrefix } from './hasPrefix';
|
||||
|
||||
/**
|
||||
@@ -17,7 +19,7 @@ import { hexHasPrefix } from './hasPrefix';
|
||||
* console.log('With prefix', hexAddPrefix('0a0b12')); // => 0x0a0b12
|
||||
* ```
|
||||
*/
|
||||
export function hexAddPrefix (value?: string | null): string {
|
||||
export function hexAddPrefix (value?: string | null): HexString {
|
||||
if (value && hexHasPrefix(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types';
|
||||
|
||||
import { hexAddPrefix } from './addPrefix';
|
||||
import { hexStripPrefix } from './stripPrefix';
|
||||
|
||||
@@ -20,7 +22,7 @@ import { hexStripPrefix } from './stripPrefix';
|
||||
* console.log('fixed', hexFixLength('0x0012', 8)); // => 0x12
|
||||
* ```
|
||||
*/
|
||||
export function hexFixLength (value: string, bitLength = -1, withPadding = false): string {
|
||||
export function hexFixLength (value: string, bitLength = -1, withPadding = false): HexString {
|
||||
const strLength = Math.ceil(bitLength / 4);
|
||||
const hexLength = strLength + 2;
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types';
|
||||
|
||||
import { isHex } from '../is/hex';
|
||||
|
||||
/**
|
||||
@@ -17,6 +19,6 @@ import { isHex } from '../is/hex';
|
||||
* console.log('has prefix', hexHasPrefix('0x1234')); // => true
|
||||
* ```
|
||||
*/
|
||||
export function hexHasPrefix (value?: string | null): boolean {
|
||||
export function hexHasPrefix (value?: string | null): value is HexString {
|
||||
return !!(value && isHex(value, -1, true) && value.substr(0, 2) === '0x');
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types';
|
||||
|
||||
import { assert } from '../assert';
|
||||
import { isHex } from '../is/hex';
|
||||
import { hexStripPrefix } from './stripPrefix';
|
||||
@@ -20,7 +22,7 @@ import { hexStripPrefix } from './stripPrefix';
|
||||
* hexToU8a('0x80001f', 32); // Uint8Array([0x00, 0x80, 0x00, 0x1f])
|
||||
* ```
|
||||
*/
|
||||
export function hexToU8a (_value?: string | null, bitLength = -1): Uint8Array {
|
||||
export function hexToU8a (_value?: HexString | string | null, bitLength = -1): Uint8Array {
|
||||
if (!_value) {
|
||||
return new Uint8Array();
|
||||
}
|
||||
|
||||
@@ -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 '../u8a/toU8a';
|
||||
import { isString } from './string';
|
||||
|
||||
@@ -12,7 +14,7 @@ const FORMAT = [9, 10, 13];
|
||||
* @description
|
||||
* Checks to see if the input string or Uint8Array is printable ASCII, 32-127 + formatters
|
||||
*/
|
||||
export function isAscii (value?: number[] | Buffer | Uint8Array | string | null): boolean {
|
||||
export function isAscii (value?: U8aLike | null): boolean {
|
||||
return value
|
||||
? !u8aToU8a(value).some((byte) => (byte >= 127) || (byte < 32 && !FORMAT.includes(byte)))
|
||||
: isString(value);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types';
|
||||
|
||||
const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
|
||||
|
||||
/**
|
||||
@@ -18,7 +20,7 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
|
||||
* isHex('0x1234', 8); // => false
|
||||
* ```
|
||||
*/
|
||||
export function isHex (value: unknown, bitLength = -1, ignoreLength = false): value is string {
|
||||
export function isHex (value: unknown, bitLength = -1, ignoreLength = false): value is HexString {
|
||||
return (typeof value === 'string' && (value === '0x' || HEX_REGEX.test(value)))
|
||||
? bitLength === -1
|
||||
? ((value.length % 2 === 0) || ignoreLength)
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
// Adapted from https://github.com/JulienPalard/is_utf8/blob/master/is_utf8.c
|
||||
|
||||
import type { HexString } from '../types';
|
||||
|
||||
import { u8aToU8a } from '../u8a/toU8a';
|
||||
import { isString } from './string';
|
||||
|
||||
@@ -12,7 +14,7 @@ import { isString } from './string';
|
||||
* @description
|
||||
* Checks to see if the input string or Uint8Array is valid Utf8
|
||||
*/
|
||||
export function isUtf8 (value?: number[] | Buffer | Uint8Array | string | null): boolean {
|
||||
export function isUtf8 (value?: HexString | number[] | Buffer | Uint8Array | string | null): boolean {
|
||||
if (!value) {
|
||||
return isString(value);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types';
|
||||
|
||||
import { hexFixLength } from '../hex/fixLength';
|
||||
import { isNull } from '../is/null';
|
||||
import { isUndefined } from '../is/undefined';
|
||||
@@ -20,7 +22,7 @@ import { isUndefined } from '../is/undefined';
|
||||
* numberToHex(0x1234, 32); // => 0x00001234
|
||||
* ```
|
||||
*/
|
||||
export function numberToHex (value?: number | null, bitLength = -1): string {
|
||||
export function numberToHex (value?: number | null, bitLength = -1): HexString {
|
||||
if (isUndefined(value) || isNull(value) || isNaN(value)) {
|
||||
return '0x';
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2021 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '../types';
|
||||
|
||||
import { u8aToHex } from '../u8a/toHex';
|
||||
import { stringToU8a } from './toU8a';
|
||||
|
||||
@@ -19,7 +21,7 @@ import { stringToU8a } from './toU8a';
|
||||
* ```
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export function stringToHex (value?: String | string): string {
|
||||
export function stringToHex (value?: String | string): HexString {
|
||||
return u8aToHex(
|
||||
stringToU8a(value)
|
||||
);
|
||||
|
||||
@@ -51,3 +51,10 @@ export interface Time {
|
||||
export type Memoized<F> = F & {
|
||||
unmemoize: (...args: unknown[]) => void;
|
||||
}
|
||||
|
||||
export type HexDigit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f';
|
||||
|
||||
// One day when template strings support regex, we can improve this
|
||||
export type HexString = `0x${string}`;
|
||||
|
||||
export type U8aLike = HexString | number[] | Buffer | Uint8Array | string;
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
Reference in New Issue
Block a user