Skip to content

Commit

Permalink
feat: rename toJson to toJSON
Browse files Browse the repository at this point in the history
This PR is to make CAIP objects compatiable with JSON.stringify that honors `toJSON`.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#description

Signed-off-by: Raymond Feng <[email protected]>
  • Loading branch information
raymondfeng committed Aug 21, 2021
1 parent bd1f355 commit 8ed8ef1
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 29 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const chainId = new ChainId({ namespace: "eip155", reference: "1" });
chainId.toString();
// "eip155:1"

chainId.toJson();
chainId.toJSON();
// { namespace: "eip155", reference: "1" }
```

Expand Down Expand Up @@ -68,7 +68,7 @@ const accountId = new AccountId({
accountId.toString();
// "eip155:1:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb"

accountId.toJson();
accountId.toJSON();
// { address: "0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb", chainId: { namespace: "eip155", reference: "1" } }
```

Expand Down Expand Up @@ -132,7 +132,7 @@ const assetId = new AssetId({
assetId.toString();
// "eip155:1/erc721:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb/1"

assetId.toJson();
assetId.toJSON();
// {
// chainId: { namespace: "eip155", reference: "1" },
// assetName: { namespace: "erc721", reference: "0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb" },
Expand Down Expand Up @@ -207,7 +207,7 @@ const assetType = new AssetType({
assetType.toString();
// "eip155:1/erc721:0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb"

assetType.toJson();
assetType.toJSON();
// {
// chainId: { namespace: "eip155", reference: "1" },
// assetName: { namespace: "erc721", reference: "0xab16a96d359ec26a11e2c2b3d8f8b8942d5bfcdb" },
Expand Down
10 changes: 5 additions & 5 deletions src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export class AccountId {
this.spec
);
const chainId = new ChainId({ namespace, reference });
return new AccountId({ chainId, address }).toJson();
return new AccountId({ chainId, address }).toJSON();
}

public static format(params: AccountIdParams): string {
const chainId = new ChainId(params.chainId);
const splitParams: AccountIdSplitParams = {
...chainId.toJson(),
...chainId.toJSON(),
address: params.address,
};
return joinParams(splitParams as any, this.spec);
Expand All @@ -48,12 +48,12 @@ export class AccountId {
}

public toString(): string {
return AccountId.format(this.toJson());
return AccountId.format(this.toJSON());
}

public toJson(): AccountIdParams {
public toJSON(): AccountIdParams {
return {
chainId: this.chainId.toJson(),
chainId: this.chainId.toJSON(),
address: this.address,
};
}
Expand Down
10 changes: 5 additions & 5 deletions src/assetId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class AssetId {
if (!isValidId(id, this.spec)) {
throw new Error(`Invalid ${this.spec.name} provided: ${id}`);
}
return new AssetId(getParams<AssetIdParams>(id, this.spec)).toJson();
return new AssetId(getParams<AssetIdParams>(id, this.spec)).toJSON();
}

public static format(params: AssetIdParams): string {
Expand All @@ -39,13 +39,13 @@ export class AssetId {
}

public toString(): string {
return AssetId.format(this.toJson());
return AssetId.format(this.toJSON());
}

public toJson(): AssetIdParams {
public toJSON(): AssetIdParams {
return {
chainId: this.chainId.toJson(),
assetName: this.assetName.toJson(),
chainId: this.chainId.toJSON(),
assetName: this.assetName.toJSON(),
tokenId: this.tokenId,
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/assetName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class AssetName {
if (!isValidId(id, this.spec)) {
throw new Error(`Invalid ${this.spec.name} provided: ${id}`);
}
return new AssetName(getParams<AssetNameParams>(id, this.spec)).toJson();
return new AssetName(getParams<AssetNameParams>(id, this.spec)).toJSON();
}

public static format(params: AssetNameParams): string {
Expand All @@ -34,10 +34,10 @@ export class AssetName {
}

public toString(): string {
return AssetName.format(this.toJson());
return AssetName.format(this.toJSON());
}

public toJson(): AssetNameParams {
public toJSON(): AssetNameParams {
return {
namespace: this.namespace,
reference: this.reference,
Expand Down
8 changes: 4 additions & 4 deletions src/assetType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class AssetType {
if (!isValidId(id, this.spec)) {
throw new Error(`Invalid ${this.spec.name} provided: ${id}`);
}
return new AssetType(getParams<AssetTypeParams>(id, this.spec)).toJson();
return new AssetType(getParams<AssetTypeParams>(id, this.spec)).toJSON();
}

public static format(params: AssetTypeParams): string {
Expand All @@ -36,12 +36,12 @@ export class AssetType {
}

public toString(): string {
return AssetType.format(this.toJson());
return AssetType.format(this.toJSON());
}

public toJson(): AssetTypeParams {
public toJSON(): AssetTypeParams {
return {
chainId: this.chainId.toJson(),
chainId: this.chainId.toJSON(),
assetName: this.assetName,
};
}
Expand Down
6 changes: 3 additions & 3 deletions src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ChainId {
if (!isValidId(id, this.spec)) {
throw new Error(`Invalid ${this.spec.name} provided: ${id}`);
}
return new ChainId(getParams<ChainIdParams>(id, this.spec)).toJson();
return new ChainId(getParams<ChainIdParams>(id, this.spec)).toJSON();
}

public static format(params: ChainIdParams): string {
Expand All @@ -34,10 +34,10 @@ export class ChainId {
}

public toString(): string {
return ChainId.format(this.toJson());
return ChainId.format(this.toJSON());
}

public toJson(): ChainIdParams {
public toJSON(): ChainIdParams {
return {
namespace: this.namespace,
reference: this.reference,
Expand Down
9 changes: 8 additions & 1 deletion test/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function assertAccountIdInterface(result: AccountId) {
expect(result.chainId.toString()).toEqual(data.CHAIN_ID_STRING);
expect(result.address).toEqual(data.ACCOUNT_ID_ADDRESS);
expect(result.toString()).toEqual(data.ACCOUNT_ID_STRING);
expect(result.toJson()).toEqual(data.ACCOUNT_ID_NESTED_PARAMS);
expect(result.toJSON()).toEqual(data.ACCOUNT_ID_NESTED_PARAMS);
}

describe("AccountId", () => {
Expand Down Expand Up @@ -34,4 +34,11 @@ describe("AccountId", () => {
const result = new AccountId(data.ACCOUNT_ID_NESTED_PARAMS);
assertAccountIdInterface(result);
});

it("should support JSON.stringify", async () => {
const result = new AccountId(data.ACCOUNT_ID_PARAMS);
const str = JSON.stringify(result);
const json = JSON.parse(str);
assertAccountIdInterface(new AccountId(json));
});
});
9 changes: 8 additions & 1 deletion test/assetId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function assertInterface(result: AssetId) {
expect(result.assetName.toString()).toEqual(data.ASSET_NAME_STRING);
expect(result.tokenId).toEqual(data.TOKEN_ID);
expect(result.toString()).toEqual(data.ASSET_ID_STRING);
expect(result.toJson()).toEqual(data.ASSET_ID_NESTED_PARAMS);
expect(result.toJSON()).toEqual(data.ASSET_ID_NESTED_PARAMS);
}

describe("AssetId", () => {
Expand Down Expand Up @@ -35,4 +35,11 @@ describe("AssetId", () => {
const result = new AssetId(data.ASSET_ID_NESTED_PARAMS);
assertInterface(result);
});

it("should support JSON.stringify", async () => {
const result = new AssetId(data.ASSET_ID_PARAMS);
const str = JSON.stringify(result);
const json = JSON.parse(str);
assertInterface(new AssetId(json));
});
});
9 changes: 8 additions & 1 deletion test/assetName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function assertInterface(result: AssetName) {
expect(result.namespace).toEqual(data.ASSET_NAMESPACE);
expect(result.reference).toEqual(data.ASSET_REFERENCE);
expect(result.toString()).toEqual(data.ASSET_NAME_STRING);
expect(result.toJson()).toEqual(data.ASSET_NAME_PARAMS);
expect(result.toJSON()).toEqual(data.ASSET_NAME_PARAMS);
}

describe("AssetName", () => {
Expand Down Expand Up @@ -34,4 +34,11 @@ describe("AssetName", () => {
const result = new AssetName(data.ASSET_NAME_PARAMS);
assertInterface(result);
});

it("should support JSON.stringify", async () => {
const result = new AssetName(data.ASSET_NAME_PARAMS);
const str = JSON.stringify(result);
const json = JSON.parse(str);
assertInterface(new AssetName(json));
});
});
9 changes: 8 additions & 1 deletion test/assetType.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function assertInterface(result: AssetType) {
expect(result.chainId.toString()).toEqual(data.CHAIN_ID_STRING);
expect(result.assetName.toString()).toEqual(data.ASSET_NAME_STRING);
expect(result.toString()).toEqual(data.ASSET_TYPE_STRING);
expect(result.toJson()).toEqual(data.ASSET_TYPE_NESTED_PARAMS);
expect(result.toJSON()).toEqual(data.ASSET_TYPE_NESTED_PARAMS);
}

describe("AssetType", () => {
Expand Down Expand Up @@ -34,4 +34,11 @@ describe("AssetType", () => {
const result = new AssetType(data.ASSET_TYPE_NESTED_PARAMS);
assertInterface(result);
});

it("should support JSON.stringify", async () => {
const result = new AssetType(data.ASSET_TYPE_PARAMS);
const str = JSON.stringify(result);
const json = JSON.parse(str);
assertInterface(new AssetType(json));
});
});
9 changes: 8 additions & 1 deletion test/chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function assertChainIdInterface(result: ChainId) {
expect(result.namespace).toEqual(data.CHAIN_ID_NAMESPACE);
expect(result.reference).toEqual(data.CHAIN_ID_REFERENCE);
expect(result.toString()).toEqual(data.CHAIN_ID_STRING);
expect(result.toJson()).toEqual(data.CHAIN_ID_PARAMS);
expect(result.toJSON()).toEqual(data.CHAIN_ID_PARAMS);
}

describe("ChainId", () => {
Expand All @@ -29,4 +29,11 @@ describe("ChainId", () => {
const result = new ChainId(data.CHAIN_ID_STRING);
assertChainIdInterface(result);
});

it("should support JSON.stringify", async () => {
const result = new ChainId(data.CHAIN_ID_STRING);
const str = JSON.stringify(result);
const json = JSON.parse(str);
assertChainIdInterface(new ChainId(json));
});
});

0 comments on commit 8ed8ef1

Please sign in to comment.