Skip to content

Commit

Permalink
feat: extend toNullable to handle null (#288)
Browse files Browse the repository at this point in the history
# Motivation

Extend `toNullable` to handle `null`.

As discussed with @lmuntaner .
  • Loading branch information
peterpeterparker authored Feb 13, 2023
1 parent 22d962c commit 5448d53
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
42 changes: 21 additions & 21 deletions packages/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ npm i @dfinity/agent @dfinity/candid @dfinity/principal
- [assertNonNullish](#gear-assertnonnullish)
- [assertPercentageNumber](#gear-assertpercentagenumber)
- [debounce](#gear-debounce)
- [toNullable](#gear-tonullable)
- [fromNullable](#gear-fromnullable)
- [fromDefinedNullable](#gear-fromdefinednullable)
- [isNullish](#gear-isnullish)
- [nonNullish](#gear-nonnullish)
- [notEmptyString](#gear-notemptystring)
- [toNullable](#gear-tonullable)
- [fromNullable](#gear-fromnullable)
- [fromDefinedNullable](#gear-fromdefinednullable)
- [principalToSubAccount](#gear-principaltosubaccount)
- [smallerVersion](#gear-smallerversion)

Expand Down Expand Up @@ -141,24 +141,6 @@ Parameters:
| ---------- | -------------------------------------------------------------------- |
| `debounce` | `(func: Function, timeout?: number) => (...args: unknown[]) => void` |

#### :gear: toNullable

| Function | Type |
| ------------ | ----------------------------- |
| `toNullable` | `<T>(value?: T) => [] or [T]` |

#### :gear: fromNullable

| Function | Type |
| -------------- | ---------------------------- |
| `fromNullable` | `<T>(value: [] or [T]) => T` |

#### :gear: fromDefinedNullable

| Function | Type |
| --------------------- | ---------------------------- |
| `fromDefinedNullable` | `<T>(value: [] or [T]) => T` |

#### :gear: isNullish

Is null or undefined
Expand All @@ -183,6 +165,24 @@ Not null and not undefined and not empty
| ---------------- | ---------------------------- |
| `notEmptyString` | `(value: string) => boolean` |

#### :gear: toNullable

| Function | Type |
| ------------ | ----------------------------- |
| `toNullable` | `<T>(value?: T) => [] or [T]` |

#### :gear: fromNullable

| Function | Type |
| -------------- | ---------------------------- |
| `fromNullable` | `<T>(value: [] or [T]) => T` |

#### :gear: fromDefinedNullable

| Function | Type |
| --------------------- | ---------------------------- |
| `fromDefinedNullable` | `<T>(value: [] or [T]) => T` |

#### :gear: principalToSubAccount

Convert a principal to a Uint8Array 32 length.
Expand Down
8 changes: 6 additions & 2 deletions packages/utils/src/utils/did.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ describe("did-utils", () => {
expect(toNullable(undefined)).toEqual([]);
});

it("should convert from null to empty array", () => {
expect(toNullable(null)).toEqual([]);
});

it("should convert object to array", () => {
const test = { test: "1" };
expect(toNullable(test)).toEqual([test]);
Expand All @@ -25,9 +29,9 @@ describe("did-utils", () => {
expect(toNullable(test)).toEqual([test]);
});

it("should convert null to array", () => {
it("should convert null to empty array", () => {
const test = null;
expect(toNullable(test)).toEqual([test]);
expect(toNullable(test)).toEqual([]);
});

it("should convert 0 to array", () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/utils/src/utils/did.utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { assertNonNullish } from "./asserts.utils";
import { nonNullish } from "./nullish.utils";

export const toNullable = <T>(value?: T): [] | [T] => {
return value !== undefined ? [value] : [];
export const toNullable = <T>(value?: T | null): [] | [T] => {
return nonNullish(value) ? [value] : [];
};

export const fromNullable = <T>(value: [] | [T]): T | undefined => {
Expand Down

0 comments on commit 5448d53

Please sign in to comment.