Cleanup assignments (#2811)

* Cleanup assignments

* Small adjustments

* Cleanup AnyJson results

* Event toHuman
This commit is contained in:
Jaco Greeff
2020-10-31 12:50:44 +01:00
committed by GitHub
parent 5be8ef8f0d
commit 94a4594d32
16 changed files with 70 additions and 108 deletions
+4 -10
View File
@@ -152,27 +152,21 @@ export default class Enum implements Codec {
const askey = `as${name}`;
const iskey = `is${name}`;
// do not clobber existing properties on the object
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (isUndefined((this as any)[iskey])) {
isUndefined(this[iskey as keyof this]) &&
Object.defineProperty(this, iskey, {
enumerable: true,
get: (): boolean => this.type === _key
get: () => this.type === _key
});
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (isUndefined((this as any)[askey])) {
isUndefined(this[askey as keyof this]) &&
Object.defineProperty(this, askey, {
enumerable: true,
get: (): Codec => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
assert((this as any)[iskey], `Cannot convert '${this.type}' via ${askey}`);
assert(this[iskey as keyof this], `Cannot convert '${this.type}' via ${askey}`);
return this.value;
}
});
}
});
}
};
+7 -13
View File
@@ -31,18 +31,12 @@ export default class StructAny extends Map<string, any> implements Codec {
this.registry = registry;
// like we are doing with structs, add the keys as getters
decoded.forEach(([key]): void => {
// do not clobber existing properties on the object
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (!isUndefined((this as any)[key])) {
return;
}
Object.defineProperty(this, key, {
enumerable: true,
get: (): Codec | undefined => this.get(key) as Codec
});
isUndefined(this[key as keyof this]) &&
Object.defineProperty(this, key, {
enumerable: true,
get: (): Codec | undefined => this.get(key) as Codec
});
});
}
@@ -84,14 +78,14 @@ export default class StructAny extends Map<string, any> implements Codec {
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public toHuman (): AnyJson {
public toHuman (): Record<string, AnyJson> {
return this.toJSON();
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): AnyJson {
public toJSON (): Record<string, AnyJson> {
return [...this.entries()].reduce((json: Record<string, AnyJson>, [key, value]): Record<string, AnyJson> => {
json[key] = value as AnyJson;
+4 -4
View File
@@ -152,8 +152,8 @@ export default class CodecMap<K extends Codec = Codec, V extends Codec = Codec>
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public toHuman (isExtended?: boolean): AnyJson {
const json: AnyJson = {};
public toHuman (isExtended?: boolean): Record<string, AnyJson> {
const json: Record<string, AnyJson> = {};
this.forEach((v: V, k: K) => {
json[k.toString()] = v.toHuman(isExtended);
@@ -165,8 +165,8 @@ export default class CodecMap<K extends Codec = Codec, V extends Codec = Codec>
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): AnyJson {
const json: AnyJson = {};
public toJSON (): Record<string, AnyJson> {
const json: Record<string, AnyJson> = {};
this.forEach((v: V, k: K) => {
json[k.toString()] = v.toJSON();
+1 -4
View File
@@ -100,14 +100,11 @@ export default class CodecSet extends Set<string> implements Codec {
const name = stringUpperFirst(stringCamelCase(_key));
const iskey = `is${name}`;
// do not clobber existing properties on the object
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (isUndefined((this as any)[iskey])) {
isUndefined(this[iskey as keyof this]) &&
Object.defineProperty(this, iskey, {
enumerable: true,
get: (): boolean => this.strings.includes(_key)
});
}
});
}
};
+21 -28
View File
@@ -155,22 +155,25 @@ export default class Struct<
constructor (registry: Registry, value?: unknown) {
super(registry, Types, value as string, jsonMap);
(Object.keys(Types) as (keyof S)[]).forEach((key): void => {
// do not clobber existing properties on the object
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (!isUndefined((this as any)[key])) {
return;
}
Object.defineProperty(this, key, {
enumerable: true,
get: (): Codec | undefined => this.get(key)
});
Object.keys(Types).forEach((key): void => {
isUndefined(this[key as keyof this]) &&
Object.defineProperty(this, key, {
enumerable: true,
get: (): Codec | undefined => this.get(key as keyof S)
});
});
}
};
}
public static typesToMap (registry: Registry, Types: Record<string, Constructor>): Record<string, string> {
return Object.entries(Types).reduce((result: Record<string, string>, [key, Type]): Record<string, string> => {
result[key] = registry.getClassName(Type) || new Type(registry).toRawType();
return result;
}, {});
}
/**
* @description The available keys for this enum
*/
@@ -264,37 +267,27 @@ export default class Struct<
/**
* @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.keys()].reduce((json, key): Record<keyof S, AnyJson> => {
public toHuman (isExtended?: boolean): Record<string, AnyJson> {
return [...this.keys()].reduce((json: Record<string, AnyJson>, key): Record<string, AnyJson> => {
const value = this.get(key);
json[key] = value && value.toHuman(isExtended);
json[key as string] = value && value.toHuman(isExtended);
return json;
}, {} as Record<keyof S, AnyJson>);
}, {});
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): AnyJson {
// FIXME the return type string is only used by Extrinsic (extends Struct),
// but its toJSON is the hex value
return [...this.keys()].reduce((json, key): Record<keyof S, AnyJson> => {
public toJSON (): Record<string, AnyJson> {
return [...this.keys()].reduce((json: Record<string, AnyJson>, key): Record<string, AnyJson> => {
const jsonKey = this.#jsonMap.get(key) || key;
const value = this.get(key);
json[jsonKey] = value && value.toJSON();
json[jsonKey as string] = value && value.toJSON();
return json;
}, {} as Record<keyof S, AnyJson>);
}
public static typesToMap (registry: Registry, Types: Record<string, Constructor>): Record<string, string> {
return Object.entries(Types).reduce((result: Record<string, string>, [key, Type]): Record<string, string> => {
result[key] = registry.getClassName(Type) || new Type(registry).toRawType();
return result;
}, {});
}
+3 -3
View File
@@ -67,12 +67,12 @@ export class MortalEra extends Tuple {
/** @internal */
private static _decodeMortalEra (registry: Registry, value?: MortalMethod | Uint8Array | number[] | string): MortalEraValue {
if (isU8a(value) || isHex(value) || Array.isArray(value)) {
if (!value) {
return [new U64(registry), new U64(registry)];
} else if (isU8a(value) || isHex(value) || Array.isArray(value)) {
return MortalEra._decodeMortalU8a(registry, u8aToU8a(value));
} else if (isObject(value)) {
return MortalEra._decodeMortalObject(registry, value);
} else if (!value) {
return [new U64(registry), new U64(registry)];
}
throw new Error('Invalid data passed to Mortal era');
+1 -1
View File
@@ -231,7 +231,7 @@ export default class Call extends Struct implements IMethod {
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public toHuman (isExpanded?: boolean): AnyJson {
public toHuman (isExpanded?: boolean): Record<string, AnyJson> {
let call: CallFunction | undefined;
try {
+11 -7
View File
@@ -84,9 +84,7 @@ export default class Event extends Struct {
/** @internal */
public static decodeEvent (registry: Registry, value: Uint8Array = new Uint8Array()): { DataType: Constructor<Null> | Constructor<EventData>; value?: { index: Uint8Array; data: Uint8Array } } {
if (!value.length) {
return {
DataType: Null
};
return { DataType: Null };
}
const index = value.subarray(0, 2);
@@ -145,9 +143,15 @@ export default class Event extends Struct {
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public toHuman (isExpanded?: boolean): AnyJson {
// FIXME May this human-friendly
return this.toJSON();
public toHuman (isExpanded?: boolean): Record<string, AnyJson> {
return {
method: this.method,
section: this.section,
...(isExpanded
? { documentation: this.meta.documentation.map((d) => d.toString()) }
: {}
),
...super.toHuman(isExpanded)
};
}
}
+1 -7
View File
@@ -5,15 +5,9 @@ import { Codec } from './codec';
import BN from 'bn.js';
// We cannot inline this into CodecArg, TS thrws up when building docs
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type AnyFunction = (...args: any[]) => any;
// eslint-disable-next-line no-use-before-define
export type AnyJson = string | number | boolean | null | undefined | AnyJsonArray | { [index: string]: AnyJson };
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface AnyJsonArray extends Array<AnyJson> {}
export type AnyJson = string | number | boolean | null | undefined | AnyJson[] | { [index: string]: AnyJson };
export type AnyNumber = BN | BigInt | Uint8Array | number | string;