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:
Jaco Greeff
2019-01-29 17:09:54 +01:00
committed by GitHub
parent 2edd69c935
commit 383573658b
18 changed files with 23 additions and 21 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ describe('Digest', () => {
expect(log.type).toBe('Seal');
expect(log.value.toJSON()).toEqual([
'0x000000000f4e8607',
256804359,
'0x2c0e0ee8a5d1a073da3f8db002b8eaebcd7b1a7eb25662e09f9ef0ac58d96bf17896256cd5f0bc672f96aab49fe5163ca5cb4c1a0f047dc39e89cd4a9eb2ea05'
]);
});
+1 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
}
}
+1 -1
View File
@@ -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);
}
}
+2 -1
View File
@@ -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();
}
+4 -3
View File
@@ -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', () => {
+1 -1
View File
@@ -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,
+1 -1
View File
@@ -208,7 +208,7 @@ describe('createType', () => {
index: '0x10'
}).toJSON()
).toEqual({
balance: '0x000000000000000000000000000004d2',
balance: 1234,
index: 16
});
});