* 7.7.1

* Extract stringify replacer

* formatting

* linting

* isUnSorted{A, B}

* Adjust

* Complexity?

* Cleanups

* Update packages/util-crypto/src/address/derive.ts

* formatBalance

* Revert formatBalance
This commit is contained in:
Jaco
2021-10-31 16:44:17 +02:00
committed by GitHub
parent 481f7f6ee0
commit 8880e8fa8c
32 changed files with 264 additions and 178 deletions
+22 -6
View File
@@ -9,6 +9,19 @@ import { isString } from './string';
const FORMAT = [9, 10, 13];
/** @internal */
function getCharCode0 (s: string): number {
return s.charCodeAt(0);
}
/** @internal */
function isAsciiByte (b: number): boolean {
return (b < 127) && (
(b >= 32) ||
FORMAT.includes(b)
);
}
/**
* @name isAscii
* @summary Tests if the input is printable ASCII
@@ -18,10 +31,13 @@ const FORMAT = [9, 10, 13];
export function isAscii (value?: U8aLike | null): boolean {
const isStringIn = isString(value);
return value
? (isStringIn && !isHex(value)
? value.toString().split('').map((s) => s.charCodeAt(0))
: u8aToU8a(value)
).every((b) => (b < 127) && ((b >= 32) || FORMAT.includes(b)))
: isStringIn;
if (value) {
return (
isStringIn && !isHex(value)
? value.toString().split('').map(getCharCode0)
: u8aToU8a(value)
).every(isAsciiByte);
}
return isStringIn;
}
+6 -1
View File
@@ -17,5 +17,10 @@ interface Compact<T> {
* @summary Tests for SCALE-Compact-like object instance.
*/
export function isCompact <T> (value: unknown): value is Compact<T> {
return isFunction((value as Compact<T>).toBigInt) && isFunction((value as Compact<T>).toBn) && isFunction((value as Compact<T>).toNumber) && isFunction((value as Compact<T>).unwrap);
return (
isFunction((value as Compact<T>).toBigInt) &&
isFunction((value as Compact<T>).toBn) &&
isFunction((value as Compact<T>).toNumber) &&
isFunction((value as Compact<T>).unwrap)
);
}
+4 -1
View File
@@ -24,5 +24,8 @@ interface Observable {
* ```
*/
export function isObservable (value: unknown): value is Observable {
return isObject(value) && isFunction((value as Observable).next);
return (
isObject(value) &&
isFunction((value as Observable).next)
);
}
+54 -27
View File
@@ -48,6 +48,20 @@ export function loggerFormat (value: unknown): unknown {
return formatOther(value);
}
function formatWithLength (maxLength: number): (v: unknown) => unknown {
return (v: unknown): unknown => {
if (maxLength <= 0) {
return v;
}
const r = `${v as string}`;
return r.length < maxLength
? v
: `${r.substr(0, maxLength)} ...`;
};
}
function apply (log: LogType, type: string, values: Logger$Data, maxSize = -1): void {
if (values.length === 1 && isFunction(values[0])) {
const fnResult = values[0]() as unknown;
@@ -60,17 +74,7 @@ function apply (log: LogType, type: string, values: Logger$Data, maxSize = -1):
type,
...values
.map(loggerFormat)
.map((v) => {
if (maxSize <= 0) {
return v;
}
const r = `${v as string}`;
return r.length < maxSize
? v
: `${r.substr(0, maxSize)} ...`;
})
.map(formatWithLength(maxSize))
);
}
@@ -78,27 +82,50 @@ function noop (): void {
// noop
}
function isDebugOn (e: string, type: string): boolean {
return !!e && (
e === '*' ||
type === e ||
(
e.endsWith('*') &&
type.startsWith(e.slice(0, -1))
)
);
}
function isDebugOff (e: string, type: string): boolean {
return !!e && (
e.startsWith('-') &&
(
type === e.slice(1) ||
(
e.endsWith('*') &&
type.startsWith(e.slice(1, -1))
)
)
);
}
function getDebugFlag (env: string[], type: string): boolean {
let flag = false;
for (const e of env) {
if (isDebugOn(e, type)) {
flag = true;
} else if (isDebugOff(e, type)) {
flag = false;
}
}
return flag;
}
function parseEnv (type: string): [boolean, number] {
const env = (typeof process === 'object' ? process : {}).env || {};
const maxSize = parseInt(env.DEBUG_MAX || '-1', 10);
let isDebugOn = false;
(env.DEBUG || '')
.toLowerCase()
.split(',')
.forEach((e) => {
if (!!e && (e === '*' || type === e || (e.endsWith('*') && type.startsWith(e.slice(0, -1))))) {
isDebugOn = true;
}
if (!!e && e.startsWith('-') && (type === e.slice(1) || (e.endsWith('*') && type.startsWith(e.slice(1, -1))))) {
isDebugOn = false;
}
});
return [
isDebugOn,
getDebugFlag((env.DEBUG || '').toLowerCase().split(','), type),
isNaN(maxSize)
? -1
: maxSize
+9 -9
View File
@@ -3,13 +3,13 @@
import { isBigInt } from './is/bigInt';
export function stringify (args: unknown, space?: string | number): string {
return JSON.stringify(
args,
(_, value: unknown) =>
isBigInt(value)
? value.toString()
: value,
space
);
/** @internal */
function replacer (_: unknown, v: unknown): unknown {
return isBigInt(v)
? v.toString()
: v;
}
export function stringify (args: unknown, space?: string | number): string {
return JSON.stringify(args, replacer, space);
}
+5 -1
View File
@@ -1,6 +1,10 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
function isByteZero (v: number): boolean {
return !v;
}
/**
* @name u8aEmpty
* @summary Tests for a `Uint8Array` for emptyness
@@ -8,5 +12,5 @@
* Checks to see if the input `Uint8Array` has zero length or contains all 0 values.
*/
export function u8aEmpty (value: Uint8Array): boolean {
return value.length === 0 || value.every((v) => !v);
return value.length === 0 || value.every(isByteZero);
}
+9 -9
View File
@@ -56,24 +56,24 @@ function getVersionLength (all: { version: string }[]): number {
/** @internal */
function flattenInfos (all: PackageJson[]): string {
const verLength = getVersionLength(all);
const stringify = ({ name, version }: PackageJson) =>
`\t${version.padEnd(verLength)}\t${name}`;
return all
.map(({ name, version }) => `\t${version.padEnd(verLength)}\t${name}`)
.join('\n');
return all.map(stringify).join('\n');
}
/** @internal */
function flattenVersions (entry: VersionPath[]): string {
const all = entry.map((version: VersionPath | string): VersionPath =>
const toPath = (version: VersionPath | string): VersionPath =>
isString(version)
? { version }
: version
);
: version;
const all = entry.map(toPath);
const verLength = getVersionLength(all);
const stringify = ({ path, version }: VersionPath) =>
`\t${version.padEnd(verLength)}\t${(!path || path.length < 5) ? '<unknown>' : path}`;
return all
.map(({ path, version }) => `\t${version.padEnd(verLength)}\t${(!path || path.length < 5) ? '<unknown>' : path}`)
.join('\n');
return all.map(stringify).join('\n');
}
/** @internal */