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:
Tarik Gul
2024-03-21 19:17:53 +02:00
committed by GitHub
parent bfabfcd99f
commit 0982f68507
20 changed files with 76 additions and 69 deletions
+6 -6
View File
@@ -307,20 +307,20 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public override toHuman (isExpanded?: boolean): AnyJson {
public override toHuman (isExpanded?: boolean, disableAscii?: boolean): AnyJson {
return objectSpread<Record<string, AnyJson>>(
{},
{
isSigned: this.isSigned,
method: this.method.toHuman(isExpanded)
method: this.method.toHuman(isExpanded, disableAscii)
},
this.isSigned
? {
era: this.era.toHuman(isExpanded),
nonce: this.nonce.toHuman(isExpanded),
era: this.era.toHuman(isExpanded, disableAscii),
nonce: this.nonce.toHuman(isExpanded, disableAscii),
signature: this.signature.toHex(),
signer: this.signer.toHuman(isExpanded),
tip: this.tip.toHuman(isExpanded)
signer: this.signer.toHuman(isExpanded, disableAscii),
tip: this.tip.toHuman(isExpanded, disableAscii)
}
: null
);
@@ -134,8 +134,8 @@ export class GenericExtrinsicPayload extends AbstractBase<ExtrinsicPayloadVx> {
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public override toHuman (isExtended?: boolean): AnyJson {
return this.inner.toHuman(isExtended);
public override toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson {
return this.inner.toHuman(isExtended, disableAscii);
}
/**
+2 -2
View File
@@ -213,7 +213,7 @@ export class GenericCall<A extends AnyTuple = AnyTuple> extends Struct implement
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public override toHuman (isExpanded?: boolean): Record<string, AnyJson> {
public override toHuman (isExpanded?: boolean, disableAscii?: boolean): Record<string, AnyJson> {
let call: CallFunction | undefined;
try {
@@ -225,7 +225,7 @@ export class GenericCall<A extends AnyTuple = AnyTuple> extends Struct implement
return objectSpread(
{
args: this.argsEntries.reduce<Record<string, AnyJson>>((args, [n, a]) =>
objectSpread(args, { [n]: a.toHuman(isExpanded) }), {}),
objectSpread(args, { [n]: a.toHuman(isExpanded, disableAscii) }), {}),
method: call?.method,
section: call?.section
},
+4 -4
View File
@@ -106,12 +106,12 @@ export class GenericEventData extends Tuple implements IEventData {
/**
* @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 {
if (this.#names !== null) {
const json: Record<string, AnyJson> = {};
for (let i = 0, count = this.#names.length; i < count; i++) {
json[this.#names[i]] = this[i].toHuman(isExtended);
json[this.#names[i]] = this[i].toHuman(isExtended, disableAscii);
}
return json;
@@ -185,7 +185,7 @@ export class GenericEvent extends Struct implements IEvent<Codec[]> {
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public override toHuman (isExpanded?: boolean): Record<string, AnyJson> {
public override toHuman (isExpanded?: boolean, disableAscii?: boolean): Record<string, AnyJson> {
return objectSpread(
{
method: this.method,
@@ -194,7 +194,7 @@ export class GenericEvent extends Struct implements IEvent<Codec[]> {
isExpanded
? { docs: this.meta.docs.map((d) => d.toString()) }
: null,
super.toHuman(isExpanded)
super.toHuman(isExpanded, disableAscii)
);
}
}
+3 -3
View File
@@ -236,10 +236,10 @@ export class StorageKey<A extends AnyTuple = AnyTuple> extends Bytes implements
/**
* @description Returns the Human representation for this type
*/
public override toHuman (): AnyJson {
public override toHuman (_isExtended?: boolean, disableAscii?: boolean): AnyJson {
return this.#args.length
? this.#args.map((a) => a.toHuman())
: super.toHuman();
? this.#args.map((a) => a.toHuman(undefined, disableAscii))
: super.toHuman(undefined, disableAscii);
}
/**