Pull out assertDefined
This commit is contained in:
parent
b212e00cf7
commit
bbee44da54
@ -3,7 +3,7 @@ import { Bech32, toAscii, toHex } from "@cosmjs/encoding";
|
||||
import { Uint64 } from "@cosmjs/math";
|
||||
import { BaseAccount, Coin, decodeAny } from "@cosmjs/proto-signing";
|
||||
import { Client as TendermintClient } from "@cosmjs/tendermint-rpc";
|
||||
import { assert } from "@cosmjs/utils";
|
||||
import { assertDefined } from "@cosmjs/utils";
|
||||
import Long from "long";
|
||||
|
||||
export interface GetSequenceResult {
|
||||
@ -37,8 +37,8 @@ export class StargateClient {
|
||||
switch (typeUrl) {
|
||||
case "/cosmos.auth.BaseAccount": {
|
||||
const { account_number, sequence } = BaseAccount.decode(value);
|
||||
assert(account_number !== undefined);
|
||||
assert(sequence !== undefined);
|
||||
assertDefined(account_number);
|
||||
assertDefined(sequence);
|
||||
return {
|
||||
accountNumber: uint64FromProto(account_number).toNumber(),
|
||||
sequence: uint64FromProto(sequence).toNumber(),
|
||||
@ -67,8 +67,8 @@ export class StargateClient {
|
||||
|
||||
const responseData = await this.queryVerified("bank", bankKey);
|
||||
const { amount, denom } = Coin.decode(responseData);
|
||||
assert(amount !== undefined);
|
||||
assert(denom !== undefined);
|
||||
assertDefined(amount);
|
||||
assertDefined(denom);
|
||||
|
||||
if (denom === "") {
|
||||
return null;
|
||||
|
||||
52
packages/utils/src/assert.spec.ts
Normal file
52
packages/utils/src/assert.spec.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import { assertDefined } from "./assert";
|
||||
|
||||
describe("assert", () => {
|
||||
describe("assertDefined", () => {
|
||||
it("passes for simple values", () => {
|
||||
{
|
||||
const value: number | undefined = 123;
|
||||
assertDefined(value);
|
||||
expect(value).toEqual(123);
|
||||
}
|
||||
{
|
||||
const value: string | undefined = "abc";
|
||||
assertDefined(value);
|
||||
expect(value).toEqual("abc");
|
||||
}
|
||||
});
|
||||
|
||||
it("passes for falsy values", () => {
|
||||
{
|
||||
const value: number | undefined = 0;
|
||||
assertDefined(value);
|
||||
expect(value).toEqual(0);
|
||||
}
|
||||
{
|
||||
const value: string | undefined = "";
|
||||
assertDefined(value);
|
||||
expect(value).toEqual("");
|
||||
}
|
||||
{
|
||||
const value: null | undefined = null;
|
||||
assertDefined(value);
|
||||
expect(value).toEqual(null);
|
||||
}
|
||||
});
|
||||
|
||||
it("throws for undefined values", () => {
|
||||
{
|
||||
const value: number | undefined = undefined;
|
||||
expect(() => assertDefined(value)).toThrowError("value is undefined");
|
||||
}
|
||||
{
|
||||
let value: string | undefined;
|
||||
expect(() => assertDefined(value)).toThrowError("value is undefined");
|
||||
}
|
||||
});
|
||||
|
||||
it("throws with custom message", () => {
|
||||
const value: number | undefined = undefined;
|
||||
expect(() => assertDefined(value, "Bug in the data source")).toThrowError("Bug in the data source");
|
||||
});
|
||||
});
|
||||
});
|
||||
@ -4,3 +4,9 @@ export function assert(condition: any, msg?: string): asserts condition {
|
||||
throw new Error(msg || "condition is not truthy");
|
||||
}
|
||||
}
|
||||
|
||||
export function assertDefined<T>(value: T | undefined, msg?: string): asserts value is T {
|
||||
if (value === undefined) {
|
||||
throw new Error(msg || "value is undefined");
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,3 @@
|
||||
export { assert } from "./assert";
|
||||
export { assert, assertDefined } from "./assert";
|
||||
export { sleep } from "./sleep";
|
||||
export { isNonNullObject, isUint8Array } from "./typechecks";
|
||||
|
||||
1
packages/utils/types/assert.d.ts
vendored
1
packages/utils/types/assert.d.ts
vendored
@ -1 +1,2 @@
|
||||
export declare function assert(condition: any, msg?: string): asserts condition;
|
||||
export declare function assertDefined<T>(value: T | undefined, msg?: string): asserts value is T;
|
||||
|
||||
2
packages/utils/types/index.d.ts
vendored
2
packages/utils/types/index.d.ts
vendored
@ -1,3 +1,3 @@
|
||||
export { assert } from "./assert";
|
||||
export { assert, assertDefined } from "./assert";
|
||||
export { sleep } from "./sleep";
|
||||
export { isNonNullObject, isUint8Array } from "./typechecks";
|
||||
|
||||
Loading…
Reference in New Issue
Block a user