Additional bigint utilities (#1194)

* Additional bigint utilities

* Adjust tests

* biTo{Hex, U8a}

* Adjust

* Ordering

* biSqrt

* Fix toU8a

* Extensible BigInt

* Class TS issues....

* Move type def to types

* Update packages/util/src/bi/sqrt.spec.ts

* max/min

* min/max tests
This commit is contained in:
Jaco
2021-11-16 20:27:19 +02:00
committed by GitHub
parent 87afdb1d86
commit fa5b5bca7e
24 changed files with 703 additions and 18 deletions
+14 -13
View File
@@ -1,11 +1,12 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString, ToBn } from '../types';
import type { HexString, ToBigInt, ToBn } from '../types';
import { hexToBn } from '../hex/toBn';
import { isBigInt } from '../is/bigInt';
import { isHex } from '../is/hex';
import { isToBigInt } from '../is/toBigInt';
import { isToBn } from '../is/toBn';
import { BN } from './bn';
@@ -25,18 +26,18 @@ import { BN } from './bn';
* bnToBn(new BN(0x1234)); // => BN(0x1234)
* ```
*/
export function bnToBn <ExtToBn extends ToBn> (value?: HexString | ExtToBn | BN | bigint | string | number | null): BN {
if (!value) {
return new BN(0);
} else if (isHex(value)) {
return hexToBn(value.toString());
} else if (isBigInt(value)) {
return new BN(value.toString());
}
export function bnToBn <ExtToBn extends ToBigInt | ToBn> (value?: HexString | ExtToBn | BN | bigint | string | number | null): BN {
return BN.isBN(value)
? value
: isToBn(value)
? value.toBn()
: new BN(value);
: !value
? new BN(0)
: isHex(value)
? hexToBn(value.toString())
: isBigInt(value)
? new BN(value.toString())
: isToBn(value)
? value.toBn()
: isToBigInt(value)
? new BN(value.toBigInt().toString())
: new BN(value);
}