Define properties on Struct.with automatically

This commit is contained in:
Jaco Greeff
2018-11-29 16:10:50 +01:00
parent 99c7052102
commit d348f50dcf
2 changed files with 53 additions and 31 deletions
+22 -7
View File
@@ -8,6 +8,8 @@ import Text from '../Text';
import U32 from '../U32'; import U32 from '../U32';
import Vector from './Vector'; import Vector from './Vector';
describe('Struct', () => {
describe('decoding', () => {
const testDecode = (type: string, input: any) => const testDecode = (type: string, input: any) =>
it(`can decode from ${type}`, () => { it(`can decode from ${type}`, () => {
const s = new Struct({ const s = new Struct({
@@ -22,6 +24,13 @@ const testDecode = (type: string, input: any) =>
).toEqual(['bazzing', '69']); ).toEqual(['bazzing', '69']);
}); });
testDecode('array', ['bazzing', 69]);
testDecode('hex', '0x1c62617a7a696e6745000000');
testDecode('object', { foo: 'bazzing', bar: 69 });
testDecode('Uint8Array', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0]));
});
describe('encoding', () => {
const testEncode = (to: CodecTo, expected: any) => const testEncode = (to: CodecTo, expected: any) =>
it(`can encode ${to}`, () => { it(`can encode ${to}`, () => {
const s = new Struct({ const s = new Struct({
@@ -31,17 +40,11 @@ const testEncode = (to: CodecTo, expected: any) =>
expect(s[to]()).toEqual(expected); expect(s[to]()).toEqual(expected);
}); });
describe('Struct', () => {
testDecode('array', ['bazzing', 69]);
testDecode('hex', '0x1c62617a7a696e6745000000');
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'); testEncode('toHex', '0x1c62617a7a696e6745000000');
testEncode('toJSON', { foo: 'bazzing', bar: 69 }); testEncode('toJSON', { foo: 'bazzing', bar: 69 });
testEncode('toU8a', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0])); testEncode('toU8a', Uint8Array.from([28, 98, 97, 122, 122, 105, 110, 103, 69, 0, 0, 0]));
testEncode('toString', '{"foo":"bazzing","bar":69}'); testEncode('toString', '{"foo":"bazzing","bar":69}');
});
it('decodes null', () => { it('decodes null', () => {
expect( expect(
@@ -85,6 +88,18 @@ describe('Struct', () => {
).toEqual('{"txt":"foo","u32":1193046}'); ).toEqual('{"txt":"foo","u32":1193046}');
}); });
it('exposes the properties on the object', () => {
const struct = new (
Struct.with({
txt: Text,
u32: U32
})
)({ txt: 'foo', u32: 0x123456 });
expect((struct as any).txt.toString()).toEqual('foo');
expect((struct as any).u32.toNumber()).toEqual(0x123456);
});
it('correctly encodes length', () => { it('correctly encodes length', () => {
expect( expect(
new ( new (
+7
View File
@@ -111,6 +111,13 @@ export default class Struct<
return class extends Struct<S> { return class extends Struct<S> {
constructor (value?: any, jsonMap?: Map<keyof S, string>) { constructor (value?: any, jsonMap?: Map<keyof S, string>) {
super(Types, value, jsonMap); super(Types, value, jsonMap);
(Object.keys(Types) as Array<keyof S>).forEach((key, index) => {
Object.defineProperty(this, key, {
enumerable: true,
get: () => this.getAtIndex(index)
});
});
} }
}; };
} }