Add Codec toPrimitive (#5080)

This commit is contained in:
Jaco
2022-07-18 14:10:46 +03:00
committed by GitHub
parent 434f969208
commit 91a04ad159
30 changed files with 250 additions and 14 deletions
@@ -140,6 +140,19 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
return result;
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): AnyJson {
const result = new Array<AnyJson>(this.length);
for (let i = 0; i < this.length; i++) {
result[i] = this[i] && this[i].toPrimitive();
}
return result;
}
/**
* @description Returns the base runtime type name for this instance
*/
@@ -83,6 +83,13 @@ export abstract class AbstractBase<T extends Codec> implements Codec {
return this.#raw.toJSON();
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): AnyJson {
return this.#raw.toPrimitive();
}
/**
* @description Returns the string representation of the value
*/
+9
View File
@@ -207,6 +207,15 @@ export abstract class AbstractInt extends BN implements INumber {
: this.toNumber();
}
/**
* @description Returns the value in a primitive form, either number when <= 52 bits, or string otherwise
*/
public toPrimitive (): number | string {
return super.bitLength() > MAX_NUMBER_BITS
? this.toString()
: this.toNumber();
}
/**
* @description Returns the base runtime type name for this instance
*/
+7
View File
@@ -171,6 +171,13 @@ export class Compact<T extends INumber> implements ICompact<T> {
return this.#raw.toNumber();
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): string | number {
return this.#raw.toPrimitive();
}
/**
* @description Returns the base runtime type name for this instance
*/
@@ -72,6 +72,10 @@ export class DoNotConstruct implements Codec {
throw this.#neverError;
}
toPrimitive (): AnyJson {
throw this.#neverError;
}
toRawType (): string {
throw this.#neverError;
}
+9
View File
@@ -414,6 +414,15 @@ export class Enum implements IEnum {
return this.index;
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): AnyJson {
return this.#isBasic
? this.type
: { [stringCamelCase(this.type)]: this.#raw.toPrimitive() };
}
/**
* @description Returns a raw struct representation of the enum types
*/
+7
View File
@@ -69,6 +69,13 @@ export class Null implements Codec {
return null;
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): null {
return null;
}
/**
* @description Returns the base runtime type name for this instance
*/
+9
View File
@@ -206,6 +206,15 @@ export class Option<T extends Codec> implements IOption<T> {
: this.#raw.toJSON();
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): AnyJson {
return this.isNone
? null
: this.#raw.toPrimitive();
}
/**
* @description Returns the base runtime type name for this instance
*/
@@ -193,6 +193,19 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
return `BTreeSet<${this.registry.getClassName(this.#ValClass) || new this.#ValClass(this.registry).toRawType()}>`;
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): AnyJson {
const json: AnyJson = [];
for (const v of this.values()) {
json.push(v.toPrimitive());
}
return json;
}
/**
* @description Returns the string representation of the value
*/
+17
View File
@@ -206,6 +206,23 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
return json;
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): AnyJson {
const json: Record<string, AnyJson> = {};
for (const [k, v] of this.entries()) {
json[
k instanceof Raw && k.isAscii
? k.toUtf8()
: k.toString()
] = v.toPrimitive();
}
return json;
}
/**
* @description Returns the base runtime type name for this instance
*/
@@ -92,6 +92,15 @@ export class WrapperKeepOpaque<T extends Codec> extends Bytes {
: super.toHuman();
}
/**
* @description Converts the value in a best-fit primitive form
*/
public override toPrimitive (): any {
return this.#decoded
? this.#decoded.toPrimitive()
: super.toPrimitive();
}
/**
* @description Returns the base runtime type name for this instance
*/
+7
View File
@@ -105,6 +105,13 @@ export class bool extends Boolean implements Codec {
return this.valueOf();
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): boolean {
return this.toJSON();
}
/**
* @description Returns the base runtime type name for this instance
*/
+7
View File
@@ -136,6 +136,13 @@ export class CodecDate extends Date implements INumber {
return Math.ceil(this.getTime() / 1000);
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): number {
return this.toNumber();
}
/**
* @description Returns the base runtime type name for this instance
*/
+7
View File
@@ -112,6 +112,13 @@ export class Float extends Number implements IFloat {
return this.valueOf();
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): number {
return this.toNumber();
}
/**
* @description Returns the base runtime type name for this instance
*/
+13
View File
@@ -109,6 +109,19 @@ export class Json extends Map<string, any> implements Codec {
}, {});
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): Record<string, AnyJson> {
return [...this.entries()].reduce<Record<string, AnyJson>>((json, [key, value]): Record<string, AnyJson> => {
json[key] = isFunction((value as Codec).toHuman)
? (value as Codec).toPrimitive()
: value as AnyJson;
return json;
}, {});
}
/**
* @description Returns the base runtime type name for this instance
*/
@@ -36,6 +36,7 @@ describe('Raw', (): void => {
testEncode('toJSON', '0x0102030405');
testEncode('toHex', '0x0102030405');
testEncode('toPrimitive', '0x0102030405');
testEncode('toString', '0x0102030405');
testEncode('toU8a', Uint8Array.from([1, 2, 3, 4, 5]));
@@ -79,6 +80,11 @@ describe('Raw', (): void => {
expect(new Raw(registry, '0x2021222324').isAscii).toBe(true);
});
it('has valid toPrimitive', (): void => {
expect(new Raw(registry, 'testing').toPrimitive()).toEqual('testing');
expect(new Raw(registry, '0xe4bda0e5a5bd').toPrimitive()).toEqual('0xe4bda0e5a5bd');
});
it('has valid toUtf8', (): void => {
expect(new Raw(registry, 'Приветствую, ми').toUtf8()).toEqual('Приветствую, ми');
expect(new Raw(registry, '0xe4bda0e5a5bd').toUtf8()).toEqual('你好');
+14 -7
View File
@@ -111,6 +111,20 @@ export class Raw extends Uint8Array implements IU8a {
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public toHuman (): AnyJson {
return this.toPrimitive();
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): string {
return this.toHex();
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): AnyJson {
if (this.isAscii) {
const text = this.toUtf8();
@@ -123,13 +137,6 @@ export class Raw extends Uint8Array implements IU8a {
return this.toJSON();
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): string {
return this.toHex();
}
/**
* @description Returns the base runtime type name for this instance
*/
+7
View File
@@ -229,6 +229,13 @@ export class CodecSet extends Set<string> implements ISet<string> {
return this.valueEncoded.toNumber();
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): string[] {
return this.toJSON();
}
/**
* @description Returns the base runtime type name for this instance
*/
+17
View File
@@ -293,6 +293,23 @@ export class Struct<
return json;
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): Record<string, AnyJson> {
const json: Record<string, AnyJson> = {};
for (const [k, v] of this.entries()) {
const jsonKey = this.#jsonMap.get(k) || k;
// We actually log inside the U8a decoding and use JSON.stringify(...), which
// means that the Vec may be partially populated (same applies to toHuman, same check)
json[jsonKey as string] = v && v.toPrimitive();
}
return json;
}
/**
* @description Returns the base runtime type name for this instance
*/
+7
View File
@@ -147,6 +147,13 @@ export class Text extends String implements IText {
return this.toString();
}
/**
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): string {
return this.toJSON();
}
/**
* @description Returns the base runtime type name for this instance
*/
+6 -1
View File
@@ -77,6 +77,11 @@ export interface Codec {
*/
toJSON (): AnyJson;
/**
* @description Converts the value in a best-fit primitive form
*/
toPrimitive (): AnyJson;
/**
* @description Returns the base runtime type name for this instance
*/
@@ -105,6 +110,6 @@ export interface CodecClass<T = Codec> {
new(registry: Registry, ...args: any[]): T;
}
export type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a';
export type CodecTo = 'toHex' | 'toJSON' | 'toPrimitive' | 'toString' | 'toU8a';
export type ArgsDef = Record<string, CodecClass | string>;
@@ -31,6 +31,7 @@ export interface INumber extends Codec {
toBigInt (): bigint;
toBn (): BN;
toNumber (): number;
toPrimitive (): string | number;
}
export interface IFloat extends Codec {