Add toBigInt to number types (#2517)
* Add toBigInt to number types * Update packages/types/src/codec/UInt.spec.ts * Update packages/types/src/codec/Compact.spec.ts * Update packages/types/src/codec/Compact.ts
This commit is contained in:
@@ -138,6 +138,13 @@ export default abstract class AbstractInt extends BN implements Codec {
|
||||
return u8a.length === (this.#bitLength / 8);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a BigInt representation of the number
|
||||
*/
|
||||
public toBigInt (): BigInt {
|
||||
return BigInt(this.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the BN representation of the number. (Compatibility)
|
||||
*/
|
||||
|
||||
@@ -162,6 +162,14 @@ describe('Compact', (): void => {
|
||||
it('unwraps to the wrapped value', (): void => {
|
||||
expect(new (Compact.with(U32))(registry, 12345).unwrap() instanceof U32).toBe(true);
|
||||
});
|
||||
|
||||
it('has a valid toBn interface', (): void => {
|
||||
expect(new (Compact.with('u128'))(registry, '12345678987654321').toBn().eq(new BN('12345678987654321'))).toBe(true);
|
||||
});
|
||||
|
||||
it('has a valid toBigInt interface', (): void => {
|
||||
expect(new (Compact.with('u128'))(registry, 12345678987654321n).toBigInt() === 12345678987654321n).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('helpers', (): void => {
|
||||
|
||||
@@ -117,6 +117,13 @@ export default class Compact<T extends CompactEncodable> implements ICompact<T>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a BigInt representation of the number
|
||||
*/
|
||||
public toBigInt (): BigInt {
|
||||
return BigInt(this.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the BN representation of the number
|
||||
*/
|
||||
|
||||
@@ -26,11 +26,12 @@ describe('Date', (): void => {
|
||||
});
|
||||
|
||||
describe('encode', (): void => {
|
||||
const testEncode = (to: 'toBn' | 'toISOString' | 'toNumber' | CodecTo, expected: BN | number | string | Uint8Array): void =>
|
||||
const testEncode = (to: 'toBigInt' | 'toBn' | 'toISOString' | 'toNumber' | CodecTo, expected: BigInt | BN | number | string | Uint8Array): void =>
|
||||
it(`can encode ${to}`, (): void => {
|
||||
expect(new CodecDate(registry, 421)[to]()).toEqual(expected);
|
||||
});
|
||||
|
||||
testEncode('toBigInt', 421n);
|
||||
testEncode('toBn', new BN(421));
|
||||
testEncode('toJSON', 421);
|
||||
testEncode('toISOString', '1970-01-01T00:07:01.000Z');
|
||||
|
||||
@@ -81,6 +81,13 @@ export default class CodecDate extends Date implements Codec {
|
||||
return BITLENGTH;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a BigInt representation of the number
|
||||
*/
|
||||
public toBigInt (): BigInt {
|
||||
return BigInt(this.toNumber());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the BN representation of the timestamp
|
||||
*/
|
||||
|
||||
@@ -8,6 +8,12 @@ import Int from './Int';
|
||||
describe('Int', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
it('provides a toBigInt interface', (): void => {
|
||||
expect(
|
||||
new Int(registry, -1234).toBigInt()
|
||||
).toEqual(-1234n);
|
||||
});
|
||||
|
||||
it('provides a toBn interface', (): void => {
|
||||
expect(
|
||||
new Int(registry, -1234).toBn().toNumber()
|
||||
|
||||
@@ -28,6 +28,12 @@ describe('UInt', (): void => {
|
||||
).toEqual('123,456,789,123,456,789,123,456,789');
|
||||
});
|
||||
|
||||
it('provides a toBigInt interface', (): void => {
|
||||
expect(
|
||||
new UInt(registry, 9876543210123456789n).toBigInt()
|
||||
).toEqual(9876543210123456789n);
|
||||
});
|
||||
|
||||
it('provides a toBn interface', (): void => {
|
||||
expect(
|
||||
new UInt(registry, 987).toBn().toNumber()
|
||||
|
||||
@@ -9,6 +9,7 @@ import { FunctionMetadataLatest } from '../interfaces/metadata';
|
||||
import { ArgsDef, Codec } from './codec';
|
||||
|
||||
export interface ICompact<T> extends Codec {
|
||||
toBigInt (): BigInt;
|
||||
toBn (): BN;
|
||||
toNumber (): number;
|
||||
unwrap (): T;
|
||||
|
||||
Reference in New Issue
Block a user