3.7 (#738)
* 3.7 * Update new rules * RegExp * formatBalance cleanups * Revert * Formatting * fill * format test fix * cleanups * Cleanups * Expand bip39 tests * Cleanups * Cleanups
This commit is contained in:
@@ -12,6 +12,15 @@ interface Options extends ToBnOptions {
|
||||
bitLength?: number;
|
||||
}
|
||||
|
||||
function setBn (valueBn: BN, byteLength: number, { isLe, isNegative }: Options): Uint8Array {
|
||||
const output = new Uint8Array(byteLength);
|
||||
const bn = isNegative ? valueBn.toTwos(byteLength * 8) : valueBn;
|
||||
|
||||
output.set(bn.toArray(isLe ? 'le' : 'be', byteLength), 0);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name bnToU8a
|
||||
* @summary Creates a Uint8Array object from a BN.
|
||||
@@ -29,7 +38,7 @@ interface Options extends ToBnOptions {
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | BigInt | number | null, options?: Options): Uint8Array;
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | BigInt | number | null, bitLength?: number, isLe?: boolean): Uint8Array;
|
||||
function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | BigInt | number | null, arg1: number | Options = { bitLength: -1, isLe: true, isNegative: false }, arg2?: boolean): Uint8Array {
|
||||
const _options: Options = {
|
||||
const options: Options = {
|
||||
bitLength: -1,
|
||||
isLe: true,
|
||||
isNegative: false,
|
||||
@@ -37,25 +46,15 @@ function bnToU8a <ExtToBn extends ToBn> (value?: ExtToBn | BN | BigInt | number
|
||||
};
|
||||
|
||||
const valueBn = bnToBn(value);
|
||||
const byteLength = _options.bitLength === -1
|
||||
const byteLength = options.bitLength === -1
|
||||
? Math.ceil(valueBn.bitLength() / 8)
|
||||
: Math.ceil((_options.bitLength || 0) / 8);
|
||||
: Math.ceil((options.bitLength || 0) / 8);
|
||||
|
||||
if (!value) {
|
||||
return _options.bitLength === -1
|
||||
return value
|
||||
? setBn(valueBn, byteLength, options)
|
||||
: options.bitLength === -1
|
||||
? new Uint8Array()
|
||||
: new Uint8Array(byteLength);
|
||||
}
|
||||
|
||||
const output = new Uint8Array(byteLength);
|
||||
const bn = _options.isNegative ? valueBn.toTwos(byteLength * 8) : valueBn;
|
||||
|
||||
output.set(
|
||||
bn.toArray(_options.isLe ? 'le' : 'be', byteLength),
|
||||
0
|
||||
);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
export default bnToU8a;
|
||||
|
||||
@@ -20,6 +20,12 @@ describe('formatBalance', (): void => {
|
||||
).toEqual('123.4567 µUnit');
|
||||
});
|
||||
|
||||
it('formats 123,456 (decimals=0)', (): void => {
|
||||
expect(
|
||||
formatBalance(123456, { decimals: 0, withSi: true })
|
||||
).toEqual('123.4560 kUnit');
|
||||
});
|
||||
|
||||
it('formats BigInt numbers', (): void => {
|
||||
expect(
|
||||
formatBalance(123456789000n, { decimals: 15, withSi: true })
|
||||
|
||||
@@ -61,8 +61,8 @@ function _formatBalance <ExtToBn extends ToBn> (input?: number | string | BN | B
|
||||
: options;
|
||||
|
||||
// NOTE We start at midpoint (8) minus 1 - this means that values display as
|
||||
// 123.456 instead of 0.123k (so always 6 relevant). Additionally we us ceil
|
||||
// so there are at most 3 decimal before the decimal seperator
|
||||
// 123.456 instead of 0.123k (so always 6 relevant). Additionally we use ceil
|
||||
// so there are at most 3 decimal before the decimal separator
|
||||
const si = calcSi(text, decimals, forceUnit);
|
||||
const mid = text.length - (decimals + si.power);
|
||||
const prefix = text.substr(0, mid);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// eslint-disable-next-line prefer-regex-literals
|
||||
const NUMBER_REGEX = new RegExp('(\\d+?)(?=(\\d{3})+(?!\\d)|$)', 'g');
|
||||
|
||||
export default function formatDecimal (value: string): string {
|
||||
|
||||
@@ -8,37 +8,38 @@ import BN from 'bn.js';
|
||||
import isToBn from '../is/toBn';
|
||||
|
||||
function getValue <ExtToBn extends ToBn> (value?: BN | ExtToBn | Date | number | null): number {
|
||||
if (value) {
|
||||
if (isToBn(value)) {
|
||||
return getValue(value.toBn());
|
||||
} else if (value instanceof Date) {
|
||||
return getValue(value.getTime());
|
||||
} else if (value instanceof BN) {
|
||||
return getValue(value.toNumber());
|
||||
}
|
||||
if (isToBn(value)) {
|
||||
return getValue(value.toBn());
|
||||
} else if (value instanceof Date) {
|
||||
return getValue(value.getTime());
|
||||
} else if (value instanceof BN) {
|
||||
return getValue(value.toNumber());
|
||||
}
|
||||
|
||||
return (value as number) || 0;
|
||||
return value || 0;
|
||||
}
|
||||
|
||||
function formatValue (elapsed: number): string {
|
||||
if (elapsed < 15) {
|
||||
return `${elapsed.toFixed(1)}s`;
|
||||
} else if (elapsed < 60) {
|
||||
return `${elapsed | 0}s`;
|
||||
} else if (elapsed < 3600) {
|
||||
return `${elapsed / 60 | 0}m`;
|
||||
}
|
||||
|
||||
return `${elapsed / 3600 | 0}h`;
|
||||
}
|
||||
|
||||
export default function formatElapsed <ExtToBn extends ToBn> (now?: Date | null, value?: BN | ExtToBn | Date | number | null): string {
|
||||
const tsNow = (now && now.getTime()) || 0;
|
||||
const tsValue = getValue(value);
|
||||
let display = '0.0s';
|
||||
|
||||
if (tsNow && tsValue) {
|
||||
const elapsed = Math.max(Math.abs(tsNow - tsValue), 0) / 1000;
|
||||
|
||||
if (elapsed < 15) {
|
||||
display = `${elapsed.toFixed(1)}s`;
|
||||
} else if (elapsed < 60) {
|
||||
display = `${elapsed | 0}s`;
|
||||
} else if (elapsed < 3600) {
|
||||
display = `${elapsed / 60 | 0}m`;
|
||||
} else {
|
||||
display = `${elapsed / 3600 | 0}h`;
|
||||
}
|
||||
return formatValue(elapsed);
|
||||
}
|
||||
|
||||
return display;
|
||||
return '0.0s';
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import hexStripPrefix from './stripPrefix';
|
||||
import { ToBnOptions } from '../types';
|
||||
|
||||
function reverse (value: string): string {
|
||||
return (value.match(new RegExp('.{1,2}', 'g')) || [])
|
||||
return (value.match(/.{1,2}/g) || [])
|
||||
.reverse()
|
||||
.join('');
|
||||
}
|
||||
|
||||
@@ -13,11 +13,7 @@ const FORMAT = [9, 10, 13];
|
||||
* Checks to see if the input string or Uint8Array is printable ASCII, 32-127 + formatters
|
||||
*/
|
||||
export default function isAscii (value?: number[] | Buffer | Uint8Array | string | null): boolean {
|
||||
if (!value) {
|
||||
return isString(value);
|
||||
}
|
||||
|
||||
const u8a = u8aToU8a(value);
|
||||
|
||||
return !u8a.some((byte) => (byte >= 127) || (byte < 32 && !FORMAT.includes(byte)));
|
||||
return value
|
||||
? !u8aToU8a(value).some((byte) => (byte >= 127) || (byte < 32 && !FORMAT.includes(byte)))
|
||||
: isString(value);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Copyright 2017-2020 @polkadot/util authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
const re = new RegExp('(Development|Local Testnet)$');
|
||||
const re = /(Development|Local Testnet)$/;
|
||||
|
||||
export default function isTestChain (chain?: string | null): boolean {
|
||||
if (!chain) {
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
|
||||
import { Logger } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
import { logger } from '.';
|
||||
import { format } from './logger';
|
||||
|
||||
@@ -62,15 +64,17 @@ describe('logger', (): void => {
|
||||
);
|
||||
});
|
||||
|
||||
it('logs with mulptiple values', (): void => {
|
||||
l.log('test', 'a', 2);
|
||||
it('logs with multiple values', (): void => {
|
||||
l.log('test', 'a', 2, new BN('12345678900987654321'), Buffer.from([0, 1, 2, 3]));
|
||||
|
||||
expect(spy.log).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
prefixMatch,
|
||||
expect.stringMatching('test'),
|
||||
expect.stringMatching('a'),
|
||||
2
|
||||
2,
|
||||
expect.stringMatching('12345678900987654321'),
|
||||
expect.stringMatching('0x00010203')
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -5,11 +5,12 @@ import { Logger, Logger$Data } from './types';
|
||||
|
||||
import formatDate from './format/formatDate';
|
||||
import isBn from './is/bn';
|
||||
import isBuffer from './is/buffer';
|
||||
import isFunction from './is/function';
|
||||
import isObject from './is/object';
|
||||
import isU8a from './is/u8a';
|
||||
import u8aToHex from './u8a/toHex';
|
||||
import u8aToU8a from './u8a/toU8a';
|
||||
import { isBuffer } from './is';
|
||||
|
||||
type ConsoleType = 'error' | 'log' | 'warn';
|
||||
type LogType = ConsoleType | 'debug';
|
||||
@@ -34,21 +35,11 @@ function formatObject (value: Record<string, unknown>): Record<string, unknown>
|
||||
export function format (value: unknown): unknown {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(format);
|
||||
}
|
||||
|
||||
if (isBn(value)) {
|
||||
} else if (isBn(value)) {
|
||||
return value.toString();
|
||||
}
|
||||
|
||||
if (isBuffer(value)) {
|
||||
return `0x${value.toString('hex')}`;
|
||||
}
|
||||
|
||||
if (isU8a(value)) {
|
||||
return u8aToHex(value);
|
||||
}
|
||||
|
||||
if (value && isObject(value) && value.constructor === Object) {
|
||||
} else if (isU8a(value) || isBuffer(value)) {
|
||||
return u8aToHex(u8aToU8a(value));
|
||||
} else if (value && isObject(value) && value.constructor === Object) {
|
||||
return formatObject(value);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,50 +2,30 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/* eslint-disable no-extend-native */
|
||||
/* eslint-disable @typescript-eslint/unbound-method */
|
||||
|
||||
import assert from '../assert';
|
||||
|
||||
function getOffset (value: number, length: number): number {
|
||||
return value < 0
|
||||
? Math.max(length + value, 0)
|
||||
: Math.min(value, length);
|
||||
}
|
||||
|
||||
if (!Array.prototype.fill) {
|
||||
// eslint-disable-next-line no-extend-native,@typescript-eslint/no-explicit-any
|
||||
Array.prototype.fill = function fill (value: any, start = 0, end?: number): any[] {
|
||||
// Steps 1-2.
|
||||
if (!this) {
|
||||
throw new TypeError('this is null or not defined');
|
||||
}
|
||||
// eslint-disable-next-line no-extend-native
|
||||
Array.prototype.fill = function fill <T> (value: T, start = 0, end?: number): T[] {
|
||||
assert(this, 'this is null or not defined');
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const A = Object(this);
|
||||
const A = Object(this) as T[];
|
||||
const relativeEnd = end ?? A.length;
|
||||
const final = getOffset(relativeEnd, A.length);
|
||||
let k = getOffset(start, A.length);
|
||||
|
||||
// Steps 3-5.
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
const len = A.length >>> 0;
|
||||
|
||||
// Steps 6-7.
|
||||
const relativeStart = start >> 0;
|
||||
|
||||
// Step 8.
|
||||
let k = relativeStart < 0
|
||||
? Math.max(len + relativeStart, 0)
|
||||
: Math.min(relativeStart, len);
|
||||
|
||||
// Steps 9-10.
|
||||
const relativeEnd = end === undefined
|
||||
? len
|
||||
: end >> 0;
|
||||
|
||||
// Step 11.
|
||||
const final = relativeEnd < 0
|
||||
? Math.max(len + relativeEnd, 0)
|
||||
: Math.min(relativeEnd, len);
|
||||
|
||||
// Step 12.
|
||||
while (k < final) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-member-access
|
||||
A[k] = value;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Step 13.
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
return A;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -7,6 +7,22 @@ for (let n = 0; n < 256; ++n) {
|
||||
ALPHABET[n] = n.toString(16).padStart(2, '0');
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function extract (value: Uint8Array): string {
|
||||
const result = new Array(value.length) as string[];
|
||||
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
result[i] = ALPHABET[value[i]];
|
||||
}
|
||||
|
||||
return result.join('');
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function trim (value: Uint8Array, halfLength: number): string {
|
||||
return `${u8aToHex(value.subarray(0, halfLength), -1, false)}…${u8aToHex(value.subarray(value.length - halfLength), -1, false)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name u8aToHex
|
||||
* @summary Creates a hex string from a Uint8Array object.
|
||||
@@ -32,17 +48,8 @@ export default function u8aToHex (value?: Uint8Array | null, bitLength = -1, isP
|
||||
|
||||
const byteLength = Math.ceil(bitLength / 8);
|
||||
|
||||
if (byteLength > 0 && value.length > byteLength) {
|
||||
const halfLength = Math.ceil(byteLength / 2);
|
||||
|
||||
return `${u8aToHex(value.subarray(0, halfLength), -1, isPrefixed)}…${u8aToHex(value.subarray(value.length - halfLength), -1, false)}`;
|
||||
}
|
||||
|
||||
const result = new Array(value.length) as string[];
|
||||
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
result[i] = ALPHABET[value[i]];
|
||||
}
|
||||
|
||||
return prefix + result.join('');
|
||||
return prefix + ((byteLength > 0 && value.length > byteLength)
|
||||
? trim(value, Math.ceil(byteLength / 2))
|
||||
: extract(value)
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user