Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add disableAscii option for toHuman, and toPrimitive #5831

Merged
merged 7 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/types-codec/src/abstract/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions packages/types-codec/src/abstract/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/types-codec/src/abstract/Object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/types-codec/src/base/Compact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/types-codec/src/base/Enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/types-codec/src/base/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/types-codec/src/extended/BTreeSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions packages/types-codec/src/extended/Map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
TarikGul marked this conversation as resolved.
Show resolved Hide resolved
? k.toUtf8()
: k.toString()
] = v.toHuman(isExtended);
] = v.toHuman(isExtended, disableAscii);
}

return json;
Expand Down
4 changes: 2 additions & 2 deletions packages/types-codec/src/extended/WrapperKeepOpaque.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ 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)
? this.#decoded.toHuman(isExtended, disableAscii)
: super.toHuman();
}

Expand Down
4 changes: 4 additions & 0 deletions packages/types-codec/src/native/Raw.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
8 changes: 4 additions & 4 deletions packages/types-codec/src/native/Raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions packages/types-codec/src/native/Struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions packages/types-codec/src/types/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 6 additions & 6 deletions packages/types/src/extrinsic/Extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/extrinsic/ExtrinsicPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/generic/Call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
},
Expand Down
8 changes: 4 additions & 4 deletions packages/types/src/generic/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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)
);
}
}
6 changes: 3 additions & 3 deletions packages/types/src/primitive/StorageKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Loading