Use static with for derived H*, U* & I* primitives (#1889)
* Usestatic with for derived H*, U* & I* primitives * Add with override tests * Fix linting
This commit is contained in:
@@ -24,7 +24,7 @@ describe('Compact', (): void => {
|
||||
|
||||
it('encodes max u8 values', (): void => {
|
||||
expect(
|
||||
Compact.encodeU8a(new (UInt.with())(registry, 63))
|
||||
Compact.encodeU8a(new (UInt.with(32))(registry, 63))
|
||||
).toEqual(
|
||||
new Uint8Array([0b11111100])
|
||||
);
|
||||
|
||||
@@ -51,4 +51,18 @@ describe('Int', (): void => {
|
||||
expect(new Int(registry, 0x12, 16).toJSON()).toEqual('0x0012');
|
||||
expect(new Int(registry, 0x12, 16, false).toJSON()).toEqual(0x12);
|
||||
});
|
||||
|
||||
describe('static with', (): void => {
|
||||
it('allows default toRawType', (): void => {
|
||||
expect(
|
||||
new (Int.with(64))(registry).toRawType()
|
||||
).toEqual('i64');
|
||||
});
|
||||
|
||||
it('allows toRawType override', (): void => {
|
||||
expect(
|
||||
new (Int.with(64, 'SomethingElse'))(registry).toRawType()
|
||||
).toEqual('SomethingElse');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, Registry } from '../types';
|
||||
import { AnyNumber, Constructor, Registry } from '../types';
|
||||
|
||||
import { bnToHex, bnToU8a } from '@polkadot/util';
|
||||
|
||||
@@ -23,6 +23,18 @@ export default class Int extends AbstractInt {
|
||||
super(registry, true, value, bitLength, isHexJson);
|
||||
}
|
||||
|
||||
public static with (bitLength: UIntBitLength, typeName?: string): Constructor<Int> {
|
||||
return class extends Int {
|
||||
constructor (registry: Registry, value?: any) {
|
||||
super(registry, value, bitLength);
|
||||
}
|
||||
|
||||
public toRawType (): string {
|
||||
return typeName || super.toRawType();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
|
||||
@@ -63,4 +63,18 @@ describe('U8aFixed', (): void => {
|
||||
expect(u8a.toRawType()).toEqual('[u8;4]');
|
||||
});
|
||||
});
|
||||
|
||||
describe('static with', (): void => {
|
||||
it('allows default toRawType', (): void => {
|
||||
expect(
|
||||
new (U8aFixed.with(64))(registry).toRawType()
|
||||
).toEqual('[u8;8]');
|
||||
});
|
||||
|
||||
it('allows toRawType override', (): void => {
|
||||
expect(
|
||||
new (U8aFixed.with(64, 'SomethingElse'))(registry).toRawType()
|
||||
).toEqual('SomethingElse');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -46,11 +46,15 @@ export default class U8aFixed extends Raw {
|
||||
return u8a;
|
||||
}
|
||||
|
||||
public static with (bitLength: BitLength): Constructor<U8aFixed> {
|
||||
public static with (bitLength: BitLength, typeName?: string): Constructor<U8aFixed> {
|
||||
return class extends U8aFixed {
|
||||
constructor (registry: Registry, value?: any) {
|
||||
super(registry, value, bitLength);
|
||||
}
|
||||
|
||||
public toRawType (): string {
|
||||
return typeName || super.toRawType();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -83,4 +83,18 @@ describe('UInt', (): void => {
|
||||
expect(new UInt(registry, '0xffffffff', 32).isMax()).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('static with', (): void => {
|
||||
it('allows default toRawType', (): void => {
|
||||
expect(
|
||||
new (UInt.with(64))(registry).toRawType()
|
||||
).toEqual('u64');
|
||||
});
|
||||
|
||||
it('allows toRawType override', (): void => {
|
||||
expect(
|
||||
new (UInt.with(64, 'SomethingElse'))(registry).toRawType()
|
||||
).toEqual('SomethingElse');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -24,11 +24,15 @@ export default class UInt extends AbstractInt {
|
||||
super(registry, false, value, bitLength, isHexJson);
|
||||
}
|
||||
|
||||
public static with (bitLength?: UIntBitLength): Constructor<UInt> {
|
||||
public static with (bitLength: UIntBitLength, typeName?: string): Constructor<UInt> {
|
||||
return class extends UInt {
|
||||
constructor (registry: Registry, value?: any) {
|
||||
super(registry, value, bitLength);
|
||||
}
|
||||
|
||||
public toRawType (): string {
|
||||
return typeName || super.toRawType();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyU8a, Registry } from '../types';
|
||||
|
||||
import U8aFixed from '../codec/U8aFixed';
|
||||
|
||||
/**
|
||||
@@ -12,15 +10,4 @@ import U8aFixed from '../codec/U8aFixed';
|
||||
* Hash containing 160 bits (20 bytes), typically used in blocks, extrinsics and
|
||||
* as a sane default for fixed-length hash representations.
|
||||
*/
|
||||
export default class H160 extends U8aFixed {
|
||||
constructor (registry: Registry, value?: AnyU8a) {
|
||||
super(registry, value, 160);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public toRawType (): string {
|
||||
return 'H160';
|
||||
}
|
||||
}
|
||||
export default class H160 extends U8aFixed.with(160, 'H160') {}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyU8a, IHash, Registry } from '../types';
|
||||
import { IHash } from '../types';
|
||||
|
||||
import U8aFixed from '../codec/U8aFixed';
|
||||
|
||||
@@ -12,15 +12,4 @@ import U8aFixed from '../codec/U8aFixed';
|
||||
* Hash containing 256 bits (32 bytes), typically used in blocks, extrinsics and
|
||||
* as a sane default for fixed-length hash representations.
|
||||
*/
|
||||
export default class H256 extends U8aFixed implements IHash {
|
||||
constructor (registry: Registry, value?: AnyU8a) {
|
||||
super(registry, value, 256);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public toRawType (): string {
|
||||
return 'H256';
|
||||
}
|
||||
}
|
||||
export default class H256 extends U8aFixed.with(256, 'H256') implements IHash {}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyU8a, Registry } from '../types';
|
||||
|
||||
import U8aFixed from '../codec/U8aFixed';
|
||||
|
||||
/**
|
||||
@@ -11,15 +9,4 @@ import U8aFixed from '../codec/U8aFixed';
|
||||
* @description
|
||||
* Hash containing 512 bits (64 bytes), typically used for signatures
|
||||
*/
|
||||
export default class H512 extends U8aFixed {
|
||||
constructor (registry: Registry, value?: AnyU8a) {
|
||||
super(registry, value, 512);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public toRawType (): string {
|
||||
return 'H512';
|
||||
}
|
||||
}
|
||||
export default class H512 extends U8aFixed.with(512, 'H512') {}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, Registry } from '../types';
|
||||
|
||||
import Int from '../codec/Int';
|
||||
|
||||
/**
|
||||
@@ -11,8 +9,4 @@ import Int from '../codec/Int';
|
||||
* @description
|
||||
* A 128-bit signed integer
|
||||
*/
|
||||
export default class I128 extends Int {
|
||||
constructor (registry: Registry, value?: AnyNumber) {
|
||||
super(registry, value, 128);
|
||||
}
|
||||
}
|
||||
export default class I128 extends Int.with(128) {}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, Registry } from '../types';
|
||||
|
||||
import Int from '../codec/Int';
|
||||
|
||||
/**
|
||||
@@ -11,8 +9,4 @@ import Int from '../codec/Int';
|
||||
* @description
|
||||
* A 16-bit signed integer
|
||||
*/
|
||||
export default class I16 extends Int {
|
||||
constructor (registry: Registry, value?: AnyNumber) {
|
||||
super(registry, value, 16);
|
||||
}
|
||||
}
|
||||
export default class I16 extends Int.with(16) {}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, Registry } from '../types';
|
||||
|
||||
import Int from '../codec/Int';
|
||||
|
||||
/**
|
||||
@@ -11,8 +9,4 @@ import Int from '../codec/Int';
|
||||
* @description
|
||||
* A 256-bit signed integer
|
||||
*/
|
||||
export default class I256 extends Int {
|
||||
constructor (registry: Registry, value?: AnyNumber) {
|
||||
super(registry, value, 256);
|
||||
}
|
||||
}
|
||||
export default class I256 extends Int.with(256) {}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, Registry } from '../types';
|
||||
|
||||
import Int from '../codec/Int';
|
||||
|
||||
/**
|
||||
@@ -11,8 +9,4 @@ import Int from '../codec/Int';
|
||||
* @description
|
||||
* A 32-bit signed integer
|
||||
*/
|
||||
export default class I32 extends Int {
|
||||
constructor (registry: Registry, value?: AnyNumber) {
|
||||
super(registry, value, 32);
|
||||
}
|
||||
}
|
||||
export default class I32 extends Int.with(32) {}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, Registry } from '../types';
|
||||
|
||||
import Int from '../codec/Int';
|
||||
|
||||
/**
|
||||
@@ -11,8 +9,4 @@ import Int from '../codec/Int';
|
||||
* @description
|
||||
* A 64-bit signed integer
|
||||
*/
|
||||
export default class I64 extends Int {
|
||||
constructor (registry: Registry, value?: AnyNumber) {
|
||||
super(registry, value, 64);
|
||||
}
|
||||
}
|
||||
export default class I64 extends Int.with(64) {}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, Registry } from '../types';
|
||||
|
||||
import Int from '../codec/Int';
|
||||
|
||||
/**
|
||||
@@ -11,8 +9,4 @@ import Int from '../codec/Int';
|
||||
* @description
|
||||
* An 8-bit signed integer
|
||||
*/
|
||||
export default class I8 extends Int {
|
||||
constructor (registry: Registry, value?: AnyNumber) {
|
||||
super(registry, value, 8);
|
||||
}
|
||||
}
|
||||
export default class I8 extends Int.with(8) {}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, Registry } from '../types';
|
||||
|
||||
import UInt from '../codec/UInt';
|
||||
|
||||
/**
|
||||
@@ -11,8 +9,4 @@ import UInt from '../codec/UInt';
|
||||
* @description
|
||||
* A 128-bit unsigned integer
|
||||
*/
|
||||
export default class U128 extends UInt {
|
||||
constructor (registry: Registry, value?: AnyNumber) {
|
||||
super(registry, value, 128);
|
||||
}
|
||||
}
|
||||
export default class U128 extends UInt.with(128) {}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, Registry } from '../types';
|
||||
|
||||
import UInt from '../codec/UInt';
|
||||
|
||||
/**
|
||||
@@ -11,8 +9,4 @@ import UInt from '../codec/UInt';
|
||||
* @description
|
||||
* A 16-bit unsigned integer
|
||||
*/
|
||||
export default class U16 extends UInt {
|
||||
constructor (registry: Registry, value?: AnyNumber) {
|
||||
super(registry, value, 16);
|
||||
}
|
||||
}
|
||||
export default class U16 extends UInt.with(16) {}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, Registry } from '../types';
|
||||
|
||||
import UInt from '../codec/UInt';
|
||||
|
||||
/**
|
||||
@@ -11,8 +9,4 @@ import UInt from '../codec/UInt';
|
||||
* @description
|
||||
* A 256-bit unsigned integer
|
||||
*/
|
||||
export default class U256 extends UInt {
|
||||
constructor (registry: Registry, value?: AnyNumber) {
|
||||
super(registry, value, 256);
|
||||
}
|
||||
}
|
||||
export default class U256 extends UInt.with(256) {}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, Registry } from '../types';
|
||||
|
||||
import UInt from '../codec/UInt';
|
||||
|
||||
/**
|
||||
@@ -11,8 +9,4 @@ import UInt from '../codec/UInt';
|
||||
* @description
|
||||
* A 32-bit unsigned integer
|
||||
*/
|
||||
export default class U32 extends UInt {
|
||||
constructor (registry: Registry, value?: AnyNumber) {
|
||||
super(registry, value, 32);
|
||||
}
|
||||
}
|
||||
export default class U32 extends UInt.with(32) {}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, Registry } from '../types';
|
||||
|
||||
import UInt from '../codec/UInt';
|
||||
|
||||
/**
|
||||
@@ -11,8 +9,4 @@ import UInt from '../codec/UInt';
|
||||
* @description
|
||||
* A 64-bit unsigned integer
|
||||
*/
|
||||
export default class U64 extends UInt {
|
||||
constructor (registry: Registry, value?: AnyNumber) {
|
||||
super(registry, value, 64);
|
||||
}
|
||||
}
|
||||
export default class U64 extends UInt.with(64) {}
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber, Registry } from '../types';
|
||||
|
||||
import UInt from '../codec/UInt';
|
||||
|
||||
/**
|
||||
@@ -11,8 +9,4 @@ import UInt from '../codec/UInt';
|
||||
* @description
|
||||
* An 8-bit unsigned integer
|
||||
*/
|
||||
export default class U8 extends UInt {
|
||||
constructor (registry: Registry, value?: AnyNumber) {
|
||||
super(registry, value, 8);
|
||||
}
|
||||
}
|
||||
export default class U8 extends UInt.with(8) {}
|
||||
|
||||
Reference in New Issue
Block a user