Skip to content

Commit

Permalink
add toPrimitive options
Browse files Browse the repository at this point in the history
  • Loading branch information
TarikGul committed Mar 21, 2024
1 parent 123737a commit fbb7691
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 23 deletions.
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 @@ -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;
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 @@ -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);
}

/**
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 @@ -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
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 @@ -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);
}

/**
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 @@ -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) };
}

/**
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 @@ -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);
}

/**
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 @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions packages/types-codec/src/extended/WrapperKeepOpaque.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,16 @@ export class WrapperKeepOpaque<T extends Codec> extends Bytes {
public override toHuman (isExtended?: boolean, disableAscii?: boolean): AnyJson {
return this.#decoded
? this.#decoded.toHuman(isExtended, disableAscii)
: super.toHuman();
: 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);
}

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

0 comments on commit fbb7691

Please sign in to comment.