Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add utility isEmptyString #792

Merged
merged 10 commits into from
Dec 16, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `emptyString` (the opposite of existing `notEmptyString`).

# 2024.11.27-1230Z

Expand Down
15 changes: 15 additions & 0 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ npm i @dfinity/agent @dfinity/candid @dfinity/principal
- [isNullish](#gear-isnullish)
- [nonNullish](#gear-nonnullish)
- [notEmptyString](#gear-notemptystring)
- [emptyString](#gear-emptystring)
- [defaultAgent](#gear-defaultagent)
- [createAgent](#gear-createagent)
- [createServices](#gear-createservices)
Expand Down Expand Up @@ -121,6 +122,20 @@ Parameters:

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/utils/nullish.utils.ts#L29)

#### :gear: emptyString

Checks if a given value is null, undefined, or an empty string.

| Function | Type |
| ------------- | ------------------------------------------------- |
| `emptyString` | `(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.
Expand Down
16 changes: 15 additions & 1 deletion packages/utils/src/utils/nullish.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { isNullish, nonNullish, notEmptyString } from "./nullish.utils";
import {
emptyString,
isNullish,
nonNullish,
notEmptyString,
} from "./nullish.utils";

describe("nullish-utils", () => {
describe("isNullish", () => {
Expand Down Expand Up @@ -42,4 +47,13 @@ describe("nullish-utils", () => {
expect(notEmptyString("test")).toBeTruthy();
});
});

describe("emptyString", () => {
peterpeterparker marked this conversation as resolved.
Show resolved Hide resolved
it("should determine empty", () => {
expect(emptyString(null)).toBeTruthy();
expect(emptyString(undefined)).toBeTruthy();
expect(emptyString("")).toBeTruthy();
expect(emptyString("test")).toBeFalsy();
});
});
});
9 changes: 9 additions & 0 deletions packages/utils/src/utils/nullish.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ export const nonNullish = <T>(
*/
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 emptyString = (value: string | undefined | null): boolean =>
peterpeterparker marked this conversation as resolved.
Show resolved Hide resolved
!notEmptyString(value);