This commit is contained in:
Jaco
2023-04-29 08:44:25 +03:00
committed by GitHub
parent 6b1ff0036e
commit 770471aef0
30 changed files with 141 additions and 150 deletions
+1 -1
View File
@@ -18,7 +18,7 @@
export function arrayChunk <T> (array: T[], chunkSize: number): T[][] {
const outputSize = Math.ceil(array.length / chunkSize);
// noop for the single-split case
// shortcut for the single-split case
if (outputSize === 1) {
return [array];
}
+1 -1
View File
@@ -19,7 +19,7 @@
* ```
*/
export function arrayFlatten <T> (arrays: readonly T[][]): T[] {
// noop for the empty & single-entry case
// shortcuts for the empty & single-entry case
if (arrays.length === 0) {
return [];
} else if (arrays.length === 1) {
+4 -3
View File
@@ -6,10 +6,11 @@
* @description Splits a single [K, V][] into [K[], V[]]
*/
export function arrayUnzip <K, V> (entries: readonly [K, V][]): [K[], V[]] {
const keys = new Array<K>(entries.length);
const values = new Array<V>(entries.length);
const count = entries.length;
const keys = new Array<K>(count);
const values = new Array<V>(count);
for (let i = 0; i < entries.length; i++) {
for (let i = 0; i < count; i++) {
[keys[i], values[i]] = entries[i];
}
+3 -2
View File
@@ -6,9 +6,10 @@
* @description Combines 2 distinct key/value arrays into a single [K, V] array
*/
export function arrayZip <K, V> (keys: readonly K[], values: readonly V[]): [K, V][] {
const result = new Array<[K, V]>(keys.length);
const count = keys.length;
const result = new Array<[K, V]>(count);
for (let i = 0; i < keys.length; i++) {
for (let i = 0; i < count; i++) {
result[i] = [keys[i], values[i]];
}
+1
View File
@@ -25,6 +25,7 @@ export * from './lazy.js';
export * from './logger.js';
export * from './memoize.js';
export * from './nextTick.js';
export * from './noop.js';
export * from './number/index.js';
export * from './object/index.js';
export * from './promisify.js';
+10 -13
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2023 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Logger, Logger$Data } from './types.js';
import type { Logger } from './types.js';
import { xglobal } from '@polkadot/x-global';
@@ -14,6 +14,7 @@ import { isU8a } from './is/u8a.js';
import { u8aToHex } from './u8a/toHex.js';
import { u8aToU8a } from './u8a/toU8a.js';
import { hasProcess } from './has.js';
import { noop } from './noop.js';
type ConsoleType = 'error' | 'log' | 'warn';
type LogType = ConsoleType | 'debug';
@@ -65,7 +66,7 @@ function formatWithLength (maxLength: number): (v: unknown) => unknown {
};
}
function apply (log: LogType, type: string, values: Logger$Data, maxSize = -1): void {
function apply (log: LogType, type: string, values: unknown[], maxSize = -1): void {
if (values.length === 1 && isFunction(values[0])) {
const fnResult = values[0]() as unknown;
@@ -81,10 +82,6 @@ function apply (log: LogType, type: string, values: Logger$Data, maxSize = -1):
);
}
function noop (): void {
// noop
}
function isDebugOn (e: string, type: string): boolean {
return !!e && (
e === '*' ||
@@ -149,17 +146,17 @@ function parseEnv (type: string): [boolean, number] {
* const l = logger('test');
* ```
*/
export function logger (_type: string): Logger {
const type = `${_type.toUpperCase()}:`.padStart(16);
const [isDebug, maxSize] = parseEnv(_type.toLowerCase());
export function logger (origin: string): Logger {
const type = `${origin.toUpperCase()}:`.padStart(16);
const [isDebug, maxSize] = parseEnv(origin.toLowerCase());
return {
debug: isDebug
? (...values: Logger$Data) => apply('debug', type, values, maxSize)
? (...values: unknown[]) => apply('debug', type, values, maxSize)
: noop,
error: (...values: Logger$Data) => apply('error', type, values),
log: (...values: Logger$Data) => apply('log', type, values),
error: (...values: unknown[]) => apply('error', type, values),
log: (...values: unknown[]) => apply('log', type, values),
noop,
warn: (...values: Logger$Data) => apply('warn', type, values)
warn: (...values: unknown[]) => apply('warn', type, values)
};
}
+16
View File
@@ -0,0 +1,16 @@
// Copyright 2017-2023 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
/**
* A sharable identity function. Returns the input as-is with no transformation applied.
*/
export function identity <T> (value: T): T {
return value;
}
/**
* A sharable noop function. As the name suggests, does nothing
*/
export function noop (): void {
// noop
}
+10 -40
View File
@@ -3,14 +3,12 @@
import type { BN } from './bn/bn.js';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface Constructor<T = any> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
new(...value: any[]): T;
export interface Constructor<T extends object = object> {
prototype: T;
new (...args: never[]): T;
hasOwnProperty (prop: string): boolean;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
isPrototypeOf (other: any): boolean;
isPrototypeOf (other: unknown): boolean;
}
export interface ToBigInt {
@@ -27,15 +25,12 @@ export interface SiDef {
value: string;
}
type Logger$Data$Fn = () => unknown[];
export type Logger$Data = (unknown | Logger$Data$Fn)[];
export interface Logger {
debug: (...values: Logger$Data) => void;
error: (...values: Logger$Data) => void;
log: (...values: Logger$Data) => void;
noop: (...values: Logger$Data) => void;
warn: (...values: Logger$Data) => void;
debug: (...values: unknown[]) => void;
error: (...values: unknown[]) => void;
log: (...values: unknown[]) => void;
noop: (...values: unknown[]) => void;
warn: (...values: unknown[]) => void;
}
export interface ToBnOptions {
@@ -56,11 +51,6 @@ export interface NumberOptions extends ToBnOptions {
bitLength?: number;
}
export type BnList = {
0: BN;
1: BN;
} & BN[];
export interface Time {
days: number;
hours: number;
@@ -90,32 +80,12 @@ export interface BufferObj extends Uint8Array {
// We define a scappy low-level interface to mock Buffer
// (this removes the need for the node typings in built bundles)
export interface BufferObjConstructor extends Function {
export interface BufferObjConstructor extends Constructor<BufferObj> {
isBuffer: (value: unknown) => boolean;
}
export type U8aLike = HexString | number[] | Uint8Array | AnyString;
export interface IBigIntConstructor {
new (value: string | number | bigint | boolean): bigint;
/**
* Interprets the low bits of a BigInt as a 2's-complement signed integer.
* All higher bits are discarded.
* @param bits The number of low bits to use
* @param int The BigInt whose bits to extract
*/
asIntN (bits: number, int: bigint): bigint;
/**
* Interprets the low bits of a BigInt as an unsigned integer.
* All higher bits are discarded.
* @param bits The number of low bits to use
* @param int The BigInt whose bits to extract
*/
asUintN (bits: number, int: bigint): bigint;
}
export interface Observable {
next: (...params: unknown[]) => unknown;
}
+3 -2
View File
@@ -23,10 +23,11 @@ import { u8aToU8a } from './toU8a.js';
* ```
*/
export function u8aConcat (...list: readonly U8aLike[]): Uint8Array {
const u8as = new Array<Uint8Array>(list.length);
const count = list.length;
const u8as = new Array<Uint8Array>(count);
let length = 0;
for (let i = 0; i < list.length; i++) {
for (let i = 0; i < count; i++) {
u8as[i] = u8aToU8a(list[i]);
length += u8as[i].length;
}
+3 -2
View File
@@ -23,9 +23,10 @@ import { u8aToU8a } from './toU8a.js';
* ```
*/
export function u8aConcat (...list: readonly U8aLike[]): Uint8Array {
const u8as = new Array<Uint8Array>(list.length);
const count = list.length;
const u8as = new Array<Uint8Array>(count);
for (let i = 0; i < list.length; i++) {
for (let i = 0; i < count; i++) {
u8as[i] = u8aToU8a(list[i]);
}
+1 -1
View File
@@ -45,7 +45,7 @@ function getEntry (name: string): VersionPath[] {
function formatDisplay <T extends { version: string }> (all: T[], fmt: (version: string, data: T) => string[]): string {
let max = 0;
for (let i = 0; i < all.length; i++) {
for (let i = 0, count = all.length; i < count; i++) {
max = Math.max(max, all[i].version.length);
}