From 63961f79dc91d44e937b1d01471e2fa06a5cd6b1 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sun, 9 Oct 2022 18:28:40 +0200 Subject: [PATCH] Add isDefined --- CHANGELOG.md | 5 +++++ packages/faucet/src/faucet.ts | 6 +----- packages/utils/src/index.ts | 2 +- packages/utils/src/typechecks.spec.ts | 17 ++++++++++++++++- packages/utils/src/typechecks.ts | 10 ++++++++++ 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a8a755c1e..85e5885bc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to ## [Unreleased] +### Added + +- @cosmjs/utils: Add `isDefined` which checks for `undefined` in a + TypeScript-friendly way. + ## [0.29.1] - 2022-10-09 ### Changed diff --git a/packages/faucet/src/faucet.ts b/packages/faucet/src/faucet.ts index bf9bd760c7..b087d38d4f 100644 --- a/packages/faucet/src/faucet.ts +++ b/packages/faucet/src/faucet.ts @@ -4,7 +4,7 @@ import { SigningStargateClient, StargateClient, } from "@cosmjs/stargate"; -import { sleep } from "@cosmjs/utils"; +import { isDefined, sleep } from "@cosmjs/utils"; import * as constants from "./constants"; import { debugAccount, logAccountsState, logSendJob } from "./debugging"; @@ -13,10 +13,6 @@ import { createClients, createWallets } from "./profile"; import { TokenConfiguration, TokenManager } from "./tokenmanager"; import { MinimalAccount, SendJob } from "./types"; -function isDefined(value: X | undefined): value is X { - return value !== undefined; -} - export class Faucet { public static async make( apiUrl: string, diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index 790c7d277c..624348188b 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -1,4 +1,4 @@ export { arrayContentEquals, arrayContentStartsWith } from "./arrays"; export { assert, assertDefined, assertDefinedAndNotNull } from "./assert"; export { sleep } from "./sleep"; -export { isNonNullObject, isUint8Array } from "./typechecks"; +export { isDefined, isNonNullObject, isUint8Array } from "./typechecks"; diff --git a/packages/utils/src/typechecks.spec.ts b/packages/utils/src/typechecks.spec.ts index 5abbac112b..ddbf1209df 100644 --- a/packages/utils/src/typechecks.spec.ts +++ b/packages/utils/src/typechecks.spec.ts @@ -1,4 +1,4 @@ -import { isNonNullObject, isUint8Array } from "./typechecks"; +import { isDefined, isNonNullObject, isUint8Array } from "./typechecks"; describe("typechecks", () => { describe("isNonNullObject", () => { @@ -55,4 +55,19 @@ describe("typechecks", () => { expect(isUint8Array(new Uint16Array())).toEqual(false); }); }); + + describe("isDefined", () => { + it("works", () => { + expect(isDefined("a")).toEqual(true); + expect(isDefined(1234)).toEqual(true); + expect(isDefined(null)).toEqual(true); + expect(isDefined(undefined)).toEqual(false); + }); + + it("narrows type of list", () => { + const a = ["one", undefined, "two", "three", undefined]; + const b: string[] = a.filter(isDefined); + expect(b).toEqual(["one", "two", "three"]); + }); + }); }); diff --git a/packages/utils/src/typechecks.ts b/packages/utils/src/typechecks.ts index c4f56347d2..1d35db5831 100644 --- a/packages/utils/src/typechecks.ts +++ b/packages/utils/src/typechecks.ts @@ -30,3 +30,13 @@ export function isUint8Array(data: unknown): data is Uint8Array { return true; } + +/** + * Checks if input is not undefined in a TypeScript-friendly way. + * + * This is convenient to use in e.g. `Array.filter` as it will convert + * the type of a `Array` to `Array`. + */ +export function isDefined(value: X | undefined): value is X { + return value !== undefined; +}