Replace byteLength() by get encodedLength() (#313)

* Replace byteLength by encodedLength

* Replace (continued)
This commit is contained in:
Amaury Martiny
2018-10-18 10:23:54 +02:00
committed by GitHub
parent 90cfd343eb
commit 008c78fe42
20 changed files with 33 additions and 33 deletions
+2 -2
View File
@@ -61,8 +61,8 @@ export default class Address extends Base<AccountId | AccountIndex> {
);
}
byteLength (): number {
return this.raw.byteLength() + (
get encodedLength (): number {
return this.raw.encodedLength + (
this.raw instanceof AccountIndex
? 0
: 1
+1 -1
View File
@@ -22,7 +22,7 @@ export default class Bool extends Base<boolean> {
return !!value;
}
byteLength (): number {
get encodedLength (): number {
return 1;
}
+1 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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;
+2 -2
View File
@@ -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
);
}
+1 -1
View File
@@ -44,7 +44,7 @@ export default class Moment extends Base<Date> {
);
}
byteLength (): number {
get encodedLength (): number {
return BITLENGTH / 8;
}
+1 -1
View File
@@ -10,7 +10,7 @@ export default class Null extends Base<null> {
super(null);
}
byteLength (): number {
get encodedLength (): number {
return 0;
}
+1 -1
View File
@@ -49,7 +49,7 @@ export default class Text extends Base<string> {
return this.raw.length;
}
byteLength (): number {
get encodedLength (): number {
return this.length + Compact.encodeU8a(this.length, DEFAULT_LENGTH_BITS).length;
}
+1 -1
View File
@@ -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]()
+2 -2
View File
@@ -22,8 +22,8 @@ export default class Base<T = any> {
this.raw = value;
}
byteLength (): number {
throw new Error('Base::byteLength: unimplemented');
get encodedLength (): number {
throw new Error('Base::encodedLength: unimplemented');
}
toJSON (): any {
+4 -4
View File
@@ -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);
});
+1 -1
View File
@@ -98,7 +98,7 @@ export default class Compact extends UInt {
);
}
byteLength (): number {
get encodedLength (): number {
return this.toU8a().length;
}
+1 -1
View File
@@ -38,7 +38,7 @@ export default class Enum extends Base<number> {
}
}
byteLength (): number {
get encodedLength (): number {
return 1;
}
+2 -2
View File
@@ -86,8 +86,8 @@ export default class EnumType<T> extends Base<Base<T>> {
return this.raw;
}
byteLength (): number {
return 1 + this.raw.byteLength();
get encodedLength (): number {
return 1 + this.raw.encodedLength;
}
setValue (index?: | EnumType<T> | number, value?: any): void {
+3 -3
View File
@@ -59,10 +59,10 @@ export default class Option<T> extends Base<Base<T>> {
: 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<T> extends Base<Base<T>> {
return this.raw.toU8a(true);
}
const u8a = new Uint8Array(this.byteLength());
const u8a = new Uint8Array(this.encodedLength);
if (!this._isEmpty) {
u8a.set([1]);
+3 -3
View File
@@ -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);
}
+1 -1
View File
@@ -34,7 +34,7 @@ export default class U8a extends Base<Uint8Array> {
return this.raw.length;
}
byteLength (): number {
get encodedLength (): number {
return this.length;
}
+1 -1
View File
@@ -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();
}
}
+1 -1
View File
@@ -54,7 +54,7 @@ export default class UInt extends Base<BN> {
return bnToBn(value);
}
byteLength (): number {
get encodedLength (): number {
return this._bitLength / 8;
}
+3 -3
View File
@@ -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);
}