mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 13:47:12 +00:00
Export omitDefault
to help build Amino JSON converters
This commit is contained in:
parent
bb34b58dd1
commit
dbc702afe8
@ -12,6 +12,7 @@ and this project adheres to
|
||||
`sign`/`signAndBroadcast`/`signAndBroadcastSync` and related functions now
|
||||
have an additional parameter to specify the timeout height. After this height,
|
||||
a signed transaction will be considered invalid by the chain. ([#1489])
|
||||
- @cosmjs/amino: Export `omitDefault` to help build Amino JSON converters.
|
||||
|
||||
[#1489]: https://github.com/cosmos/cosmjs/pull/1489
|
||||
|
||||
|
@ -14,6 +14,7 @@ export {
|
||||
encodeSecp256k1Pubkey,
|
||||
} from "./encoding";
|
||||
export { createMultisigThresholdPubkey } from "./multisig";
|
||||
export { omitDefault } from "./omitdefault";
|
||||
export { makeCosmoshubPath } from "./paths";
|
||||
export {
|
||||
Ed25519Pubkey,
|
||||
|
23
packages/amino/src/omitdefault.spec.ts
Normal file
23
packages/amino/src/omitdefault.spec.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { omitDefault } from "./omitdefault";
|
||||
|
||||
describe("omitDefault", () => {
|
||||
it("works for numbers", () => {
|
||||
expect(omitDefault(17)).toEqual(17);
|
||||
expect(omitDefault(0)).toEqual(undefined);
|
||||
});
|
||||
|
||||
it("works for bigint", () => {
|
||||
expect(omitDefault(17n)).toEqual(17n);
|
||||
expect(omitDefault(0n)).toEqual(undefined);
|
||||
});
|
||||
|
||||
it("works for boolean", () => {
|
||||
expect(omitDefault(true)).toEqual(true);
|
||||
expect(omitDefault(false)).toEqual(undefined);
|
||||
});
|
||||
|
||||
it("works for strings", () => {
|
||||
expect(omitDefault("hi")).toEqual("hi");
|
||||
expect(omitDefault("")).toEqual(undefined);
|
||||
});
|
||||
});
|
18
packages/amino/src/omitdefault.ts
Normal file
18
packages/amino/src/omitdefault.ts
Normal file
@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Returns the given input. If the input is the default value
|
||||
* of protobuf, undefined is retunred. Use this when creating Amino JSON converters.
|
||||
*/
|
||||
export function omitDefault<T extends string | number | bigint | boolean>(input: T): T | undefined {
|
||||
switch (typeof input) {
|
||||
case "string":
|
||||
return input === "" ? undefined : input;
|
||||
case "number":
|
||||
return input === 0 ? undefined : input;
|
||||
case "bigint":
|
||||
return input === BigInt(0) ? undefined : input;
|
||||
case "boolean":
|
||||
return !input ? undefined : input;
|
||||
default:
|
||||
throw new Error(`Got unsupported type '${typeof input}'`);
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { AminoMsg, Coin } from "@cosmjs/amino";
|
||||
import { AminoMsg, Coin, omitDefault } from "@cosmjs/amino";
|
||||
import { MsgTransfer } from "cosmjs-types/ibc/applications/transfer/v1/tx";
|
||||
|
||||
import { AminoConverters } from "../../aminotypes";
|
||||
@ -45,22 +45,6 @@ export function isAminoMsgTransfer(msg: AminoMsg): msg is AminoMsgTransfer {
|
||||
return msg.type === "cosmos-sdk/MsgTransfer";
|
||||
}
|
||||
|
||||
function omitDefault<T extends string | number | bigint>(input: T): T | undefined {
|
||||
if (typeof input === "string") {
|
||||
return input === "" ? undefined : input;
|
||||
}
|
||||
|
||||
if (typeof input === "number") {
|
||||
return input === 0 ? undefined : input;
|
||||
}
|
||||
|
||||
if (typeof input === "bigint") {
|
||||
return input === BigInt(0) ? undefined : input;
|
||||
}
|
||||
|
||||
throw new Error(`Got unsupported type '${typeof input}'`);
|
||||
}
|
||||
|
||||
export function createIbcAminoConverters(): AminoConverters {
|
||||
return {
|
||||
"/ibc.applications.transfer.v1.MsgTransfer": {
|
||||
|
Loading…
x
Reference in New Issue
Block a user