diff --git a/CHANGELOG.md b/CHANGELOG.md index 60971750..e0638334 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Expose a few additional types related to snapshots in `ic-management`. - Add support for `get_subnet_types_to_subnets` to `@dfinity/cmc`. - Support `VotingPowerEconomics`, `potential_voting_power` and `deciding_voting_power` in `@dfinity/nns`. +- Add utility `isEmptyString` (the opposite of existing `notEmptyString`). # 2024.11.27-1230Z diff --git a/packages/utils/README.md b/packages/utils/README.md index cbc71dfa..968fd6af 100644 --- a/packages/utils/README.md +++ b/packages/utils/README.md @@ -35,6 +35,7 @@ npm i @dfinity/agent @dfinity/candid @dfinity/principal - [isNullish](#gear-isnullish) - [nonNullish](#gear-nonnullish) - [notEmptyString](#gear-notemptystring) +- [isEmptyString](#gear-isemptystring) - [defaultAgent](#gear-defaultagent) - [createAgent](#gear-createagent) - [createServices](#gear-createservices) @@ -121,6 +122,20 @@ Parameters: [:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/utils/nullish.utils.ts#L29) +#### :gear: isEmptyString + +Checks if a given value is null, undefined, or an empty string. + +| Function | Type | +| --------------- | ------------------------------------------------- | +| `isEmptyString` | `(value: string or null or undefined) => boolean` | + +Parameters: + +- `value`: - The value to check. + +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/utils/nullish.utils.ts#L38) + #### :gear: defaultAgent Get a default agent that connects to mainnet with the anonymous identity. diff --git a/packages/utils/src/utils/nullish.utils.spec.ts b/packages/utils/src/utils/nullish.utils.spec.ts index 845ebd6b..30737616 100644 --- a/packages/utils/src/utils/nullish.utils.spec.ts +++ b/packages/utils/src/utils/nullish.utils.spec.ts @@ -1,4 +1,9 @@ -import { isNullish, nonNullish, notEmptyString } from "./nullish.utils"; +import { + isEmptyString, + isNullish, + nonNullish, + notEmptyString, +} from "./nullish.utils"; describe("nullish-utils", () => { describe("isNullish", () => { @@ -42,4 +47,13 @@ describe("nullish-utils", () => { expect(notEmptyString("test")).toBeTruthy(); }); }); + + describe("isEmptyString", () => { + it("should determine empty", () => { + expect(isEmptyString(null)).toBeTruthy(); + expect(isEmptyString(undefined)).toBeTruthy(); + expect(isEmptyString("")).toBeTruthy(); + expect(isEmptyString("test")).toBeFalsy(); + }); + }); }); diff --git a/packages/utils/src/utils/nullish.utils.ts b/packages/utils/src/utils/nullish.utils.ts index e6fb3d45..0a2d4df0 100644 --- a/packages/utils/src/utils/nullish.utils.ts +++ b/packages/utils/src/utils/nullish.utils.ts @@ -28,3 +28,12 @@ export const nonNullish = ( */ export const notEmptyString = (value: string | undefined | null): boolean => nonNullish(value) && value !== ""; + +/** + * Checks if a given value is null, undefined, or an empty string. + * + * @param {string | undefined | null} value - The value to check. + * @returns {boolean} `true` if the value is null, undefined, or an empty string; otherwise, `false`. + */ +export const isEmptyString = (value: string | undefined | null): boolean => + !notEmptyString(value);