@polkadot/types
Advanced tools
Comparing version 0.30.41 to 0.30.42
@@ -8,2 +8,3 @@ import Base from './Base'; | ||
constructor(def: EnumDef, value?: Enum | number); | ||
static decodeEnum(value?: Enum | number): number; | ||
byteLength(): number; | ||
@@ -10,0 +11,0 @@ fromJSON(input: any): Enum; |
@@ -10,2 +10,4 @@ "use strict"; | ||
var _u8a = _interopRequireDefault(require("@polkadot/util/is/u8a")); | ||
var _Base = _interopRequireDefault(require("./Base")); | ||
@@ -25,3 +27,3 @@ | ||
constructor(def, value = 0) { | ||
super(value instanceof Enum ? value.raw : value); | ||
super(Enum.decodeEnum(value)); | ||
this._enum = void 0; | ||
@@ -31,2 +33,12 @@ this._enum = def; | ||
static decodeEnum(value = 0) { | ||
if (value instanceof Enum) { | ||
return value.raw; | ||
} else if ((0, _u8a.default)(value)) { | ||
return value[0]; | ||
} else { | ||
return value; | ||
} | ||
} | ||
byteLength() { | ||
@@ -33,0 +45,0 @@ return 1; |
@@ -7,3 +7,28 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
describe('Struct', () => { | ||
const testDecode = (type, input, expected) => | ||
it(`can decode from ${type}`, () => { | ||
const e = new Enum(['foo', 'bar'], input); | ||
expect(e.toString()).toBe(expected); | ||
}); | ||
const testEncode = (to, expected) => | ||
it(`can encode ${to}`, () => { | ||
const e = new Enum(['foo', 'bar'], 1); | ||
expect(e[to]()).toEqual(expected); | ||
}); | ||
describe('Enum', () => { | ||
testDecode('Enum', undefined, 'foo'); | ||
testDecode('Enum', new Enum([], 1), 'bar'); | ||
testDecode('number', 0, 'foo'); | ||
testDecode('number', 1, 'bar'); | ||
testDecode('Uint8Array', Uint8Array.from([0]), 'foo'); | ||
testDecode('Uint8Array', Uint8Array.from([1]), 'bar'); | ||
testEncode('toJSON', 1); | ||
testEncode('toNumber', 1); | ||
testEncode('toString', 'bar'); | ||
testEncode('toU8a', Uint8Array.from([1])); | ||
it('provides a clean toString()', () => { | ||
@@ -10,0 +35,0 @@ expect( |
@@ -5,2 +5,4 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
import isU8a from '@polkadot/util/is/u8a'; | ||
import Base from './Base'; | ||
@@ -24,5 +26,3 @@ | ||
super( | ||
value instanceof Enum | ||
? value.raw | ||
: value | ||
Enum.decodeEnum(value) | ||
); | ||
@@ -33,2 +33,12 @@ | ||
static decodeEnum (value: Enum | number = 0): number { | ||
if (value instanceof Enum) { | ||
return value.raw; | ||
} else if (isU8a(value)) { | ||
return value[0]; | ||
} else { | ||
return value; | ||
} | ||
} | ||
byteLength (): number { | ||
@@ -35,0 +45,0 @@ return 1; |
import Base from './Base'; | ||
export default class Option<T> extends Base<Base<T>> { | ||
private _isEmpty; | ||
constructor(Value: { | ||
constructor(Type: { | ||
new (value?: any): Base<T>; | ||
}, value?: any); | ||
static decodeOption<O>(Type: { | ||
new (value?: any): Base<O>; | ||
}, value?: any): Base<O>; | ||
static with<O>(Type: { | ||
@@ -8,0 +11,0 @@ new (value?: any): Base<O>; |
@@ -12,2 +12,4 @@ "use strict"; | ||
var _u8a = _interopRequireDefault(require("@polkadot/util/is/u8a")); | ||
var _undefined = _interopRequireDefault(require("@polkadot/util/is/undefined")); | ||
@@ -25,8 +27,16 @@ | ||
class Option extends _Base.default { | ||
constructor(Value, value) { | ||
super(new Value(value)); | ||
constructor(Type, value) { | ||
super(Option.decodeOption(Type, value)); | ||
this._isEmpty = void 0; | ||
this._isEmpty = (0, _undefined.default)(value); | ||
this._isEmpty = (0, _undefined.default)(value) || value.length <= 1; | ||
} | ||
static decodeOption(Type, value) { | ||
if ((0, _u8a.default)(value)) { | ||
return new Type(value.subarray(1)); | ||
} | ||
return new Type(value); | ||
} | ||
static with(Type) { | ||
@@ -33,0 +43,0 @@ return class extends Option { |
@@ -8,17 +8,26 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
describe('Option', () => { | ||
it('converts fromJSON() with', () => { | ||
const o = new Option(Text).fromJSON('1234'); | ||
const testDecode = (type, input, expected) => | ||
it(`can decode from ${type}`, () => { | ||
const o = new Option(Text, input); | ||
expect(o.isEmpty).toBe(!expected.length) | ||
expect(o.toString()).toBe(expected); | ||
}); | ||
expect(o.isEmpty).toEqual(false); | ||
expect(o.toString()).toEqual('1234'); | ||
const testEncode = (to, expected) => | ||
it(`can encode ${to}`, () => { | ||
const e = new Option(Text, 'foo'); | ||
expect(e[to]()).toEqual(expected); | ||
}); | ||
it('converts fromJSON() without', () => { | ||
const o = new Option(Text, '1234').fromJSON(); | ||
describe('Option', () => { | ||
expect(o.isEmpty).toEqual(true); | ||
expect(o.toString()).toEqual(''); | ||
}); | ||
testDecode('string (with)', 'foo', 'foo'); | ||
testDecode('string (without)', undefined, ''); | ||
testDecode('Uint8Array (with)', Uint8Array.from([1, 12, 102, 111, 111]), 'foo'); | ||
testDecode('Uint8Array (without)', Uint8Array.from([0]), ''); | ||
// testEncode('toHex', '0x010c666f6f'); // FIXME Add this | ||
testEncode('toString', 'foo'); | ||
testEncode('toU8a', Uint8Array.from([1, 12, 102, 111, 111])); | ||
it('has empty toString() (undefined)', () => { | ||
@@ -25,0 +34,0 @@ expect( |
@@ -5,2 +5,3 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
import isU8a from '@polkadot/util/is/u8a'; | ||
import isUndefined from '@polkadot/util/is/undefined'; | ||
@@ -14,14 +15,21 @@ | ||
// with a value if/as required/found. | ||
export default class Option <T> extends Base<Base<T>> { | ||
export default class Option<T> extends Base<Base<T>> { | ||
private _isEmpty: boolean; | ||
constructor (Value: { new(value?: any): Base<T> }, value?: any) { | ||
constructor (Type: { new(value?: any): Base<T> }, value?: any) { | ||
super( | ||
new Value(value) | ||
Option.decodeOption(Type, value) | ||
); | ||
this._isEmpty = isUndefined(value); | ||
this._isEmpty = isUndefined(value) || value.length <= 1; | ||
} | ||
static with <O> (Type: { new(value?: any): Base<O> }): { new(value?: any): Option<O> } { | ||
static decodeOption<O> (Type: { new(value?: any): Base<O> }, value?: any): Base<O> { | ||
if (isU8a(value)) { | ||
return new Type(value.subarray(1)); | ||
} | ||
return new Type(value); | ||
} | ||
static with<O> (Type: { new(value?: any): Base<O> }): { new(value?: any): Option<O> } { | ||
return class extends Option<O> { | ||
@@ -28,0 +36,0 @@ constructor (value?: any) { |
@@ -16,3 +16,3 @@ import Base from './Base'; | ||
constructor(Types: S, value?: V | Array<any>, jsonMap?: Map<keyof S, string>); | ||
static decode<S, V, T>(Types: S, value: V | Array<any>): T; | ||
static decodeStruct<S, V, T>(Types: S, value: any): T; | ||
static with<S = { | ||
@@ -19,0 +19,0 @@ [index: string]: { |
@@ -14,12 +14,16 @@ "use strict"; | ||
var _toU8a = _interopRequireDefault(require("@polkadot/util/hex/toU8a")); | ||
var _hex = _interopRequireDefault(require("@polkadot/util/is/hex")); | ||
var _object = _interopRequireDefault(require("@polkadot/util/is/object")); | ||
var _u8a = _interopRequireDefault(require("@polkadot/util/is/u8a")); | ||
var _concat = _interopRequireDefault(require("@polkadot/util/u8a/concat")); | ||
var _toU8a = _interopRequireDefault(require("@polkadot/util/u8a/toU8a")); | ||
var _toU8a2 = _interopRequireDefault(require("@polkadot/util/u8a/toU8a")); | ||
var _Base = _interopRequireDefault(require("./Base")); | ||
var _u8a = _interopRequireDefault(require("@polkadot/util/is/u8a")); | ||
// Copyright 2017-2018 @polkadot/types authors & contributors | ||
@@ -35,3 +39,3 @@ // This software may be modified and distributed under the terms | ||
constructor(Types, value = {}, jsonMap = new Map()) { | ||
super(Struct.decode(Types, value)); | ||
super(Struct.decodeStruct(Types, value)); | ||
this._jsonMap = void 0; | ||
@@ -47,13 +51,35 @@ this._Types = void 0; | ||
static decode(Types, value) { | ||
static decodeStruct(Types, value) { | ||
// l.debug(() => ['Struct.decode', { Types, value }]); | ||
if ((0, _hex.default)(value)) { | ||
return Struct.decodeStruct(Types, (0, _toU8a.default)(value)); | ||
} else if (!value) { | ||
return {}; | ||
} // `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 that array. | ||
let currentIndex = 0; | ||
return Object.keys(Types).reduce((raw, key, index) => { | ||
// FIXME Ok, something weird is going on here or I just don't get it... | ||
// it works, so ignore the checker, although it drives me batty. (It started when | ||
// the [key in keyof T] was added, the idea is to provide better checks, which | ||
// does backfire here, but works externally.) | ||
// @ts-ignore | ||
raw[key] = value[key] instanceof Types[key] // @ts-ignore | ||
? value[key] // @ts-ignore | ||
: new Types[key](value[key]); | ||
if ((0, _u8a.default)(value)) { | ||
// @ts-ignore FIXME See below | ||
raw[key] = new Types[key](value.subarray(currentIndex)); // Move the currentIndex forward | ||
// @ts-ignore FIXME See below | ||
currentIndex += raw[key].byteLength(); // @ts-ignore FIXME See below | ||
} else if (value[key] instanceof Types[key]) { | ||
// @ts-ignore FIXME See below | ||
raw[key] = value[key]; | ||
} else if ((0, _object.default)(value)) { | ||
// @ts-ignore FIXME Ok, something weird is going on here or I just don't get it... | ||
// it works, so ignore the checker, although it drives me batty. (It started when | ||
// the [key in keyof T] was added, the idea is to provide better checks, which | ||
// does backfire here, but works externally.) | ||
raw[key] = new Types[key]( // @ts-ignore FIXME | ||
value[key]); | ||
} else { | ||
// @ts-ignore FIXME | ||
throw new Error(`Struct: cannot decode type "${Types[key].name}" with value "${value}".`); | ||
} | ||
return raw; | ||
@@ -84,3 +110,3 @@ }, {}); | ||
if ((0, _hex.default)(input) || (0, _u8a.default)(input)) { | ||
return this.fromU8a((0, _toU8a.default)(input)); | ||
return this.fromU8a((0, _toU8a2.default)(input)); | ||
} // null & undefined yields empty | ||
@@ -87,0 +113,0 @@ |
@@ -8,4 +8,46 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
import U32 from '../U32'; | ||
import Vector from './Vector'; | ||
const testDecode = (type, input) => | ||
it(`can decode from ${type}`, () => { | ||
const s = new Struct({ | ||
foo: Text, | ||
bar: U32 | ||
}, input); | ||
expect(s.keys()).toEqual(['foo', 'bar']); | ||
expect( | ||
s.values().map((v) => | ||
v.toString() | ||
) | ||
).toEqual(['bazzing', '69']); | ||
}); | ||
const testEncode = (to, expected) => | ||
it(`can encode ${to}`, () => { | ||
const s = new Struct({ | ||
foo: Text, | ||
bar: U32 | ||
}, { foo: 'bazzing', bar: 69 }); | ||
expect(s[to]()).toEqual(expected); | ||
}); | ||
describe('Struct', () => { | ||
testDecode('object', { foo: 'bazzing', bar: 69 }); | ||
testDecode('Uint8Array', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0])); | ||
// testEncode('toHex', '0x1c62617a7a696e6745000000'); // FIXME Add this | ||
testEncode('toU8a', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0])); | ||
testEncode('toString', '{foo: bazzing, bar: 69}'); | ||
it('decodes a more complicated type', () => { | ||
const s = new Struct({ | ||
foo: Vector.with(Struct.with({ | ||
bar: Text | ||
})) | ||
}, { foo: [{ bar: 1 }, { bar: 2 }] }); | ||
expect(s.toString()).toBe('{foo: [{bar: 1}, {bar: 2}]}'); | ||
}) | ||
it('provides a clean toString()', () => { | ||
@@ -12,0 +54,0 @@ expect( |
@@ -5,3 +5,6 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
import hexToU8a from '@polkadot/util/hex/toU8a'; | ||
import isHex from '@polkadot/util/is/hex'; | ||
import isObject from '@polkadot/util/is/object'; | ||
import isU8a from '@polkadot/util/is/u8a'; | ||
import u8aConcat from '@polkadot/util/u8a/concat'; | ||
@@ -11,3 +14,2 @@ import u8aToU8a from '@polkadot/util/u8a/toU8a'; | ||
import Base from './Base'; | ||
import isU8a from '@polkadot/util/is/u8a'; | ||
@@ -19,3 +21,3 @@ // A Struct defines an Object with key/values - where the values are Base<T> values. It removes | ||
// it needs to decoded in the specific defined order. | ||
export default class Struct < | ||
export default class Struct< | ||
// The actual Class structure, i.e. key -> Class | ||
@@ -29,3 +31,3 @@ S = { [index: string]: { new(...value: Array<any>): Base } }, | ||
E = { [K in keyof S]: string } | ||
> extends Base<T> { | ||
> extends Base<T> { | ||
protected _jsonMap: Map<keyof S, string>; | ||
@@ -36,3 +38,3 @@ protected _Types: E; | ||
super( | ||
Struct.decode(Types, value) | ||
Struct.decodeStruct(Types, value) | ||
); | ||
@@ -51,19 +53,48 @@ | ||
static decode <S, V, T> (Types: S, value: V | Array<any>): T { | ||
static decodeStruct<S, V, T> (Types: S, value: any): T { | ||
// l.debug(() => ['Struct.decode', { Types, value }]); | ||
if (isHex(value)) { | ||
return Struct.decodeStruct(Types, hexToU8a(value as string)); | ||
} else if (!value) { | ||
return {} as T; | ||
} | ||
// `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 that array. | ||
let currentIndex = 0; | ||
return Object | ||
.keys(Types) | ||
.reduce((raw: T, key, index) => { | ||
// FIXME Ok, something weird is going on here or I just don't get it... | ||
// it works, so ignore the checker, although it drives me batty. (It started when | ||
// the [key in keyof T] was added, the idea is to provide better checks, which | ||
// does backfire here, but works externally.) | ||
// @ts-ignore | ||
raw[key] = value[key] instanceof Types[key] | ||
// @ts-ignore | ||
? value[key] | ||
// @ts-ignore | ||
: new Types[key](value[key]); | ||
if (isU8a(value)) { | ||
// @ts-ignore FIXME See below | ||
raw[key] = new Types[key]( | ||
value.subarray( | ||
currentIndex | ||
) | ||
); | ||
// Move the currentIndex forward | ||
// @ts-ignore FIXME See below | ||
currentIndex += raw[key].byteLength(); | ||
// @ts-ignore FIXME See below | ||
} else if (value[key] instanceof Types[key]) { | ||
// @ts-ignore FIXME See below | ||
raw[key] = value[key]; | ||
} else if (isObject(value)) { | ||
// @ts-ignore FIXME Ok, something weird is going on here or I just don't get it... | ||
// it works, so ignore the checker, although it drives me batty. (It started when | ||
// the [key in keyof T] was added, the idea is to provide better checks, which | ||
// does backfire here, but works externally.) | ||
raw[key] = new Types[key]( | ||
// @ts-ignore FIXME | ||
value[key] | ||
); | ||
} else { | ||
// @ts-ignore FIXME | ||
throw new Error(`Struct: cannot decode type "${Types[key].name}" with value "${value}".`); | ||
} | ||
return raw; | ||
@@ -73,5 +104,5 @@ }, {} as T); | ||
static with < | ||
static with< | ||
S = { [index: string]: { new(value?: any): Base } } | ||
> (Types: S): { new(value?: any): Struct<S> } { | ||
> (Types: S): { new(value?: any): Struct<S> } { | ||
return class extends Struct<S> { | ||
@@ -78,0 +109,0 @@ constructor (value?: any, jsonMap?: Map<keyof S, string>) { |
@@ -19,3 +19,3 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
// while the U8a encoding is handled in the same way as a Struct | ||
export default class Tuple < | ||
export default class Tuple< | ||
// S & T definitions maps to what we have in Struct (naming documented there) | ||
@@ -25,3 +25,3 @@ S = { [index: string]: { new(value?: any): Base } }, | ||
V = { [K in keyof S]: any } | ||
> extends Struct<S, T, V> { | ||
> extends Struct<S, T, V> { | ||
constructor (Types: S, value?: V | AnyU8a, jsonMap?: Map<keyof S, string>) { | ||
@@ -31,3 +31,3 @@ super(Types, Tuple.decodeU8a(Types, value), jsonMap); | ||
static decodeU8a <S, V> (Types: S, _value: V | AnyU8a): V { | ||
static decodeU8a<S, V> (Types: S, _value: V | AnyU8a): V { | ||
if (!isU8a(_value) && !isString(_value) && !Array.isArray(_value)) { | ||
@@ -53,5 +53,5 @@ return _value as V; | ||
static with < | ||
static with< | ||
S = { [index: string]: { new(value?: any): Base } } | ||
> (Types: S): { new(value?: any): Tuple<S> } { | ||
> (Types: S): { new(value?: any): Tuple<S> } { | ||
return class extends Tuple<S> { | ||
@@ -58,0 +58,0 @@ constructor (value?: any) { |
@@ -5,2 +5,3 @@ import { AnyU8a } from '../types'; | ||
constructor(value?: AnyU8a); | ||
static decodeU8a(value: any): Uint8Array; | ||
readonly length: number; | ||
@@ -7,0 +8,0 @@ byteLength(): number; |
@@ -10,2 +10,4 @@ "use strict"; | ||
var _u8a = _interopRequireDefault(require("@polkadot/util/is/u8a")); | ||
var _toHex = _interopRequireDefault(require("@polkadot/util/u8a/toHex")); | ||
@@ -26,5 +28,15 @@ | ||
constructor(value = new Uint8Array()) { | ||
super(value instanceof U8a ? value.raw : (0, _toU8a.default)(value)); | ||
super(U8a.decodeU8a(value)); | ||
} | ||
static decodeU8a(value) { | ||
if ((0, _u8a.default)(value)) { | ||
return value; | ||
} else if (value instanceof U8a) { | ||
return value.raw; | ||
} else { | ||
return (0, _toU8a.default)(value); | ||
} | ||
} | ||
get length() { | ||
@@ -31,0 +43,0 @@ return this.raw.length; |
@@ -7,2 +7,14 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
const testDecode = (type, input, expected) => | ||
it(`can decode from ${type}`, () => { | ||
const e = new U8a(input); | ||
expect(e.toString()).toBe(expected); | ||
}); | ||
const testEncode = (to, expected) => | ||
it(`can encode ${to}`, () => { | ||
const e = new U8a([1, 2, 3, 4, 5]); | ||
expect(e[to]()).toEqual(expected); | ||
}); | ||
describe('U8a', () => { | ||
@@ -15,2 +27,12 @@ let u8a; | ||
testDecode('Array', [1, 2, 3, 4, 5], '0x0102030405') | ||
testDecode('hex', '0x0102030405', '0x0102030405'); | ||
testDecode('U8a', new Uint8Array([1, 2, 3, 4, 5]), '0x0102030405'); | ||
testDecode('Uint8Array', Uint8Array.from([1, 2, 3, 4, 5]), '0x0102030405'); | ||
testEncode('toJSON', '0x0102030405'); | ||
testEncode('toHex', '0x0102030405'); | ||
testEncode('toString', '0x0102030405'); | ||
testEncode('toU8a', Uint8Array.from([1, 2, 3, 4, 5])); | ||
it('contains the length of the elements', () => { | ||
@@ -17,0 +39,0 @@ expect(u8a.length).toEqual(5); // eslint-disable-line |
@@ -5,7 +5,7 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
import { AnyU8a } from '../types'; | ||
import isU8a from '@polkadot/util/is/u8a'; | ||
import u8aToHex from '@polkadot/util/u8a/toHex'; | ||
import toU8a from '@polkadot/util/u8a/toU8a'; | ||
import { AnyU8a } from '../types'; | ||
import Base from './Base'; | ||
@@ -20,8 +20,16 @@ | ||
super( | ||
value instanceof U8a | ||
? value.raw | ||
: toU8a(value) | ||
U8a.decodeU8a(value) | ||
); | ||
} | ||
static decodeU8a (value: any): Uint8Array { | ||
if (isU8a(value)) { | ||
return value; | ||
} else if (value instanceof U8a) { | ||
return value.raw; | ||
} else { | ||
return toU8a(value); | ||
} | ||
} | ||
get length (): number { | ||
@@ -28,0 +36,0 @@ return this.raw.length; |
@@ -10,3 +10,3 @@ import { AnyNumber } from '../types'; | ||
constructor(value?: AnyNumber, bitLength?: UIntBitLength, isHexJson?: boolean); | ||
static decode(value: AnyNumber): BN; | ||
static decodeUInt(value: AnyNumber): BN; | ||
byteLength(): number; | ||
@@ -13,0 +13,0 @@ fromJSON(input: any): UInt; |
@@ -46,3 +46,3 @@ "use strict"; | ||
constructor(value = 0, bitLength = DEFAULT_UINT_BITS, isHexJson = true) { | ||
super(UInt.decode(value)); | ||
super(UInt.decodeUInt(value)); | ||
this._bitLength = void 0; | ||
@@ -54,3 +54,3 @@ this._isHexJson = void 0; | ||
static decode(value) { | ||
static decodeUInt(value) { | ||
if (value instanceof UInt) { | ||
@@ -75,3 +75,3 @@ return value.raw; | ||
fromJSON(input) { | ||
this.raw = UInt.decode(input); | ||
this.raw = UInt.decodeUInt(input); | ||
return this; | ||
@@ -78,0 +78,0 @@ } |
@@ -35,3 +35,3 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
super( | ||
UInt.decode(value) | ||
UInt.decodeUInt(value) | ||
); | ||
@@ -43,3 +43,3 @@ | ||
static decode (value: AnyNumber): BN { | ||
static decodeUInt (value: AnyNumber): BN { | ||
if (value instanceof UInt) { | ||
@@ -64,3 +64,3 @@ return value.raw; | ||
fromJSON (input: any): UInt { | ||
this.raw = UInt.decode(input); | ||
this.raw = UInt.decodeUInt(input); | ||
@@ -67,0 +67,0 @@ return this; |
@@ -58,2 +58,4 @@ "use strict"; | ||
for (let index = 0; index < length; index++) { | ||
// FIXME replace by | ||
// const decoded = new Type(u8a.subarray(offset)); | ||
// @ts-ignore Not sure why we get "Property 'fromU8a' does not exist on type 'T'.", T extends Base in def? | ||
@@ -60,0 +62,0 @@ const decoded = new Type().fromU8a(u8a.subarray(offset)); |
@@ -16,5 +16,5 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
// `reduce` is exposed on the interface. | ||
export default class Vector < | ||
export default class Vector< | ||
T extends Base | ||
> extends Base<Array<T>> { | ||
> extends Base<Array<T>> { | ||
private _Type: { new(value?: any): T }; | ||
@@ -30,3 +30,3 @@ | ||
static decode <T> (Type: { new(value?: any): T }, value: Uint8Array | string | Array<any>): Array<T> { | ||
static decode<T> (Type: { new(value?: any): T }, value: Uint8Array | string | Array<any>): Array<T> { | ||
if (Array.isArray(value)) { | ||
@@ -48,2 +48,4 @@ return value.map((entry) => | ||
for (let index = 0; index < length; index++) { | ||
// FIXME replace by | ||
// const decoded = new Type(u8a.subarray(offset)); | ||
// @ts-ignore Not sure why we get "Property 'fromU8a' does not exist on type 'T'.", T extends Base in def? | ||
@@ -59,3 +61,3 @@ const decoded = new Type().fromU8a(u8a.subarray(offset)); | ||
static with <O extends Base> (Type: { new(value?: any): O }): { new(value?: any): Vector<O> } { | ||
static with<O extends Base> (Type: { new(value?: any): O }): { new(value?: any): Vector<O> } { | ||
return class extends Vector<O> { | ||
@@ -113,3 +115,3 @@ constructor (value?: Array<any>) { | ||
map <O> (fn: (item: T, index?: number) => O): Array<O> { | ||
map<O> (fn: (item: T, index?: number) => O): Array<O> { | ||
return this.raw.map(fn); | ||
@@ -122,3 +124,3 @@ } | ||
reduce <O> (fn: (result: O, item: T, index?: number) => O, initial: O): O { | ||
reduce<O> (fn: (result: O, item: T, index?: number) => O, initial: O): O { | ||
return this.raw.reduce(fn, initial); | ||
@@ -125,0 +127,0 @@ } |
@@ -15,3 +15,3 @@ import { AnyU8a } from './types'; | ||
constructor(index: Method | AnyU8a, meta?: FunctionMetadata, args?: Array<any>); | ||
static decode(meta: FunctionMetadata, data: Uint8Array): Array<Base>; | ||
static decodeMethod(meta: FunctionMetadata, data: Uint8Array): Array<Base>; | ||
static encode(meta: FunctionMetadata, args: Array<any>): Uint8Array; | ||
@@ -18,0 +18,0 @@ static filterOrigin(meta?: FunctionMetadata): Array<FunctionArgumentMetadata>; |
@@ -51,3 +51,3 @@ "use strict"; | ||
static decode(meta, data) { | ||
static decodeMethod(meta, data) { | ||
let offset = 0; | ||
@@ -115,3 +115,3 @@ return Method.filterOrigin(meta).map(({ | ||
this._meta = Method.findFunction(this.callIndex).meta; | ||
this._args = Method.decode(this._meta, input.subarray(startData)); | ||
this._args = Method.decodeMethod(this._meta, input.subarray(startData)); | ||
@@ -118,0 +118,0 @@ const argsLength = this._args.reduce((length, arg) => length + arg.byteLength(), 0); |
@@ -45,3 +45,3 @@ // Copyright 2017-2018 @polkadot/extrinsics authors & contributors | ||
static decode (meta: FunctionMetadata, data: Uint8Array): Array<Base> { | ||
static decodeMethod (meta: FunctionMetadata, data: Uint8Array): Array<Base> { | ||
let offset = 0; | ||
@@ -119,3 +119,3 @@ | ||
this._meta = Method.findFunction(this.callIndex).meta; | ||
this._args = Method.decode(this._meta, input.subarray(startData)); | ||
this._args = Method.decodeMethod(this._meta, input.subarray(startData)); | ||
@@ -122,0 +122,0 @@ const argsLength = this._args.reduce((length, arg) => length + arg.byteLength(), 0); |
{ | ||
"name": "@polkadot/types", | ||
"version": "0.30.41", | ||
"version": "0.30.42", | ||
"description": "Implementation of the Parity codec", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,4 +1,6 @@ | ||
import { AnyU8a } from '@polkadot/types/types'; | ||
import Base from './codec/Base'; | ||
export default class Text extends Base<string> { | ||
constructor(value?: Text | string); | ||
constructor(value?: Text | string | AnyU8a); | ||
static decodeText(input: any): string; | ||
readonly length: number; | ||
@@ -5,0 +7,0 @@ byteLength(): number; |
37
Text.js
@@ -16,2 +16,6 @@ "use strict"; | ||
var _function = _interopRequireDefault(require("@polkadot/util/is/function")); | ||
var _string = _interopRequireDefault(require("@polkadot/util/is/string")); | ||
var _fromUtf = _interopRequireDefault(require("@polkadot/util/u8a/fromUtf8")); | ||
@@ -27,2 +31,4 @@ | ||
var _U8a = _interopRequireDefault(require("./codec/U8a")); | ||
// Copyright 2017-2018 @polkadot/types authors & contributors | ||
@@ -41,5 +47,26 @@ // This software may be modified and distributed under the terms | ||
constructor(value = '') { | ||
super(value instanceof Text ? value.raw : value.trim()); | ||
super(Text.decodeText(value)); | ||
} | ||
static decodeText(input) { | ||
if ((0, _string.default)(input)) { | ||
return input; | ||
} else if (input instanceof Text) { | ||
return input.raw; | ||
} else if (input instanceof Uint8Array) { | ||
const _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]; | ||
return (0, _toUtf.default)(input.subarray(offset, offset + length.toNumber())); | ||
} else if (input instanceof _U8a.default) { | ||
return Text.decodeText(input.raw); | ||
} else if ((0, _function.default)(input.toString)) { | ||
return input.toString(); | ||
} else { | ||
return `${input}`; | ||
} | ||
} | ||
get length() { | ||
@@ -54,6 +81,6 @@ return this.raw.length; | ||
fromU8a(input) { | ||
const _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 _Compact$decodeU8a3 = _Compact.default.decodeU8a(input, _Compact.DEFAULT_LENGTH_BITS), | ||
_Compact$decodeU8a4 = (0, _slicedToArray2.default)(_Compact$decodeU8a3, 2), | ||
offset = _Compact$decodeU8a4[0], | ||
length = _Compact$decodeU8a4[1]; | ||
@@ -60,0 +87,0 @@ this.raw = (0, _toUtf.default)(input.subarray(offset, offset + length.toNumber())); |
27
Text.ts
@@ -5,2 +5,4 @@ // Copyright 2017-2018 @polkadot/types authors & contributors | ||
import isFunction from '@polkadot/util/is/function'; | ||
import isString from '@polkadot/util/is/string'; | ||
import u8aFromUtf8 from '@polkadot/util/u8a/fromUtf8'; | ||
@@ -10,4 +12,6 @@ import u8aToUtf8 from '@polkadot/util/u8a/toUtf8'; | ||
import { AnyU8a } from '@polkadot/types/types'; | ||
import Base from './codec/Base'; | ||
import Compact, { DEFAULT_LENGTH_BITS } from './codec/Compact'; | ||
import U8a from './codec/U8a'; | ||
@@ -23,10 +27,25 @@ // This is a string wrapper, along with the length. It is used both for strings as well | ||
export default class Text extends Base<string> { | ||
constructor (value: Text | string = '') { | ||
constructor (value: Text | string | AnyU8a = '') { | ||
super( | ||
value instanceof Text | ||
? value.raw | ||
: value.trim() | ||
Text.decodeText(value) | ||
); | ||
} | ||
static decodeText (input: any): string { | ||
if (isString(input)) { | ||
return input; | ||
} else if (input instanceof Text) { | ||
return input.raw; | ||
} else if (input instanceof Uint8Array) { | ||
const [offset, length] = Compact.decodeU8a(input, DEFAULT_LENGTH_BITS); | ||
return u8aToUtf8(input.subarray(offset, offset + length.toNumber())); | ||
} else if (input instanceof U8a) { | ||
return Text.decodeText(input.raw); | ||
} else if (isFunction(input.toString)) { | ||
return input.toString(); | ||
} else { | ||
return `${input}`; | ||
} | ||
} | ||
get length (): number { | ||
@@ -33,0 +52,0 @@ return this.raw.length; |
464414
261
11443