Add disableAscii option for toHuman, and toPrimitive (#5831)
* Add `allowAscii` option for toHuman, and toPrimitive * change allowAscii to disableAscii * add test * Ensure Struct passes down the value to Raw, and add disableAscii to other types * ensure CodecMap also has disableAscii * toPrimitive CodecMap disableAscii * add toPrimitive options
This commit is contained in:
@@ -123,12 +123,12 @@ 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 {
|
||||
public toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson {
|
||||
const count = this.length;
|
||||
const result = new Array<AnyJson>(count);
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
result[i] = this[i] && this[i].toHuman(isExtended);
|
||||
result[i] = this[i] && this[i].toHuman(isExtended, disableAscii);
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -153,12 +153,12 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
/**
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public toPrimitive (): AnyJson {
|
||||
public toPrimitive (disableAscii?: boolean): AnyJson {
|
||||
const count = this.length;
|
||||
const result = new Array<AnyJson>(count);
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
result[i] = this[i] && this[i].toPrimitive();
|
||||
result[i] = this[i] && this[i].toPrimitive(disableAscii);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
@@ -75,8 +75,8 @@ export abstract class AbstractBase<T extends Codec> implements Codec {
|
||||
/**
|
||||
* @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.#raw.toHuman(isExtended);
|
||||
public toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson {
|
||||
return this.#raw.toHuman(isExtended, disableAscii);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,8 +89,8 @@ export abstract class AbstractBase<T extends Codec> implements Codec {
|
||||
/**
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public toPrimitive (): AnyJson {
|
||||
return this.#raw.toPrimitive();
|
||||
public toPrimitive (disableAscii?: boolean): AnyJson {
|
||||
return this.#raw.toPrimitive(disableAscii);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -60,7 +60,7 @@ export abstract class AbstractObject<T extends ToString> implements CodecObject<
|
||||
/**
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
public abstract toHuman (isExtended?: boolean): AnyJson;
|
||||
public abstract toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson;
|
||||
|
||||
/**
|
||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||
@@ -70,7 +70,7 @@ export abstract class AbstractObject<T extends ToString> implements CodecObject<
|
||||
/**
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public abstract toPrimitive (): AnyJson;
|
||||
public abstract toPrimitive (disableAscii?: boolean): AnyJson;
|
||||
|
||||
/**
|
||||
* @description Returns the string representation of the value
|
||||
|
||||
@@ -143,8 +143,8 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
/**
|
||||
* @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.#raw.toHuman(isExtended);
|
||||
public toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson {
|
||||
return this.#raw.toHuman(isExtended, disableAscii);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,8 +164,8 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
/**
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public toPrimitive (): string | number {
|
||||
return this.#raw.toPrimitive();
|
||||
public toPrimitive (disableAscii?: boolean): string | number {
|
||||
return this.#raw.toPrimitive(disableAscii);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -374,10 +374,10 @@ export class Enum implements IEnum {
|
||||
/**
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
public toHuman (isExtended?: boolean): AnyJson {
|
||||
public toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson {
|
||||
return this.#isBasic || this.isNone
|
||||
? this.type
|
||||
: { [this.type]: this.#raw.toHuman(isExtended) };
|
||||
: { [this.type]: this.#raw.toHuman(isExtended, disableAscii) };
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -399,10 +399,10 @@ export class Enum implements IEnum {
|
||||
/**
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public toPrimitive (): AnyJson {
|
||||
public toPrimitive (disableAscii?: boolean): AnyJson {
|
||||
return this.#isBasic
|
||||
? this.type
|
||||
: { [stringCamelCase(this.type)]: this.#raw.toPrimitive() };
|
||||
: { [stringCamelCase(this.type)]: this.#raw.toPrimitive(disableAscii) };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -183,8 +183,8 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
/**
|
||||
* @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.#raw.toHuman(isExtended);
|
||||
public toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson {
|
||||
return this.#raw.toHuman(isExtended, disableAscii);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -199,10 +199,10 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
/**
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public toPrimitive (): AnyJson {
|
||||
public toPrimitive (disableAscii?: boolean): AnyJson {
|
||||
return this.isNone
|
||||
? null
|
||||
: this.#raw.toPrimitive();
|
||||
: this.#raw.toPrimitive(disableAscii);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -163,11 +163,11 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
/**
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
public toHuman (isExtended?: boolean): AnyJson {
|
||||
public toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson {
|
||||
const json: AnyJson = [];
|
||||
|
||||
for (const v of this.values()) {
|
||||
json.push(v.toHuman(isExtended));
|
||||
json.push(v.toHuman(isExtended, disableAscii));
|
||||
}
|
||||
|
||||
return json;
|
||||
@@ -196,11 +196,11 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
/**
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public toPrimitive (): AnyJson {
|
||||
public toPrimitive (disableAscii?: boolean): AnyJson {
|
||||
const json: AnyJson = [];
|
||||
|
||||
for (const v of this.values()) {
|
||||
json.push(v.toPrimitive());
|
||||
json.push(v.toPrimitive(disableAscii));
|
||||
}
|
||||
|
||||
return json;
|
||||
|
||||
@@ -177,15 +177,15 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
/**
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
public toHuman (isExtended?: boolean): Record<string, AnyJson> {
|
||||
public toHuman (isExtended?: boolean, disableAscii?: boolean): Record<string, AnyJson> {
|
||||
const json: Record<string, AnyJson> = {};
|
||||
|
||||
for (const [k, v] of this.entries()) {
|
||||
json[
|
||||
k instanceof Raw && k.isAscii
|
||||
k instanceof Raw && !disableAscii && k.isAscii
|
||||
? k.toUtf8()
|
||||
: k.toString()
|
||||
] = v.toHuman(isExtended);
|
||||
] = v.toHuman(isExtended, disableAscii);
|
||||
}
|
||||
|
||||
return json;
|
||||
@@ -207,15 +207,15 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
/**
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public toPrimitive (): AnyJson {
|
||||
public toPrimitive (disableAscii?: boolean): AnyJson {
|
||||
const json: Record<string, AnyJson> = {};
|
||||
|
||||
for (const [k, v] of this.entries()) {
|
||||
json[
|
||||
k instanceof Raw && k.isAscii
|
||||
k instanceof Raw && !disableAscii && k.isAscii
|
||||
? k.toUtf8()
|
||||
: k.toString()
|
||||
] = v.toPrimitive();
|
||||
] = v.toPrimitive(disableAscii);
|
||||
}
|
||||
|
||||
return json;
|
||||
|
||||
@@ -84,19 +84,19 @@ export class WrapperKeepOpaque<T extends Codec> extends Bytes {
|
||||
/**
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
public override toHuman (isExtended?: boolean): AnyJson {
|
||||
public override toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson {
|
||||
return this.#decoded
|
||||
? this.#decoded.toHuman(isExtended)
|
||||
: super.toHuman();
|
||||
? this.#decoded.toHuman(isExtended, disableAscii)
|
||||
: super.toHuman(isExtended, disableAscii);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public override toPrimitive (): any {
|
||||
public override toPrimitive (disableAscii?: boolean): any {
|
||||
return this.#decoded
|
||||
? this.#decoded.toPrimitive()
|
||||
: super.toPrimitive();
|
||||
? this.#decoded.toPrimitive(disableAscii)
|
||||
: super.toPrimitive(disableAscii);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -114,10 +114,10 @@ export class Json extends Map<string, any> implements Codec {
|
||||
/**
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public toPrimitive (): Record<string, AnyJson> {
|
||||
public toPrimitive (disableAscii?: boolean): Record<string, AnyJson> {
|
||||
return [...this.entries()].reduce<Record<string, AnyJson>>((json, [key, value]): Record<string, AnyJson> => {
|
||||
json[key] = isFunction((value as Codec).toPrimitive)
|
||||
? (value as Codec).toPrimitive()
|
||||
? (value as Codec).toPrimitive(disableAscii)
|
||||
: value as AnyJson;
|
||||
|
||||
return json;
|
||||
|
||||
@@ -82,6 +82,10 @@ describe('Raw', (): void => {
|
||||
expect(new Raw(registry, '0x2021222324').isAscii).toBe(true);
|
||||
});
|
||||
|
||||
it('has valid toHuman with disableAscii option set as true', (): void => {
|
||||
expect(new Raw(registry, new Uint8Array([85, 85, 85, 85, 85, 85, 85, 85])).toHuman(undefined, true)).toEqual('0x5555555555555555');
|
||||
});
|
||||
|
||||
it('has valid toPrimitive', (): void => {
|
||||
expect(new Raw(registry, 'testing').toPrimitive()).toEqual('testing');
|
||||
expect(new Raw(registry, '0xe4bda0e5a5bd').toPrimitive()).toEqual('0xe4bda0e5a5bd');
|
||||
|
||||
@@ -110,8 +110,8 @@ 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();
|
||||
public toHuman (_isExtended?: boolean, disableAscii?: boolean): AnyJson {
|
||||
return this.toPrimitive(disableAscii);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,8 +124,8 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
/**
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public toPrimitive (): AnyJson {
|
||||
if (this.isAscii) {
|
||||
public toPrimitive (disableAscii?: boolean): AnyJson {
|
||||
if (!disableAscii && this.isAscii) {
|
||||
const text = this.toUtf8();
|
||||
|
||||
// ensure we didn't end up with multibyte codepoints
|
||||
|
||||
@@ -268,11 +268,11 @@ export class Struct<
|
||||
/**
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
public toHuman (isExtended?: boolean): Record<string, AnyJson> {
|
||||
public toHuman (isExtended?: boolean, disableAscii?: boolean): Record<string, AnyJson> {
|
||||
const json: Record<string, AnyJson> = {};
|
||||
|
||||
for (const [k, v] of this.entries()) {
|
||||
json[k as string] = v.toHuman(isExtended);
|
||||
json[k as string] = v.toHuman(isExtended, disableAscii);
|
||||
}
|
||||
|
||||
return json;
|
||||
@@ -296,11 +296,11 @@ export class Struct<
|
||||
/**
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
*/
|
||||
public toPrimitive (): Record<string, AnyJson> {
|
||||
public toPrimitive (disableAscii?: boolean): Record<string, AnyJson> {
|
||||
const json: Record<string, AnyJson> = {};
|
||||
|
||||
for (const [k, v] of this.entries()) {
|
||||
json[k as string] = v.toPrimitive();
|
||||
json[k as string] = v.toPrimitive(disableAscii);
|
||||
}
|
||||
|
||||
return json;
|
||||
|
||||
@@ -86,8 +86,10 @@ export interface Codec {
|
||||
|
||||
/**
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
* @param isExtended When set, for some (e.g. call) it can add more info, e.g. metadata documentation. (Generally not needed in all cases, but can be useful in Events, Calls, ...)
|
||||
* @param disableAscii When set, for some (e.g. `Raw` types) it will disable converting the value to ascii.
|
||||
*/
|
||||
toHuman (isExtended?: boolean): AnyJson;
|
||||
toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson;
|
||||
|
||||
/**
|
||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||
@@ -96,8 +98,9 @@ export interface Codec {
|
||||
|
||||
/**
|
||||
* @description Converts the value in a best-fit primitive form
|
||||
* @param disableAscii
|
||||
*/
|
||||
toPrimitive (): AnyJson;
|
||||
toPrimitive (disableAscii?: boolean): AnyJson;
|
||||
|
||||
/**
|
||||
* @description Returns the base runtime type name for this instance
|
||||
|
||||
@@ -31,7 +31,7 @@ export interface INumber extends Codec {
|
||||
toBigInt (): bigint;
|
||||
toBn (): BN;
|
||||
toNumber (): number;
|
||||
toPrimitive (): string | number;
|
||||
toPrimitive (disableAscii?: boolean): string | number;
|
||||
}
|
||||
|
||||
export interface IFloat extends Codec {
|
||||
@@ -78,7 +78,7 @@ export interface IU8a extends Uint8Array, Codec {
|
||||
readonly isUtf8: boolean
|
||||
|
||||
bitLength (): number;
|
||||
toHuman (isExtended?: boolean): any;
|
||||
toHuman (isExtended?: boolean, disableAscii?: boolean): any;
|
||||
toJSON (): any;
|
||||
toUtf8 (): string;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user