Use BigInt only via xglobal (#1272)

* Use BigInt only via xglobal

* Adjust

* yarn
This commit is contained in:
Jaco
2021-12-01 20:37:11 +02:00
committed by GitHub
parent ee99ffdbb4
commit 776a653f42
14 changed files with 49 additions and 27 deletions
+6 -2
View File
@@ -1,13 +1,17 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { xglobal } from '@polkadot/x-global';
import { hasBigInt } from '../has';
/**
* @name _n
* @description Creates a new instance of BigInt in environments that do support it
*/
export function _n (n: string | number | bigint | boolean): bigint {
return typeof BigInt === 'function' && typeof BigInt.asIntN === 'function'
? BigInt(n)
return hasBigInt
? xglobal.BigInt(n)
: Number.NaN as unknown as bigint;
}
+5 -4
View File
@@ -4,6 +4,7 @@
import type { BN } from '../bn/bn';
import type { HexString, ToBigInt, ToBn } from '../types';
import { _n } from '../bi/consts';
import { hexToBigInt } from '../hex/toBigInt';
import { isBn } from '../is/bn';
import { isHex } from '../is/hex';
@@ -18,14 +19,14 @@ export function nToBigInt <ExtToBn extends ToBigInt | ToBn> (value?: HexString |
return typeof value === 'bigint'
? value
: !value
? BigInt(0)
? _n(0)
: isHex(value)
? hexToBigInt(value.toString())
: isBn(value)
? BigInt(value.toString())
? _n(value.toString())
: isToBigInt(value)
? value.toBigInt()
: isToBn(value)
? BigInt(value.toBn().toString())
: BigInt(value);
? _n(value.toBn().toString())
: _n(value);
}
+3 -1
View File
@@ -1,7 +1,9 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
export const hasBigInt = typeof BigInt === 'function' && typeof BigInt.asIntN === 'function';
import { xglobal } from '@polkadot/x-global';
export const hasBigInt = typeof xglobal.BigInt === 'function' && typeof xglobal.BigInt.asIntN === 'function';
export const hasBuffer = typeof Buffer !== 'undefined';
export const hasDirname = typeof __dirname !== 'undefined';
export const hasProcess = typeof process === 'object';
+2 -1
View File
@@ -3,6 +3,7 @@
import type { ToBnOptions } from '../types';
import { _n } from '../bi/consts';
import { objectSpread } from '../object/spread';
import { u8aToBigInt } from '../u8a/toBigInt';
import { hexToU8a } from './toU8a';
@@ -13,7 +14,7 @@ import { hexToU8a } from './toU8a';
*/
export function hexToBigInt (value?: string | null, options: ToBnOptions = {}): bigint {
if (!value || value === '0x') {
return BigInt(0);
return _n(0);
}
return u8aToBigInt(
+4 -4
View File
@@ -31,14 +31,14 @@ function toBigInt (input: Uint8Array): bigint {
const dvI = new DataView(input.buffer, input.byteOffset);
const mod = input.length % 2;
const length = input.length - mod;
let result = BigInt(0);
let result = _n(0);
for (let i = 0; i < length; i += 2) {
result = (result * U16_MAX) + BigInt(dvI.getUint16(i));
result = (result * U16_MAX) + _n(dvI.getUint16(i));
}
if (mod) {
result = (result * U8_MAX) + BigInt(dvI.getUint8(length));
result = (result * U8_MAX) + _n(dvI.getUint8(length));
}
return result;
@@ -50,7 +50,7 @@ function toBigInt (input: Uint8Array): bigint {
*/
export function u8aToBigInt (value: Uint8Array, options: ToBnOptions = {}): bigint {
if (!value || !value.length) {
return BigInt(0);
return _n(0);
}
const { isLe, isNegative }: ToBnOptions = objectSpread({ isLe: true, isNegative: false }, options);