Tuple extends Array (#432)
* Tuple extend array * Fix all tests * Add tests on Tupel * Remove getAtIndex for tuples * Add noInheritDoc * Update comments * Update docs
This commit is contained in:
committed by
Jaco Greeff
parent
41e98ed243
commit
1d1832111b
@@ -20,7 +20,7 @@ async function main () {
|
||||
console.log(`\t\t${event.meta.documentation.toString()}`);
|
||||
|
||||
// loop through each of the parameters, displaying the type and data
|
||||
event.data.toArray().forEach((data, index) => {
|
||||
event.data.forEach((data, index) => {
|
||||
console.log(`\t\t\t${types[index].type}: ${data.toString()}`);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -12,9 +12,9 @@ const ProposalStruct: Constructor<Struct<any>> = Struct.with({ id: PropIndex, pr
|
||||
export class RxProposal extends ProposalStruct {
|
||||
constructor (value: Tuple) {
|
||||
super({
|
||||
id: value.getAtIndex(0),
|
||||
proposal: value.getAtIndex(1),
|
||||
address: value.getAtIndex(2)
|
||||
id: value[0],
|
||||
proposal: value[1],
|
||||
address: value[2]
|
||||
});
|
||||
}
|
||||
|
||||
@@ -36,8 +36,8 @@ const DepositStruct: Constructor<Struct<any>> = Struct.with({ balance: Balance,
|
||||
export class RxProposalDeposits extends DepositStruct {
|
||||
constructor (value: Tuple) {
|
||||
super({
|
||||
balance: value.getAtIndex(0),
|
||||
addresses: value.getAtIndex(1)
|
||||
balance: value[0],
|
||||
addresses: value[1]
|
||||
});
|
||||
}
|
||||
|
||||
@@ -55,9 +55,9 @@ const ReferendumStruct: Constructor<Struct<any>> = Struct.with({ blockNumber: Bl
|
||||
export class RxReferendum extends ReferendumStruct {
|
||||
constructor (value: Tuple, id: ReferendumIndex | BN | number) {
|
||||
super({
|
||||
blockNumber: value.getAtIndex(0),
|
||||
proposal: value.getAtIndex(1),
|
||||
voteThreshold: value.getAtIndex(2),
|
||||
blockNumber: value[0],
|
||||
proposal: value[1],
|
||||
voteThreshold: value[2],
|
||||
id
|
||||
});
|
||||
}
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
import { BftAuthoritySignature, BftHashSignature } from './Bft';
|
||||
|
||||
describe('BftAuthoritySignature', () => {
|
||||
const sig = new BftAuthoritySignature({
|
||||
authorityId: '0x12344321',
|
||||
signature: '0x567890098765'
|
||||
});
|
||||
const sig = new BftAuthoritySignature([
|
||||
'0x12344321',
|
||||
'0x567890098765'
|
||||
]);
|
||||
|
||||
it('has the correct authorityId', () => {
|
||||
expect(sig.authorityId.toHex()).toEqual('0x12344321');
|
||||
@@ -20,10 +20,10 @@ describe('BftAuthoritySignature', () => {
|
||||
});
|
||||
|
||||
describe('BftHashSignature', () => {
|
||||
const sig = new BftHashSignature({
|
||||
hash: '0x12344321',
|
||||
signature: '0x567890098765'
|
||||
});
|
||||
const sig = new BftHashSignature([
|
||||
'0x12344321',
|
||||
'0x567890098765'
|
||||
]);
|
||||
|
||||
it('has the correct hash', () => {
|
||||
expect(sig.hash.toHex()).toEqual('0x12344321');
|
||||
|
||||
+14
-20
@@ -9,10 +9,7 @@ import AuthorityId from './AuthorityId';
|
||||
import Hash from './Hash';
|
||||
import Signature from './Signature';
|
||||
|
||||
export type BftAuthoritySignatureValue = {
|
||||
authorityId?: AnyU8a,
|
||||
signature?: AnyU8a
|
||||
};
|
||||
export type BftAuthoritySignatureValue = [AnyU8a, AnyU8a];
|
||||
|
||||
/**
|
||||
* @name BftAuthoritySignature
|
||||
@@ -22,31 +19,28 @@ export type BftAuthoritySignatureValue = {
|
||||
*/
|
||||
export class BftAuthoritySignature extends Tuple {
|
||||
constructor (value?: BftAuthoritySignatureValue | Uint8Array) {
|
||||
super({
|
||||
authorityId: AuthorityId,
|
||||
signature: Signature
|
||||
}, value);
|
||||
super(
|
||||
[AuthorityId, Signature],
|
||||
value
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The wrapped [[AuthoriyId]]
|
||||
*/
|
||||
get authorityId (): AuthorityId {
|
||||
return this.getAtIndex(0) as AuthorityId;
|
||||
return this[0] as AuthorityId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The wrapped [[Signature]] value
|
||||
*/
|
||||
get signature (): Signature {
|
||||
return this.getAtIndex(1) as Signature;
|
||||
return this[1] as Signature;
|
||||
}
|
||||
}
|
||||
|
||||
export type BftHashSignatureValue = {
|
||||
hash?: AnyU8a,
|
||||
signature?: AnyU8a
|
||||
};
|
||||
export type BftHashSignatureValue = [AnyU8a, AnyU8a];
|
||||
|
||||
/**
|
||||
* @name BftHashSignature
|
||||
@@ -56,23 +50,23 @@ export type BftHashSignatureValue = {
|
||||
*/
|
||||
export class BftHashSignature extends Tuple {
|
||||
constructor (value?: BftHashSignatureValue | Uint8Array) {
|
||||
super({
|
||||
hash: Hash,
|
||||
signature: Signature
|
||||
}, value);
|
||||
super([
|
||||
Hash,
|
||||
Signature
|
||||
], value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The wrapped [[Hash]]
|
||||
*/
|
||||
get hash (): Hash {
|
||||
return this.getAtIndex(0) as Hash;
|
||||
return this[0] as Hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The wrapped [[Signature]]
|
||||
*/
|
||||
get signature (): Signature {
|
||||
return this.getAtIndex(1) as Signature;
|
||||
return this[1] as Signature;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,24 +43,24 @@ export class Other extends Bytes {
|
||||
*/
|
||||
export class Seal extends Tuple {
|
||||
constructor (value: any) {
|
||||
super({
|
||||
slot: U64,
|
||||
signature: Signature
|
||||
}, value);
|
||||
super([
|
||||
U64,
|
||||
Signature
|
||||
], value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The wrapped [[Signature]]
|
||||
*/
|
||||
get signature (): Signature {
|
||||
return this.get('signature') as Signature;
|
||||
return this[1] as Signature;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The wrapped [[U64]] slot
|
||||
*/
|
||||
get slot (): U64 {
|
||||
return this.get('slot') as U64;
|
||||
return this[0] as U64;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ export class Seal extends Tuple {
|
||||
* A [[EnumType]] the specifies the specific item in the logs of a [[Digest]]
|
||||
*/
|
||||
export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | Other
|
||||
| Seal> {
|
||||
| Seal> {
|
||||
constructor (value: any) {
|
||||
super([
|
||||
Other, // Position 0, as per Rust (encoding control)
|
||||
|
||||
@@ -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 { Constructor, ConstructorDef } from './types';
|
||||
import { Constructor } from './types';
|
||||
|
||||
import { isUndefined, stringCamelCase, u8aToHex } from '@polkadot/util';
|
||||
|
||||
@@ -25,7 +25,7 @@ export class EventData extends Tuple {
|
||||
private _section: string;
|
||||
private _typeDef: Array<TypeDef>;
|
||||
|
||||
constructor (Types: ConstructorDef, value: Uint8Array, typeDef: Array<TypeDef>, meta: EventMetadata, section: string, method: string) {
|
||||
constructor (Types: Array<Constructor>, value: Uint8Array, typeDef: Array<TypeDef>, meta: EventMetadata, section: string, method: string) {
|
||||
super(Types, value);
|
||||
|
||||
this._meta = meta;
|
||||
@@ -120,11 +120,7 @@ export default class Event extends Struct {
|
||||
const methodName = meta.name.toString();
|
||||
const eventIndex = new Uint8Array([sectionIndex, methodIndex]);
|
||||
const typeDef = meta.arguments.map((arg) => getTypeDef(arg));
|
||||
const Types = typeDef.reduce((result, def, index) => {
|
||||
result[index] = getTypeClass(def);
|
||||
|
||||
return result;
|
||||
}, {} as { [index: string]: Constructor });
|
||||
const Types = typeDef.map(getTypeClass);
|
||||
|
||||
EventTypes[eventIndex.toString()] = class extends EventData {
|
||||
constructor (value: Uint8Array) {
|
||||
|
||||
@@ -10,8 +10,8 @@ describe('RhdJustification', () => {
|
||||
roundNumber: 32,
|
||||
hash: '0xabcd',
|
||||
signatures: [
|
||||
{ authorityId: '0x1234', signature: '0x5678' },
|
||||
{ authorityId: '0x9876', signature: '0x5432' }
|
||||
['0x1234', '0x5678'],
|
||||
['0x9876', '0x5432']
|
||||
]
|
||||
});
|
||||
|
||||
|
||||
@@ -50,9 +50,9 @@ describe('KeyValue', () => {
|
||||
|
||||
describe('KeyValueOption', () => {
|
||||
it('exposes the properties for key/value', () => {
|
||||
const kv = new KeyValueOption({
|
||||
key: '0x11223344'
|
||||
});
|
||||
const kv = new KeyValueOption([
|
||||
'0x11223344'
|
||||
]);
|
||||
|
||||
expect(kv.key.toHex()).toEqual('0x11223344');
|
||||
expect(kv.value.isNone).toEqual(true);
|
||||
|
||||
@@ -46,10 +46,7 @@ export default class KeyValue extends Struct {
|
||||
}
|
||||
}
|
||||
|
||||
export type KeyValueOptionValue = {
|
||||
key?: AnyU8a,
|
||||
value?: AnyU8a
|
||||
};
|
||||
export type KeyValueOptionValue = [AnyU8a, AnyU8a?];
|
||||
|
||||
/**
|
||||
* @name KeyValueOption
|
||||
@@ -60,23 +57,23 @@ export type KeyValueOptionValue = {
|
||||
*/
|
||||
export class KeyValueOption extends Tuple {
|
||||
constructor (value?: KeyValueOptionValue | Uint8Array) {
|
||||
super({
|
||||
key: StorageKey,
|
||||
value: Option.with(StorageData)
|
||||
}, value);
|
||||
super([
|
||||
StorageKey,
|
||||
Option.with(StorageData)
|
||||
], value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[StorageKey]]
|
||||
*/
|
||||
get key (): StorageKey {
|
||||
return this.getAtIndex(0) as StorageKey;
|
||||
return this[0] as StorageKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[Option]] [[StorageData]]
|
||||
*/
|
||||
get value (): Option<StorageData> {
|
||||
return this.getAtIndex(1) as Option<StorageData>;
|
||||
return this[1] as Option<StorageData>;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,24 +125,24 @@ export class EventMetadata extends Struct {
|
||||
|
||||
export class OuterEventMetadataEvent extends Tuple {
|
||||
constructor (value?: any) {
|
||||
super({
|
||||
name: Text,
|
||||
events: Vector.with(EventMetadata)
|
||||
}, value);
|
||||
super([
|
||||
Text,
|
||||
Vector.with(EventMetadata)
|
||||
], value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[EventMetadata]]
|
||||
*/
|
||||
get events (): Vector<EventMetadata> {
|
||||
return this.getAtIndex(1) as Vector<EventMetadata>;
|
||||
return this[1] as Vector<EventMetadata>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The name of the section
|
||||
*/
|
||||
get name (): Text {
|
||||
return this.getAtIndex(0) as Text;
|
||||
return this[0] as Text;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,14 +7,8 @@ import MisbehaviorReport, { BftAtReport } from './MisbehaviorReport';
|
||||
describe('BftAtReport', () => {
|
||||
const report = new BftAtReport({
|
||||
round: 16,
|
||||
a: {
|
||||
hash: '0x1234',
|
||||
signature: '0x5678'
|
||||
},
|
||||
b: {
|
||||
hash: '0x8765',
|
||||
signature: '0x4321'
|
||||
}
|
||||
a: ['0x1234', '0x5678'],
|
||||
b: ['0x8765', '0x4321']
|
||||
});
|
||||
|
||||
it('has the correct round', () => {
|
||||
|
||||
@@ -34,24 +34,24 @@ type RuntimeVersionApiValue = {
|
||||
*/
|
||||
export class RuntimeVersionApi extends Tuple {
|
||||
constructor (value?: RuntimeVersionApiValue | Uint8Array) {
|
||||
super({
|
||||
id: ApiId,
|
||||
version: U32
|
||||
}, value);
|
||||
super([
|
||||
ApiId,
|
||||
U32
|
||||
], value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[ApiId]]
|
||||
*/
|
||||
get id (): ApiId {
|
||||
return this.getAtIndex(0) as ApiId;
|
||||
return this[0] as ApiId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The specific version as [[U32]]
|
||||
*/
|
||||
get version (): U32 {
|
||||
return this.getAtIndex(1) as U32;
|
||||
return this[1] as U32;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ describe('StorageChangeSet', () => {
|
||||
const set = new StorageChangeSet({
|
||||
block: '0x1234',
|
||||
changes: [
|
||||
{ key: '0xab', value: '0xcd' }
|
||||
['0xab', '0xcd']
|
||||
]
|
||||
});
|
||||
|
||||
|
||||
@@ -30,18 +30,18 @@ export type StoredPendingChangeValue = {
|
||||
*/
|
||||
export class NextAuthority extends Tuple {
|
||||
constructor (value?: Uint8Array | NextAuthorityValue) {
|
||||
super({
|
||||
sessionKey: SessionKey,
|
||||
index: U64
|
||||
}, value);
|
||||
super([
|
||||
SessionKey,
|
||||
U64
|
||||
], value);
|
||||
}
|
||||
|
||||
get index (): U64 {
|
||||
return this.get('index') as U64;
|
||||
return this[1] as U64;
|
||||
}
|
||||
|
||||
get sessionKey (): SessionKey {
|
||||
return this.get('sessionKey') as SessionKey;
|
||||
return this[0] as SessionKey;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
import extrinsics from '@polkadot/extrinsics/static';
|
||||
|
||||
import { CodecTo } from '../types';
|
||||
import Text from '../Text';
|
||||
import U32 from '../U32';
|
||||
import Tuple from './Tuple';
|
||||
@@ -16,51 +17,64 @@ describe('Tuple', () => {
|
||||
let tuple: Tuple;
|
||||
|
||||
beforeEach(() => {
|
||||
Method.injectExtrinsics(extrinsics);
|
||||
|
||||
// FIXME something weird here, injecting any :(
|
||||
tuple = new Tuple({
|
||||
foo: Text,
|
||||
bar: U32
|
||||
} as any, { foo: 'foo', bar: 69 });
|
||||
tuple = new Tuple(
|
||||
[Text, U32],
|
||||
['bazzing', 69]
|
||||
);
|
||||
});
|
||||
|
||||
it('returns array with toJSON', () => {
|
||||
expect(
|
||||
tuple.toJSON()
|
||||
).toEqual([
|
||||
'foo',
|
||||
69
|
||||
]);
|
||||
describe('decoding', () => {
|
||||
const testDecode = (type: string, input: any) =>
|
||||
it(`can decode from ${type}`, () => {
|
||||
const t = new Tuple([
|
||||
Text,
|
||||
U32
|
||||
], input);
|
||||
expect(
|
||||
t.toJSON()
|
||||
).toEqual(['bazzing', 69]);
|
||||
});
|
||||
|
||||
testDecode('array', ['bazzing', 69]);
|
||||
testDecode('hex', '0x1c62617a7a696e6745000000');
|
||||
testDecode('Uint8Array', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0]));
|
||||
});
|
||||
|
||||
it('initialises via JSON array', () => {
|
||||
const test = new (Tuple.with({
|
||||
a: Text,
|
||||
b: U32,
|
||||
c: Text
|
||||
}))([
|
||||
'bazzing', 32
|
||||
]);
|
||||
describe('encoding', () => {
|
||||
const testEncode = (to: CodecTo | 'toArray', expected: any) =>
|
||||
it(`can encode ${to}`, () => {
|
||||
expect(tuple[to]()).toEqual(expected);
|
||||
});
|
||||
|
||||
expect(
|
||||
test.toJSON()
|
||||
).toEqual([
|
||||
'bazzing',
|
||||
32,
|
||||
''
|
||||
]);
|
||||
testEncode('toHex', '0x1c62617a7a696e6745000000');
|
||||
testEncode('toJSON', ['bazzing', 69]);
|
||||
testEncode('toU8a', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0]));
|
||||
testEncode('toString', '["bazzing",69]');
|
||||
});
|
||||
|
||||
it('creates properly via actual hex string', () => {
|
||||
const test = new (Tuple.with({
|
||||
blockNumber: BlockNumber,
|
||||
proposal: Proposal,
|
||||
threshold: VoteThreshold
|
||||
}))('0x62190000000000000003507b0a092230783432223a202230783433220a7d0a01');
|
||||
Method.injectExtrinsics(extrinsics);
|
||||
|
||||
expect((test.getAtIndex(0) as BlockNumber).toNumber()).toEqual(6498);
|
||||
expect((test.getAtIndex(1) as Method).callIndex).toEqual(new Uint8Array([0, 3]));
|
||||
expect((test.getAtIndex(2) as VoteThreshold).toNumber()).toEqual(1);
|
||||
const test = new (Tuple.with([
|
||||
BlockNumber, Proposal, VoteThreshold
|
||||
]
|
||||
))('0x62190000000000000003507b0a092230783432223a202230783433220a7d0a01');
|
||||
|
||||
expect((test[0] as BlockNumber).toNumber()).toEqual(6498);
|
||||
expect((test[1] as Method).callIndex).toEqual(new Uint8Array([0, 3]));
|
||||
expect((test[2] as VoteThreshold).toNumber()).toEqual(1);
|
||||
});
|
||||
|
||||
it('exposes the Types', () => {
|
||||
expect(tuple.Types).toEqual(['Text', 'U32']);
|
||||
});
|
||||
|
||||
it("doesn't expose filter", () => {
|
||||
expect(tuple.filter).toThrowError(/is not implemented on Tuple/);
|
||||
});
|
||||
|
||||
it("doesn't expose map", () => {
|
||||
expect(tuple.map).toThrowError(/is not implemented on Tuple/);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -2,24 +2,56 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Codec, Constructor, ConstructorDef } from '../types';
|
||||
import Struct from './Struct';
|
||||
import { isU8a, u8aConcat, u8aToHex, isHex, hexToU8a } from '@polkadot/util';
|
||||
|
||||
import { AnyNumber, AnyU8a, AnyString, Codec, Constructor } from '../types';
|
||||
|
||||
/**
|
||||
* @name Tuple
|
||||
* @description
|
||||
* A Tuple defines an anonymous Object with key/values - where the values are Codec values.
|
||||
* It is a specialization of the Struct type where the toJSON operates on Array structures,
|
||||
* while the U8a encoding is handled in the same way as a Struct
|
||||
* A Tuple defines an anonymous fixed-length array, where each element has its
|
||||
* own type. It extends the base JS `Array` object.
|
||||
* @noInheritDoc
|
||||
*/
|
||||
export default class Tuple<
|
||||
// S & T definitions maps to what we have in Struct (naming documented there)
|
||||
S extends ConstructorDef = { [index: string]: Constructor<Codec> },
|
||||
T extends { [K in keyof S]: Codec } = { [K in keyof S]: Codec },
|
||||
V extends { [K in keyof S]: any } = { [K in keyof S]: any }
|
||||
> extends Struct<S, T, V> {
|
||||
S extends Array<Constructor> = Array<Constructor>
|
||||
> extends Array<Codec> implements Codec {
|
||||
private _Types: S;
|
||||
|
||||
constructor (Types: S, value: any) {
|
||||
super(
|
||||
...Tuple.decodeTuple(Types, value)
|
||||
);
|
||||
this._Types = Types;
|
||||
}
|
||||
|
||||
private static decodeTuple<
|
||||
S extends Array<Constructor>
|
||||
> (Types: S, value: AnyU8a | string | Array<AnyU8a | AnyNumber | AnyString | undefined | null>): Array<Codec> {
|
||||
if (isU8a(value)) {
|
||||
// `currentIndex` is only used when we have a UintArray/U8a as value. It's
|
||||
// used to track at which index we are currently parsing in the U8a.
|
||||
let currentIndex = 0;
|
||||
|
||||
return Types
|
||||
.map((Type) => {
|
||||
const raw = new Type(value.subarray(currentIndex));
|
||||
// Move the currentIndex forward
|
||||
currentIndex += raw.encodedLength;
|
||||
|
||||
return raw;
|
||||
});
|
||||
} else if (isHex(value)) {
|
||||
return Tuple.decodeTuple(Types, hexToU8a(value));
|
||||
} else {
|
||||
return Types.map((Type, index) => {
|
||||
return new Type(value && value[index]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static with<
|
||||
S extends ConstructorDef = { [index: string]: Constructor<Codec> }
|
||||
S extends Array<Constructor>
|
||||
> (Types: S): Constructor<Tuple<S>> {
|
||||
return class extends Tuple<S> {
|
||||
constructor (value?: any) {
|
||||
@@ -28,12 +60,80 @@ export default class Tuple<
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
get encodedLength (): number {
|
||||
return this.reduce((length, entry) => {
|
||||
return length += entry.encodedLength;
|
||||
}, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The types definition of the tuple
|
||||
*/
|
||||
get Types (): Array<string> {
|
||||
return this._Types.map(({ name }) => name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Converts the Object to an standard JavaScript Array
|
||||
*/
|
||||
toArray (): Array<Codec> {
|
||||
return Array.from(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
toHex () {
|
||||
return u8aToHex(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||
*/
|
||||
toJSON (): any {
|
||||
return [...this.values()].map((entry) =>
|
||||
return this.toArray().map((entry) =>
|
||||
entry.toJSON()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the string representation of the value
|
||||
*/
|
||||
toString () {
|
||||
// Overwrite the default toString representation of Array.
|
||||
return JSON.stringify(this.toJSON());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Encodes the value as a Uint8Array as per the parity-codec specifications
|
||||
* @param isBare true when the value has none of the type-specific prefixes (internal)
|
||||
*/
|
||||
toU8a (isBare?: boolean): Uint8Array {
|
||||
return u8aConcat(
|
||||
...this.toArray().map((entry) =>
|
||||
entry.toU8a(isBare)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Below are methods that we override. When we do a `new Tuple(...).map()`,
|
||||
// we want it to throw an error. We only override the methods that return a
|
||||
// new instance.
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
filter (callbackfn: (value: Codec, index: number, array: Array<Codec>) => any, thisArg?: any): Array<Codec> {
|
||||
throw new Error("Method 'filter' is not implemented on Tuple.");
|
||||
}
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
*/
|
||||
map<U> (callbackfn: (value: Codec, index: number, array: Array<Codec>) => U, thisArg?: any): Array<U> {
|
||||
throw new Error("Method 'map' is not implemented on Tuple.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,9 +58,9 @@ describe('Vector', () => {
|
||||
]));
|
||||
const first = (test as Vector<Codec>)[0] as Tuple;
|
||||
|
||||
expect((first.getAtIndex(0) as PropIndex).toNumber()).toEqual(10);
|
||||
expect((first.getAtIndex(1) as Proposal).callIndex).toEqual(new Uint8Array([0, 3]));
|
||||
expect((first.getAtIndex(2) as AccountId).toString()).toEqual('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ');
|
||||
expect((first[0] as PropIndex).toNumber()).toEqual(10);
|
||||
expect((first[1] as Proposal).callIndex).toEqual(new Uint8Array([0, 3]));
|
||||
expect((first[2] as AccountId).toString()).toEqual('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ');
|
||||
});
|
||||
|
||||
describe('vector-like functions', () => {
|
||||
|
||||
@@ -122,11 +122,7 @@ export function getTypeClass (value: TypeDef): Constructor {
|
||||
}
|
||||
|
||||
return Tuple.with(
|
||||
value.sub.reduce((result, type, index) => {
|
||||
result[`entry${index}`] = getTypeClass(type);
|
||||
|
||||
return result;
|
||||
}, {} as { [index: string]: Constructor })
|
||||
value.sub.map(getTypeClass)
|
||||
);
|
||||
} else if (value.info === TypeDefInfo.Compact) {
|
||||
if (!value.sub || Array.isArray(value.sub)) {
|
||||
|
||||
Reference in New Issue
Block a user