Asserts for number formats (#4439)
* Asserts for number formats * Test for Compact (used base format asserts) * Lint fixes
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyNumber, CodecRegistry, INumber, IU8a, UIntBitLength } from '../types';
|
||||
|
||||
import { assert, BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, BN_ZERO, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isHex, isU8a, u8aToBn } from '@polkadot/util';
|
||||
import { assert, BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, BN_ZERO, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isHex, isNumber, isString, isU8a, u8aToBn } from '@polkadot/util';
|
||||
|
||||
export const DEFAULT_UINT_BITS = 64;
|
||||
|
||||
@@ -32,6 +32,10 @@ function decodeAbstractInt (value: AnyNumber, bitLength: UIntBitLength, isNegati
|
||||
return value.toString();
|
||||
} else if (isHex(value, -1, true)) {
|
||||
return hexToBn(value, { isLe: false, isNegative }).toString();
|
||||
} else if (isNumber(value)) {
|
||||
assert(value <= Number.MAX_SAFE_INTEGER && value >= Number.MIN_SAFE_INTEGER && Math.floor(value) === value, 'Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1');
|
||||
} else if (isString(value)) {
|
||||
assert(!(value.includes('.') || value.includes(',') || value.includes('e')), 'String should not contain decimal points or scientific notation');
|
||||
}
|
||||
|
||||
return bnToBn(value).toString();
|
||||
|
||||
@@ -9,6 +9,11 @@ describe('Compact', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
describe('constructor', (): void => {
|
||||
it('fails on > MAX_SAFE_INTEGER', (): void => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
|
||||
expect(() => new Compact(registry, 'u128', 9007199254740999)).toThrow(/integer <= Number.MAX_SAFE_INTEGER/);
|
||||
});
|
||||
|
||||
it('has support for BigInt', (): void => {
|
||||
expect(
|
||||
new Compact(registry, 'u128', 123456789000123456789n).toHuman()
|
||||
|
||||
@@ -8,6 +8,19 @@ import { BN, BN_TWO, isBn } from '@polkadot/util';
|
||||
describe('UInt', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
it('fails on > MAX_SAFE_INTEGER and float', (): void => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
|
||||
expect(() => new UInt(registry, 9007199254740999)).toThrow(/integer <= Number.MAX_SAFE_INTEGER/);
|
||||
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
|
||||
expect(() => new UInt(registry, -9007199254740999)).toThrow(/integer <= Number.MAX_SAFE_INTEGER/);
|
||||
expect(() => new UInt(registry, 9.123)).toThrow(/integer <= Number.MAX_SAFE_INTEGER/);
|
||||
});
|
||||
|
||||
it('fails on strings with decimal points & scientific notation', (): void => {
|
||||
expect(() => new UInt(registry, '123.4')).toThrow(/not contain decimal points/);
|
||||
expect(() => new UInt(registry, '9e10')).toThrow(/not contain decimal points/);
|
||||
});
|
||||
|
||||
it('decodes an empty Uint8array correctly', (): void => {
|
||||
expect(
|
||||
new UInt(registry, new Uint8Array()).toNumber()
|
||||
|
||||
Reference in New Issue
Block a user