Small consistency cleanups (#1178)

* Small consistency cleanups

* Adjust

* Adjust
This commit is contained in:
Jaco
2021-11-13 17:39:52 +02:00
committed by GitHub
parent 9043f335d4
commit 5fdd967666
14 changed files with 48 additions and 31 deletions
+3 -3
View File
@@ -5,9 +5,9 @@ import { assert } from '../assert';
import { BN, BN_ONE, BN_TWO, bnToBn, bnToU8a } from '../bn';
import { u8aConcat } from '../u8a';
const MAX_U8 = new BN(2).pow(new BN(8 - 2)).subn(1);
const MAX_U16 = new BN(2).pow(new BN(16 - 2)).subn(1);
const MAX_U32 = new BN(2).pow(new BN(32 - 2)).subn(1);
const MAX_U8 = BN_TWO.pow(new BN(8 - 2)).isub(BN_ONE);
const MAX_U16 = BN_TWO.pow(new BN(16 - 2)).isub(BN_ONE);
const MAX_U32 = BN_TWO.pow(new BN(32 - 2)).isub(BN_ONE);
/**
* @name compactToU8a
+3 -2
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
/**
* @name isString
* @summary Tests for a string.
@@ -15,7 +17,6 @@
* console.log('isString', isString('test')); // => true
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function isString (value: unknown): value is string | String {
export function isString (value: unknown): value is AnyString {
return typeof value === 'string' || value instanceof String;
}
+2 -4
View File
@@ -19,14 +19,12 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function promisify <R = any> (self: unknown, fn: (...params: any) => any, ...params: any[]): Promise<R> {
return new Promise((resolve, reject): void => {
const handler = (error: Error | null, result?: R): void => {
fn.apply(self, params.concat((error: Error | null, result?: R): void => {
if (error) {
reject(error);
} else {
resolve(result as R);
}
};
fn.apply(self, params.concat(handler));
}));
});
}
+3 -2
View File
@@ -1,13 +1,14 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
import camelcase from 'camelcase';
/**
* @name stringCamelCase
* @summary Convert a dash/dot/underscore/space separated string/String to camelCase
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringCamelCase (value: string | String): string {
export function stringCamelCase (value: AnyString): string {
return camelcase(value.toString());
}
+3 -2
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
/**
* @name stringLowerFirst
* @summary Lowercase the first letter of a string
@@ -15,8 +17,7 @@
* stringLowerFirst('ABC'); // => 'aBC'
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringLowerFirst (value?: string | String | null): string {
export function stringLowerFirst (value?: AnyString | null): string {
return value
? value.charAt(0).toLowerCase() + value.slice(1)
: '';
+6 -3
View File
@@ -1,13 +1,16 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
import camelcase from 'camelcase';
const OPTS = { pascalCase: true };
/**
* @name stringPascalCase
* @summary Convert a dash/dot/underscore/space separated string/String to PascalCase
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringPascalCase (value: string | String): string {
return camelcase(value.toString(), { pascalCase: true });
export function stringPascalCase (value: AnyString): string {
return camelcase(value.toString(), OPTS);
}
+3 -2
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
/**
* @name stringShorten
* @summary Returns a string with maximum length
@@ -15,8 +17,7 @@
* stringShorten('1234567890', 2); // => 12..90
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringShorten (value: String | string, prefixLength = 6): string {
export function stringShorten (value: AnyString, prefixLength = 6): string {
if (value.length <= 2 + 2 * prefixLength) {
return value.toString();
}
+2 -3
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '../types';
import type { AnyString, HexString } from '../types';
import { u8aToHex } from '../u8a/toHex';
import { stringToU8a } from './toU8a';
@@ -20,8 +20,7 @@ import { stringToU8a } from './toU8a';
* stringToU8a('hello'); // 0x68656c6c6f
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringToHex (value?: String | string): HexString {
export function stringToHex (value?: AnyString): HexString {
return u8aToHex(
stringToU8a(value)
);
+3 -1
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
import { TextEncoder } from '@polkadot/x-textencoder';
const encoder = new TextEncoder();
@@ -20,7 +22,7 @@ const encoder = new TextEncoder();
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringToU8a (value?: String | string): Uint8Array {
export function stringToU8a (value?: AnyString): Uint8Array {
return value
? encoder.encode(value.toString())
: new Uint8Array();
+3 -1
View File
@@ -1,6 +1,8 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyString } from '../types';
/**
* @name stringUpperFirst
* @summary Lowercase the first letter of a string
@@ -16,7 +18,7 @@
* ```
*/
// eslint-disable-next-line @typescript-eslint/ban-types
export function stringUpperFirst (value?: String | string | null): string {
export function stringUpperFirst (value?: AnyString | null): string {
return value
? value.charAt(0).toUpperCase() + value.slice(1)
: '';
+7 -2
View File
@@ -10,6 +10,11 @@ function replacer (_: unknown, v: unknown): unknown {
: v;
}
export function stringify (args: unknown, space?: string | number): string {
return JSON.stringify(args, replacer, space);
/**
* @name stringify
* @summary Performs a JSON.stringify, with BigInt handling
* @description A wrapper for JSON.stringify that handles BigInt values transparently, converting them to string. No differences from the native JSON.stringify function otherwise.
*/
export function stringify (value: unknown, space?: string | number): string {
return JSON.stringify(value, replacer, space);
}
+4 -1
View File
@@ -52,9 +52,12 @@ export type Memoized<F> = F & {
unmemoize: (...args: unknown[]) => void;
}
// eslint-disable-next-line @typescript-eslint/ban-types
export type AnyString = string | String;
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;
export type U8aLike = HexString | number[] | Buffer | Uint8Array | AnyString;
+1
View File
@@ -92,6 +92,7 @@ function getPath (pathOrFn?: FnString | string | false): string {
/**
* @name detectPackage
* @summary Checks that a specific package is only imported once
* @description A `@polkadot/*` version detection utility, checking for one occurence of a package in addition to checking for ddependency versions.
*/
export function detectPackage ({ name, version }: PackageJson, pathOrFn?: FnString | string | false, deps: PackageJson[] = []): void {
assert(name.startsWith('@polkadot'), () => `Invalid package descriptor ${name}`);
+5 -5
View File
@@ -5,14 +5,14 @@
// The ISC License
// Copyright (c) Isaac Z. Schlueter
export default function inherits (ctor: Record<string, unknown>, superCtor: Record<string, unknown>): void {
if (superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype as null, {
export default function inherits (child: Record<string, unknown>, parent: Record<string, unknown>): void {
if (parent) {
child.super_ = parent;
child.prototype = Object.create(parent.prototype as object, {
constructor: {
configurable: true,
enumerable: false,
value: ctor,
value: child,
writable: true
}
});