Internal adjustments (part 2) (#4134)

This commit is contained in:
Jaco
2021-11-01 09:27:04 +02:00
committed by GitHub
parent 6a5f1bbbf4
commit aef4d07c48
4 changed files with 50 additions and 29 deletions
+25 -14
View File
@@ -98,18 +98,26 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public toHuman (isExtended?: boolean): AnyJson {
return this.map((entry): AnyJson =>
entry.toHuman(isExtended)
);
const result = new Array<AnyJson>(this.length);
for (let i = 0; i < this.length; i++) {
result[i] = this[i].toHuman(isExtended);
}
return result;
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): AnyJson {
return this.map((entry): AnyJson =>
entry.toJSON()
);
const result = new Array<AnyJson>(this.length);
for (let i = 0; i < this.length; i++) {
result[i] = this[i].toJSON();
}
return result;
}
/**
@@ -121,12 +129,13 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
* @description Returns the string representation of the value
*/
public override toString (): string {
// Overwrite the default toString representation of Array.
const data = this.map((entry): string =>
entry.toString()
);
const result = new Array<string>(this.length);
return `[${data.join(', ')}]`;
for (let i = 0; i < this.length; i++) {
result[i] = this[i].toString();
}
return `[${result.join(', ')}]`;
}
/**
@@ -134,9 +143,11 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public toU8a (isBare?: boolean): Uint8Array {
const encoded = this.map((entry): Uint8Array =>
entry.toU8a(isBare)
);
const encoded = new Array<Uint8Array>(this.length);
for (let i = 0; i < this.length; i++) {
encoded[i] = this[i].toU8a(isBare);
}
return isBare
? u8aConcat(...encoded)
+13 -9
View File
@@ -303,18 +303,22 @@ export class Struct<
public toU8a (isBare?: BareOpts): Uint8Array {
// we have keyof S here, cast to string to make it compatible with isBare
const entries = [...this.entries()] as [string, Codec][];
const encoded: Uint8Array[] = [];
return u8aConcat(
...entries
// eslint-disable-next-line @typescript-eslint/unbound-method
.filter(([, value]) => isFunction(value?.toU8a))
.map(([key, value]): Uint8Array =>
value.toU8a(
for (let i = 0; i < entries.length; i++) {
const [k, v] = entries[i];
if (v && isFunction(v.toU8a)) {
encoded.push(
v.toU8a(
!isBare || isBoolean(isBare)
? isBare
: isBare[key]
: isBare[k]
)
)
);
);
}
}
return u8aConcat(...encoded);
}
}
+7 -5
View File
@@ -122,10 +122,12 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public override toU8a (isBare?: boolean): Uint8Array {
return u8aConcat(
...this.map((e): Uint8Array =>
e.toU8a(isBare)
)
);
const encoded = new Array<Uint8Array>(this.length);
for (let i = 0; i < this.length; i++) {
encoded[i] = this[i].toU8a(isBare);
}
return u8aConcat(...encoded);
}
}
+5 -1
View File
@@ -65,7 +65,11 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
public override toU8a (): Uint8Array {
// we override, we don't add the length prefix for ourselves, and at the same time we
// ignore isBare on entries, since they should be properly encoded at all times
const encoded = this.map((e) => e.toU8a());
const encoded = new Array<Uint8Array>(this.length);
for (let i = 0; i < this.length; i++) {
encoded[i] = this[i].toU8a();
}
return encoded.length
? u8aConcat(...encoded)