Adjust asserts (#1544)

This commit is contained in:
Jaco
2022-06-18 13:31:25 +03:00
committed by GitHub
parent 096c772d7d
commit 4edb0f00b6
7 changed files with 21 additions and 24 deletions
+3 -3
View File
@@ -1,8 +1,6 @@
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { assert } from '../assert';
/**
* @name arrayRange
* @summary Returns a range of numbers ith the size and the specified offset
@@ -19,7 +17,9 @@ import { assert } from '../assert';
* ```
*/
export function arrayRange (size: number, startAt = 0): number[] {
assert(size > 0, 'Expected non-zero, positive number as a range size');
if (size <= 0) {
throw new Error('Expected non-zero, positive number as a range size');
}
const result = new Array<number>(size);
+3 -3
View File
@@ -1,12 +1,12 @@
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { assert } from '../assert';
/** @internal */
export function createCmp <T> (cmp: (a: T, b: T) => boolean): (...items: T[]) => T {
return (...items: T[]): T => {
assert(items.length >= 1, 'Must provide one or more arguments');
if (items.length === 0) {
throw new Error('Must provide one or more arguments');
}
let result = items[0];
+3 -2
View File
@@ -6,7 +6,6 @@ import type { ToBigInt, ToBn } from '../types';
import { BigInt } from '@polkadot/x-bigint';
import { assert } from '../assert';
import { _0n, _1n, _2pow53n } from './consts';
import { nToBigInt } from './toBigInt';
@@ -20,7 +19,9 @@ export const SQRT_MAX_SAFE_INTEGER = BigInt(94906265);
export function nSqrt <ExtToBn extends ToBn | ToBigInt> (value: ExtToBn | BN | bigint | string | number | null): bigint {
const n = nToBigInt(value);
assert(n >= _0n, 'square root of negative numbers is not supported');
if (n < _0n) {
throw new Error('square root of negative numbers is not supported');
}
// https://stackoverflow.com/questions/53683995/javascript-big-integer-square-root/
// shortcut <= 2^53 - 1 to use the JS utils
+4 -3
View File
@@ -3,9 +3,8 @@
import type { ToBn } from '../types';
import { assert } from '../assert';
import { BN } from './bn';
import { BN_MAX_INTEGER, BN_ONE, BN_ZERO } from './consts';
import { BN_MAX_INTEGER, BN_ONE } from './consts';
import { bnToBn } from './toBn';
/** @internal */
@@ -27,7 +26,9 @@ export const SQRT_MAX_SAFE_INTEGER = new BN(94906265);
export function bnSqrt <ExtToBn extends ToBn> (value: ExtToBn | BN | bigint | string | number | null): BN {
const n = bnToBn(value);
assert(n.gte(BN_ZERO), 'square root of negative numbers is not supported');
if (n.isNeg()) {
throw new Error('square root of negative numbers is not supported');
}
// https://stackoverflow.com/questions/53683995/javascript-big-integer-square-root/
// shortcut <= 2^53 - 1 to use the JS utils
+3 -2
View File
@@ -1,7 +1,6 @@
// Copyright 2017-2022 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { assert } from '../assert';
import { BN, BN_ONE, BN_TWO, bnToBn, bnToU8a } from '../bn';
import { u8aConcatStrict } from '../u8a';
@@ -42,7 +41,9 @@ export function compactToU8a (value: BN | bigint | number): Uint8Array {
length--;
}
assert(length >= 4, 'Invalid length, previous checks match anything less than 2^30');
if (length < 4) {
throw new Error('Invalid length, previous checks match anything less than 2^30');
}
return u8aConcatStrict([
// subtract 4 as minimum (also catered for in decoding)
+2 -9
View File
@@ -3,7 +3,6 @@
import type { ToBnOptions } from '../types';
import { assertUnreachable } from '../assert';
import { BN } from '../bn/bn';
import { isBoolean } from '../is/boolean';
@@ -73,12 +72,9 @@ function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = {}): BN {
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) + ((value[4] ^ 0xff) * 0x1_00_00_00_00);
break;
case 6:
default: // 6
result = ((value[0] + (value[1] << 8) + (value[2] << 16) + (value[3] * 0x1_00_00_00)) ^ 0xffff_ffff) + (((value[4] + (value[5] << 8)) ^ 0x0000_ffff) * 0x1_00_00_00_00);
break;
default:
assertUnreachable(count as never);
}
} else {
for (let i = 0; i < count; i++) {
@@ -116,11 +112,8 @@ function u8aToBn (value: Uint8Array, options: ToBnOptions | boolean = {}): BN {
case 5:
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8)) * 0x1_00_00_00));
case 6:
default: // 6
return new BN(value[0] + (value[1] << 8) + (value[2] << 16) + ((value[3] + (value[4] << 8) + (value[5] << 16)) * 0x1_00_00_00));
default:
assertUnreachable(count as never);
}
} else {
let result = 0;
+3 -2
View File
@@ -4,7 +4,6 @@
import { xglobal } from '@polkadot/x-global';
import { isFunction } from './is/function';
import { assert } from './assert';
type This = typeof globalThis;
@@ -112,7 +111,9 @@ function warn <T extends { version: string }> (pre: string, all: T[], fmt: (vers
* @description A `@polkadot/*` version detection utility, checking for one occurence of a package in addition to checking for ddependency versions.
*/
export function detectPackage ({ name, path, type, version }: PackageJson, pathOrFn?: FnString | string | false | null, deps: PackageJson[] = []): void {
assert(name.startsWith('@polkadot'), () => `Invalid package descriptor ${name}`);
if (!name.startsWith('@polkadot')) {
throw new Error(`Invalid package descriptor ${name}`);
}
const entry = getEntry(name);