@polkadot/types
Advanced tools
Comparing version 0.30.39 to 0.30.40
@@ -114,2 +114,29 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
}); | ||
it('returns a type structure (actual)', () => { | ||
expect( | ||
getTypeDef('Vec<(PropIndex, Proposal, AccountId)>') | ||
).toEqual({ | ||
info: TypeDefInfo.Vector, | ||
type: 'Vec<(PropIndex, Proposal, AccountId)>', | ||
sub: { | ||
info: TypeDefInfo.Tuple, | ||
type: '(PropIndex, Proposal, AccountId)', | ||
sub: [ | ||
{ | ||
info: TypeDefInfo.Plain, | ||
type: 'PropIndex' | ||
}, | ||
{ | ||
info: TypeDefInfo.Plain, | ||
type: 'Proposal' | ||
}, | ||
{ | ||
info: TypeDefInfo.Plain, | ||
type: 'AccountId' | ||
} | ||
] | ||
} | ||
}); | ||
}); | ||
}); | ||
@@ -116,0 +143,0 @@ |
@@ -6,3 +6,6 @@ import Base from './Base'; | ||
new (value?: any): T; | ||
}, value?: Array<any>); | ||
}, value?: Uint8Array | string | Array<any>); | ||
static decode<T>(Type: { | ||
new (value?: any): T; | ||
}, value: Uint8Array | string | Array<any>): Array<T>; | ||
static with<O extends Base>(Type: { | ||
@@ -9,0 +12,0 @@ new (value?: any): O; |
@@ -14,8 +14,10 @@ "use strict"; | ||
require("core-js/modules/web.dom.iterable"); | ||
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); | ||
require("core-js/modules/web.dom.iterable"); | ||
var _concat = _interopRequireDefault(require("@polkadot/util/u8a/concat")); | ||
var _toU8a = _interopRequireDefault(require("@polkadot/util/u8a/toU8a")); | ||
var _Base = _interopRequireDefault(require("./Base")); | ||
@@ -35,3 +37,3 @@ | ||
constructor(Type, value = []) { | ||
super(value.map(entry => entry instanceof Type ? entry : new Type(entry))); | ||
super(Vector.decode(Type, value)); | ||
this._Type = void 0; | ||
@@ -41,2 +43,28 @@ this._Type = Type; | ||
static decode(Type, value) { | ||
if (Array.isArray(value)) { | ||
return value.map(entry => entry instanceof Type ? entry : new Type(entry)); | ||
} | ||
const u8a = (0, _toU8a.default)(value); | ||
let _Compact$decodeU8a = _Compact.default.decodeU8a(value, _Compact.DEFAULT_LENGTH_BITS), | ||
_Compact$decodeU8a2 = (0, _slicedToArray2.default)(_Compact$decodeU8a, 2), | ||
offset = _Compact$decodeU8a2[0], | ||
_length = _Compact$decodeU8a2[1]; | ||
const length = _length.toNumber(); | ||
const result = []; | ||
for (let index = 0; index < length; index++) { | ||
// @ts-ignore Not sure why we get "Property 'fromU8a' does not exist on type 'T'.", T extends Base in def? | ||
const decoded = new Type().fromU8a(u8a.subarray(offset)); | ||
result.push(decoded); | ||
offset += decoded.byteLength(); | ||
} | ||
return result; | ||
} | ||
static with(Type) { | ||
@@ -78,3 +106,4 @@ return class extends Vector { | ||
fromJSON(input) { | ||
this.raw = input.map(input => new this._Type().fromJSON(input)); | ||
// input could be null/undefined to indicate empty | ||
this.raw = (input || []).map(input => new this._Type().fromJSON(input)); | ||
return this; | ||
@@ -84,17 +113,3 @@ } | ||
fromU8a(input) { | ||
let _Compact$decodeU8a = _Compact.default.decodeU8a(input, _Compact.DEFAULT_LENGTH_BITS), | ||
_Compact$decodeU8a2 = (0, _slicedToArray2.default)(_Compact$decodeU8a, 2), | ||
offset = _Compact$decodeU8a2[0], | ||
_length = _Compact$decodeU8a2[1]; | ||
const length = _length.toNumber(); | ||
this.raw = []; | ||
for (let index = 0; index < length; index++) { | ||
const raw = new this._Type().fromU8a(input.subarray(offset)); | ||
this.raw.push(raw); | ||
offset += raw.byteLength(); | ||
} | ||
this.raw = Vector.decode(this._Type, input); | ||
return this; | ||
@@ -101,0 +116,0 @@ } |
@@ -5,2 +5,6 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
import extrinsics from '@polkadot/extrinsics/static'; | ||
import createType from './createType'; | ||
import Method from '../Method'; | ||
import Text from '../Text'; | ||
@@ -14,2 +18,4 @@ import Vector from './Vector'; | ||
array = new Vector(Text, [ '1', '23', '345', '4567', new Text('56789') ]); | ||
Method.injectExtrinsics(extrinsics); | ||
}); | ||
@@ -46,2 +52,13 @@ | ||
it('decodes a complex type via construction', () => { | ||
const test = createType('Vec<(PropIndex, Proposal, AccountId)>', new Uint8Array([ | ||
4, 10, 0, 0, 0, 0, 3, 80, 123, 10, 9, 34, 48, 120, 52, 50, 34, 58, 32, 34, 48, 120, 52, 51, 34, 10, 125, 10, 209, 114, 167, 76, 218, 76, 134, 89, 18, 195, 43, 160, 168, 10, 87, 174, 105, 171, 174, 65, 14, 92, 203, 89, 222, 232, 78, 47, 68, 50, 219, 79 | ||
])); | ||
const first = test.get(0); | ||
expect(first.get(0).toNumber()).toEqual(10); | ||
expect(first.get(1).callIndex).toEqual(new Uint8Array([0, 3])); | ||
expect(first.get(2).toString()).toEqual('5GoKvZWG5ZPYL1WUovuHW3zJBWBP5eT8CbqjdRY4Q6iMaDtZ'); | ||
}); | ||
describe('array-like functions', () => { | ||
@@ -48,0 +65,0 @@ it('allows retrieval of a specific item', () => { |
@@ -6,2 +6,3 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
import u8aConcat from '@polkadot/util/u8a/concat'; | ||
import toU8a from '@polkadot/util/u8a/toU8a'; | ||
@@ -21,12 +22,35 @@ import Base from './Base'; | ||
constructor (Type: { new(value?: any): T }, value: Array<any> = [] as Array<any>) { | ||
constructor (Type: { new(value?: any): T }, value: Uint8Array | string | Array<any> = [] as Array<any>) { | ||
super( | ||
value.map((entry) => | ||
Vector.decode(Type, value) | ||
); | ||
this._Type = Type; | ||
} | ||
static decode <T> (Type: { new(value?: any): T }, value: Uint8Array | string | Array<any>): Array<T> { | ||
if (Array.isArray(value)) { | ||
return value.map((entry) => | ||
entry instanceof Type | ||
? entry | ||
: new Type(entry) | ||
) | ||
); | ||
); | ||
} | ||
this._Type = Type; | ||
const u8a = toU8a(value); | ||
let [offset, _length] = Compact.decodeU8a(value, DEFAULT_LENGTH_BITS); | ||
const length = _length.toNumber(); | ||
const result = []; | ||
for (let index = 0; index < length; index++) { | ||
// @ts-ignore Not sure why we get "Property 'fromU8a' does not exist on type 'T'.", T extends Base in def? | ||
const decoded = new Type().fromU8a(u8a.subarray(offset)); | ||
result.push(decoded as T); | ||
offset += decoded.byteLength(); | ||
} | ||
return result; | ||
} | ||
@@ -69,3 +93,4 @@ | ||
fromJSON (input: any): Vector<T> { | ||
this.raw = input.map((input: any) => | ||
// input could be null/undefined to indicate empty | ||
this.raw = (input || []).map((input: any) => | ||
new this._Type().fromJSON(input) | ||
@@ -78,14 +103,4 @@ ); | ||
fromU8a (input: Uint8Array): Vector<T> { | ||
let [offset, _length] = Compact.decodeU8a(input, DEFAULT_LENGTH_BITS); | ||
const length = _length.toNumber(); | ||
this.raw = Vector.decode(this._Type, input); | ||
this.raw = []; | ||
for (let index = 0; index < length; index++) { | ||
const raw = new this._Type().fromU8a(input.subarray(offset)); | ||
this.raw.push(raw as T); | ||
offset += raw.byteLength(); | ||
} | ||
return this; | ||
@@ -92,0 +107,0 @@ } |
{ | ||
"name": "@polkadot/types", | ||
"version": "0.30.39", | ||
"version": "0.30.40", | ||
"description": "Implementation of the Parity codec", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
454554
11191