Auto-convert large numbers to hex, smaller as number (#647)
* Auto-convert large numbers to hex, smaller as number * Update tests
This commit is contained in:
@@ -15,7 +15,7 @@ describe('Digest', () => {
|
||||
|
||||
expect(log.type).toBe('Seal');
|
||||
expect(log.value.toJSON()).toEqual([
|
||||
'0x000000000f4e8607',
|
||||
256804359,
|
||||
'0x2c0e0ee8a5d1a073da3f8db002b8eaebcd7b1a7eb25662e09f9ef0ac58d96bf17896256cd5f0bc672f96aab49fe5163ca5cb4c1a0f047dc39e89cd4a9eb2ea05'
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -13,6 +13,6 @@ import Int from './codec/Int';
|
||||
*/
|
||||
export default class I128 extends Int {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 128, true);
|
||||
super(value, 128);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ import Int from './codec/Int';
|
||||
*/
|
||||
export default class I16 extends Int {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 16, false);
|
||||
super(value, 16);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ import Int from './codec/Int';
|
||||
*/
|
||||
export default class I256 extends Int {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 256, true);
|
||||
super(value, 256);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ import Int from './codec/Int';
|
||||
*/
|
||||
export default class I32 extends Int {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 32, false);
|
||||
super(value, 32);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ import Int from './codec/Int';
|
||||
*/
|
||||
export default class I64 extends Int {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 64, true);
|
||||
super(value, 64);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ import Int from './codec/Int';
|
||||
*/
|
||||
export default class I8 extends Int {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 8, false);
|
||||
super(value, 8);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ import UInt from './codec/UInt';
|
||||
*/
|
||||
export default class U128 extends UInt {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 128, true);
|
||||
super(value, 128);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ import UInt from './codec/UInt';
|
||||
*/
|
||||
export default class U16 extends UInt {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 16, false);
|
||||
super(value, 16);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ import UInt from './codec/UInt';
|
||||
*/
|
||||
export default class U256 extends UInt {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 256, true);
|
||||
super(value, 256);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ import UInt from './codec/UInt';
|
||||
*/
|
||||
export default class U32 extends UInt {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 32, false);
|
||||
super(value, 32);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ import UInt from './codec/UInt';
|
||||
*/
|
||||
export default class U64 extends UInt {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 64, true);
|
||||
super(value, 64);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,6 @@ import UInt from './codec/UInt';
|
||||
*/
|
||||
export default class U8 extends UInt {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 8, false);
|
||||
super(value, 8);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,7 +101,8 @@ export default abstract class AbstractInt extends BN implements Codec {
|
||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||
*/
|
||||
toJSON (): any {
|
||||
return this._isHexJson
|
||||
// Maximum allowed integer for JS is 2^53 - 1, set limit at 52
|
||||
return this._isHexJson || (super.bitLength() > 52)
|
||||
? this.toHex()
|
||||
: this.toNumber();
|
||||
}
|
||||
|
||||
@@ -46,9 +46,10 @@ describe('UInt', () => {
|
||||
).toEqual(123);
|
||||
});
|
||||
|
||||
it('converts to JSON depending on flags', () => {
|
||||
expect(new UInt(0x12, 16).toJSON()).toEqual('0x0012');
|
||||
expect(new UInt(0x12, 16, false).toJSON()).toEqual(0x12);
|
||||
it('converts to JSON representation based on flags/size', () => {
|
||||
expect(new UInt('0x12345678', 64, true).toJSON()).toEqual('0x0000000012345678');
|
||||
expect(new UInt('0x1234567890', 64, false).toJSON()).toEqual(0x1234567890);
|
||||
expect(new UInt('0x1234567890abcdef', 64, false).toJSON()).toEqual('0x1234567890abcdef');
|
||||
});
|
||||
|
||||
describe('eq', () => {
|
||||
|
||||
@@ -21,7 +21,7 @@ import AbstractInt, { DEFAULT_UINT_BITS, UIntBitLength } from './AbstractInt';
|
||||
export default class UInt extends AbstractInt {
|
||||
constructor (
|
||||
value: AnyNumber = 0,
|
||||
bitLength: UIntBitLength = DEFAULT_UINT_BITS, isHexJson: boolean = true) {
|
||||
bitLength: UIntBitLength = DEFAULT_UINT_BITS, isHexJson: boolean = false) {
|
||||
super(
|
||||
false,
|
||||
value,
|
||||
|
||||
@@ -208,7 +208,7 @@ describe('createType', () => {
|
||||
index: '0x10'
|
||||
}).toJSON()
|
||||
).toEqual({
|
||||
balance: '0x000000000000000000000000000004d2',
|
||||
balance: 1234,
|
||||
index: 16
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user