Properly toJSON() on StorageFunction (#179)

* Properly toJSON() on StorageFunction

* Add isZero(), cmp & toStringFormat

* Update tests for UInt toString()
This commit is contained in:
Jaco Greeff
2018-10-01 16:33:01 +02:00
committed by GitHub
parent 69ab177468
commit 0c906e54f8
4 changed files with 17 additions and 6 deletions
+2 -2
View File
@@ -15,7 +15,7 @@ describe('Struct', () => {
u32: U32
})
)({ txt: 'foo', u32: 0x123456 }).toString()
).toEqual(`{txt: foo, u32: 0x00123456}`);
).toEqual(`{txt: foo, u32: 1193046}`);
});
it('exposes the types', () => {
@@ -43,6 +43,6 @@ describe('Struct', () => {
test.values().map((v) =>
v.toString()
)
).toEqual(['bazzing', '0x00000045']);
).toEqual(['bazzing', '69']);
});
});
+1 -1
View File
@@ -33,7 +33,7 @@ describe('UInt', () => {
const u = new UInt('0x12', 16);
expect(u.toHex()).toEqual('0x0012');
expect(u.toString()).toEqual('0x0012');
expect(u.toString()).toEqual('18');
});
it('converts to equivalents', () => {
+12 -2
View File
@@ -71,7 +71,7 @@ export default class UInt extends Base<BN> {
}
toJSON (): any {
return this.toString();
return this.toHex();
}
toU8a (isBare?: boolean): Uint8Array {
@@ -79,7 +79,7 @@ export default class UInt extends Base<BN> {
}
toString (): string {
return this.toHex();
return this.raw.toString();
}
toBn (): BN {
@@ -98,12 +98,22 @@ export default class UInt extends Base<BN> {
: this.raw.add(bnToBn(other));
}
cmp (other: UInt | BN | number): number {
return other instanceof UInt
? this.raw.cmp(other.raw)
: this.raw.cmp(bnToBn(other));
}
div (other: UInt | BN | number): BN {
return other instanceof UInt
? this.raw.div(other.raw)
: this.raw.div(bnToBn(other));
}
isZero (): boolean {
return this.raw.isZero();
}
lt (test: UInt | BN | number): boolean {
return test instanceof UInt
? this.raw.lt(test.raw)
@@ -65,7 +65,8 @@ export default function createFunction (
}
storageFn.meta = meta;
storageFn.toJSON = meta.toJSON;
storageFn.toJSON = (): any =>
meta.toJSON();
return storageFn as StorageFunction;
}