From 008c78fe423c142c5fc11b69ffbac26875b4181e Mon Sep 17 00:00:00 2001 From: Amaury Martiny Date: Thu, 18 Oct 2018 10:23:54 +0200 Subject: [PATCH] Replace `byteLength()` by `get encodedLength()` (#313) * Replace byteLength by encodedLength * Replace (continued) --- packages/types/src/Address.ts | 4 ++-- packages/types/src/Bool.ts | 2 +- packages/types/src/Bytes.ts | 2 +- packages/types/src/Extrinsic.ts | 2 +- packages/types/src/ExtrinsicSignature.ts | 4 ++-- packages/types/src/Moment.ts | 2 +- packages/types/src/Null.ts | 2 +- packages/types/src/Text.ts | 2 +- packages/types/src/codec/Base.spec.js | 2 +- packages/types/src/codec/Base.ts | 4 ++-- packages/types/src/codec/Compact.spec.js | 8 ++++---- packages/types/src/codec/Compact.ts | 2 +- packages/types/src/codec/Enum.ts | 2 +- packages/types/src/codec/EnumType.ts | 4 ++-- packages/types/src/codec/Option.ts | 6 +++--- packages/types/src/codec/Struct.ts | 6 +++--- packages/types/src/codec/U8a.ts | 2 +- packages/types/src/codec/U8aFixed.ts | 2 +- packages/types/src/codec/UInt.ts | 2 +- packages/types/src/codec/Vector.ts | 6 +++--- 20 files changed, 33 insertions(+), 33 deletions(-) diff --git a/packages/types/src/Address.ts b/packages/types/src/Address.ts index 721daf6742..fa36c8a4a1 100644 --- a/packages/types/src/Address.ts +++ b/packages/types/src/Address.ts @@ -61,8 +61,8 @@ export default class Address extends Base { ); } - byteLength (): number { - return this.raw.byteLength() + ( + get encodedLength (): number { + return this.raw.encodedLength + ( this.raw instanceof AccountIndex ? 0 : 1 diff --git a/packages/types/src/Bool.ts b/packages/types/src/Bool.ts index 6ca356d4be..96c4dde549 100644 --- a/packages/types/src/Bool.ts +++ b/packages/types/src/Bool.ts @@ -22,7 +22,7 @@ export default class Bool extends Base { return !!value; } - byteLength (): number { + get encodedLength (): number { return 1; } diff --git a/packages/types/src/Bytes.ts b/packages/types/src/Bytes.ts index 543522f880..eef258a3c0 100644 --- a/packages/types/src/Bytes.ts +++ b/packages/types/src/Bytes.ts @@ -46,7 +46,7 @@ export default class Bytes extends U8a { return this.raw.length; } - byteLength (): number { + get encodedLength (): number { return this.length + Compact.encodeU8a(this.length, DEFAULT_LENGTH_BITS).length; } diff --git a/packages/types/src/Extrinsic.ts b/packages/types/src/Extrinsic.ts index bb6548e16f..48c09d2faf 100644 --- a/packages/types/src/Extrinsic.ts +++ b/packages/types/src/Extrinsic.ts @@ -105,7 +105,7 @@ export default class Extrinsic extends Struct { return this.raw.signature as ExtrinsicSignature; } - byteLength (): number { + get encodedLength (): number { const length = this.length; return length + Compact.encodeU8a(length, DEFAULT_LENGTH_BITS).length; diff --git a/packages/types/src/ExtrinsicSignature.ts b/packages/types/src/ExtrinsicSignature.ts index 301dcce417..17b87b43a8 100644 --- a/packages/types/src/ExtrinsicSignature.ts +++ b/packages/types/src/ExtrinsicSignature.ts @@ -61,11 +61,11 @@ export default class ExtrinsicSignature extends Struct { return value as any; } - byteLength (): number { + get encodedLength (): number { // version has 1 byte, signature takes the rest return 1 + ( this.isSigned - ? super.byteLength() + ? super.encodedLength : 0 ); } diff --git a/packages/types/src/Moment.ts b/packages/types/src/Moment.ts index 94e649d1b0..e5d23584f3 100644 --- a/packages/types/src/Moment.ts +++ b/packages/types/src/Moment.ts @@ -44,7 +44,7 @@ export default class Moment extends Base { ); } - byteLength (): number { + get encodedLength (): number { return BITLENGTH / 8; } diff --git a/packages/types/src/Null.ts b/packages/types/src/Null.ts index 3d38bb0fec..51a603764a 100644 --- a/packages/types/src/Null.ts +++ b/packages/types/src/Null.ts @@ -10,7 +10,7 @@ export default class Null extends Base { super(null); } - byteLength (): number { + get encodedLength (): number { return 0; } diff --git a/packages/types/src/Text.ts b/packages/types/src/Text.ts index 5b00b45864..eb09c22097 100644 --- a/packages/types/src/Text.ts +++ b/packages/types/src/Text.ts @@ -49,7 +49,7 @@ export default class Text extends Base { return this.raw.length; } - byteLength (): number { + get encodedLength (): number { return this.length + Compact.encodeU8a(this.length, DEFAULT_LENGTH_BITS).length; } diff --git a/packages/types/src/codec/Base.spec.js b/packages/types/src/codec/Base.spec.js index 870e49be03..f2c41339c9 100644 --- a/packages/types/src/codec/Base.spec.js +++ b/packages/types/src/codec/Base.spec.js @@ -9,7 +9,7 @@ describe('Base', () => { expect(new Base('foo-bar').raw).toEqual('foo-bar'); }); - ['byteLength', 'toJSON', 'toString', 'toU8a'].forEach((method) => { + ['encodedLength', 'toJSON', 'toString', 'toU8a'].forEach((method) => { it(`has abstract (?) impl for ${method}`, () => { expect( () => new Base()[method]() diff --git a/packages/types/src/codec/Base.ts b/packages/types/src/codec/Base.ts index a943e6f3aa..0a2de7363f 100644 --- a/packages/types/src/codec/Base.ts +++ b/packages/types/src/codec/Base.ts @@ -22,8 +22,8 @@ export default class Base { this.raw = value; } - byteLength (): number { - throw new Error('Base::byteLength: unimplemented'); + get encodedLength (): number { + throw new Error('Base::encodedLength: unimplemented'); } toJSON (): any { diff --git a/packages/types/src/codec/Compact.spec.js b/packages/types/src/codec/Compact.spec.js index adc204f522..b6f09157db 100644 --- a/packages/types/src/codec/Compact.spec.js +++ b/packages/types/src/codec/Compact.spec.js @@ -90,15 +90,15 @@ describe('Compact', () => { }); }); - it('has the correct byteLength for constructor values (default)', () => { + it('has the correct encodedLength for constructor values (default)', () => { expect( - new Compact(0xfffffff9, 64).byteLength() + new Compact(0xfffffff9, 64).encodedLength ).toEqual(9); }); - it('has the correct byteLength for constructor values (u32)', () => { + it('has the correct encodedLength for constructor values (u32)', () => { expect( - new Compact(0xfffffff9, 32).byteLength() + new Compact(0xfffffff9, 32).encodedLength ).toEqual(5); }); diff --git a/packages/types/src/codec/Compact.ts b/packages/types/src/codec/Compact.ts index f5ca1b8d0a..7d592b01dd 100644 --- a/packages/types/src/codec/Compact.ts +++ b/packages/types/src/codec/Compact.ts @@ -98,7 +98,7 @@ export default class Compact extends UInt { ); } - byteLength (): number { + get encodedLength (): number { return this.toU8a().length; } diff --git a/packages/types/src/codec/Enum.ts b/packages/types/src/codec/Enum.ts index ba132ea209..b28b7cd65b 100644 --- a/packages/types/src/codec/Enum.ts +++ b/packages/types/src/codec/Enum.ts @@ -38,7 +38,7 @@ export default class Enum extends Base { } } - byteLength (): number { + get encodedLength (): number { return 1; } diff --git a/packages/types/src/codec/EnumType.ts b/packages/types/src/codec/EnumType.ts index b437a6aa8f..9a25f4cbfa 100644 --- a/packages/types/src/codec/EnumType.ts +++ b/packages/types/src/codec/EnumType.ts @@ -86,8 +86,8 @@ export default class EnumType extends Base> { return this.raw; } - byteLength (): number { - return 1 + this.raw.byteLength(); + get encodedLength (): number { + return 1 + this.raw.encodedLength; } setValue (index?: | EnumType | number, value?: any): void { diff --git a/packages/types/src/codec/Option.ts b/packages/types/src/codec/Option.ts index 83b30b8a7e..2578139611 100644 --- a/packages/types/src/codec/Option.ts +++ b/packages/types/src/codec/Option.ts @@ -59,10 +59,10 @@ export default class Option extends Base> { : this.raw.raw; } - byteLength (): number { + get encodedLength (): number { const childLength = this._isEmpty ? 0 - : this.raw.byteLength(); + : this.raw.encodedLength; return 1 + childLength; } @@ -78,7 +78,7 @@ export default class Option extends Base> { return this.raw.toU8a(true); } - const u8a = new Uint8Array(this.byteLength()); + const u8a = new Uint8Array(this.encodedLength); if (!this._isEmpty) { u8a.set([1]); diff --git a/packages/types/src/codec/Struct.ts b/packages/types/src/codec/Struct.ts index 8231689811..d4d966e6a9 100644 --- a/packages/types/src/codec/Struct.ts +++ b/packages/types/src/codec/Struct.ts @@ -75,7 +75,7 @@ export default class Struct< // Move the currentIndex forward // @ts-ignore FIXME See below - currentIndex += raw[key].byteLength(); + currentIndex += raw[key].encodedLength; // @ts-ignore FIXME See below } else if (value[jsonKey] instanceof Types[key]) { // @ts-ignore FIXME See below @@ -118,9 +118,9 @@ export default class Struct< return this._Types; } - byteLength (): number { + get encodedLength (): number { return Object.values(this.raw).reduce((length, entry) => { - return length += entry.byteLength(); + return length += entry.encodedLength; }, 0); } diff --git a/packages/types/src/codec/U8a.ts b/packages/types/src/codec/U8a.ts index e91eee0e3f..c3fcbd8d76 100644 --- a/packages/types/src/codec/U8a.ts +++ b/packages/types/src/codec/U8a.ts @@ -34,7 +34,7 @@ export default class U8a extends Base { return this.raw.length; } - byteLength (): number { + get encodedLength (): number { return this.length; } diff --git a/packages/types/src/codec/U8aFixed.ts b/packages/types/src/codec/U8aFixed.ts index f01ce4b491..d40ce5627b 100644 --- a/packages/types/src/codec/U8aFixed.ts +++ b/packages/types/src/codec/U8aFixed.ts @@ -29,7 +29,7 @@ export default class U8aFixed extends U8a { this.raw = this.raw.subarray(0, this._trimLength()); } - byteLength (): number { + get encodedLength (): number { return this._trimLength(); } } diff --git a/packages/types/src/codec/UInt.ts b/packages/types/src/codec/UInt.ts index 025e6c895d..6d1ceb2cfe 100644 --- a/packages/types/src/codec/UInt.ts +++ b/packages/types/src/codec/UInt.ts @@ -54,7 +54,7 @@ export default class UInt extends Base { return bnToBn(value); } - byteLength (): number { + get encodedLength (): number { return this._bitLength / 8; } diff --git a/packages/types/src/codec/Vector.ts b/packages/types/src/codec/Vector.ts index e0a30cd4b6..04eebbc6b5 100644 --- a/packages/types/src/codec/Vector.ts +++ b/packages/types/src/codec/Vector.ts @@ -47,7 +47,7 @@ export default class Vector< const decoded = new Type(u8a.subarray(offset)); result.push(decoded); - offset += decoded.byteLength(); + offset += decoded.encodedLength; } return result; @@ -69,9 +69,9 @@ export default class Vector< return this.raw.length; } - byteLength (): number { + get encodedLength (): number { return this.raw.reduce((total, raw) => { - return total + raw.byteLength(); + return total + raw.encodedLength; }, Compact.encodeU8a(this.length, DEFAULT_LENGTH_BITS).length); }