From e0860decbebe47250ef4dabc99e404890f4de230 Mon Sep 17 00:00:00 2001 From: David Dal Busco Date: Mon, 13 Jan 2025 12:18:08 +0100 Subject: [PATCH 1/3] feat: fromNullishNullable Signed-off-by: David Dal Busco --- CHANGELOG.md | 1 + packages/utils/src/utils/did.utils.spec.ts | 85 ++++++++++++++-------- packages/utils/src/utils/did.utils.ts | 11 +++ 3 files changed, 68 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d70d18f91..92cd0d267 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Expose types `IcrcApproveError` and `IcrcTransferFromError` in `@dfinity/ledger-icrc`. - Expose few additional did types in `@dfinity/ledger-icp`. +- Add utility `fromNullishNullable` extracts the value from a nullish Candid-style variant representation. ## Fix diff --git a/packages/utils/src/utils/did.utils.spec.ts b/packages/utils/src/utils/did.utils.spec.ts index 9880180a7..e6eda9227 100644 --- a/packages/utils/src/utils/did.utils.spec.ts +++ b/packages/utils/src/utils/did.utils.spec.ts @@ -1,47 +1,56 @@ import { NullishError } from "./asserts.utils"; -import { fromDefinedNullable, fromNullable, toNullable } from "./did.utils"; +import { + fromDefinedNullable, + fromNullable, + fromNullishNullable, + toNullable, +} from "./did.utils"; describe("did-utils", () => { - it("should convert from empty array to undefined", () => { - expect(fromNullable([])).toBeUndefined(); - }); + const test = { test: "1" }; - it("should convert from array to object", () => { - const test = { test: "1" }; - expect(fromNullable([{ test: "1" }])).toEqual(test); - }); + describe("fromNullable", () => { + it("should convert from empty array to undefined", () => { + expect(fromNullable([])).toBeUndefined(); + }); - it("should convert from undefined to empty array", () => { - expect(toNullable(undefined)).toEqual([]); + it("should convert from array to object", () => { + expect(fromNullable([{ test: "1" }])).toEqual(test); + }); }); - it("should convert from null to empty array", () => { - expect(toNullable(null)).toEqual([]); - }); + describe("toNullable", () => { + it("should convert from undefined to empty array", () => { + expect(toNullable(undefined)).toEqual([]); + }); - it("should convert object to array", () => { - const test = { test: "1" }; - expect(toNullable(test)).toEqual([test]); - }); + it("should convert from null to empty array", () => { + expect(toNullable(null)).toEqual([]); + }); - it("should convert boolean to array", () => { - const test = false; - expect(toNullable(test)).toEqual([test]); - }); + it("should convert object to array", () => { + const test = { test: "1" }; + expect(toNullable(test)).toEqual([test]); + }); - it("should convert null to empty array", () => { - const test = null; - expect(toNullable(test)).toEqual([]); - }); + it("should convert boolean to array", () => { + const test = false; + expect(toNullable(test)).toEqual([test]); + }); - it("should convert 0 to array", () => { - const test = 0; - expect(toNullable(test)).toEqual([test]); + it("should convert null to empty array", () => { + const test = null; + expect(toNullable(test)).toEqual([]); + }); + + it("should convert 0 to array", () => { + const test = 0; + expect(toNullable(test)).toEqual([test]); + }); }); describe("fromDefinedNullable", () => { it("should convert from array to object", () => { - const test = { test: "1" }; expect(fromDefinedNullable([{ test: "1" }])).toEqual(test); }); @@ -49,4 +58,22 @@ describe("did-utils", () => { expect(() => fromDefinedNullable([])).toThrow(NullishError); }); }); + + describe("fromNullishNullable", () => { + it("should return undefined for an empty array", () => { + expect(fromNullishNullable([])).toBeUndefined(); + }); + + it("should return undefined for undefined input", () => { + expect(fromNullishNullable(undefined)).toBeUndefined(); + }); + + it("should return the value for a non-empty array", () => { + expect(fromNullishNullable([{ test: "1" }])).toEqual(test); + }); + + it("should return undefined when the array contains undefined", () => { + expect(fromNullishNullable([undefined])).toBeUndefined(); + }); + }); }); diff --git a/packages/utils/src/utils/did.utils.ts b/packages/utils/src/utils/did.utils.ts index 9b74be103..e12fb7999 100644 --- a/packages/utils/src/utils/did.utils.ts +++ b/packages/utils/src/utils/did.utils.ts @@ -16,3 +16,14 @@ export const fromDefinedNullable = (value: [] | [T]): T => { return result; }; + +/** + * Extracts the value from a nullish Candid-style variant representation. + * + * @template T The type of the value. + * @param {([] | [T]) | undefined} value - A Candid-style variant or `undefined`. + * @returns {T | undefined} The extracted value, or `undefined` if the input is nullish or the array is empty. + */ +export const fromNullishNullable = ( + value: ([] | [T]) | undefined, +): T | undefined => fromNullable(value ?? []); From 933611b8481d71a4ed5b2dacf871cc7f745da050 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 13 Jan 2025 11:19:32 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=A4=96=20Documentation=20auto-update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/utils/README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/packages/utils/README.md b/packages/utils/README.md index 98e435518..829b8a6e9 100644 --- a/packages/utils/README.md +++ b/packages/utils/README.md @@ -61,6 +61,7 @@ npm i @dfinity/agent @dfinity/candid @dfinity/principal - [toNullable](#gear-tonullable) - [fromNullable](#gear-fromnullable) - [fromDefinedNullable](#gear-fromdefinednullable) +- [fromNullishNullable](#gear-fromnullishnullable) - [jsonReplacer](#gear-jsonreplacer) - [jsonReviver](#gear-jsonreviver) - [principalToSubAccount](#gear-principaltosubaccount) @@ -372,6 +373,20 @@ Returns the current timestamp in nanoseconds as a `bigint`. [:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/utils/did.utils.ts#L12) +#### :gear: fromNullishNullable + +Extracts the value from a nullish Candid-style variant representation. + +| Function | Type | +| --------------------- | ------------------------------------------------------ | +| `fromNullishNullable` | `(value: [] or [T] or undefined) => T or undefined` | + +Parameters: + +- `value`: - A Candid-style variant or `undefined`. + +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/utils/did.utils.ts#L27) + #### :gear: jsonReplacer A parser that interprets revived BigInt, Principal, and Uint8Array when constructing JavaScript values or objects. From 3c318bf53ab214eec421d1a481c1d8fc5a29714d Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 20 Jan 2025 08:58:27 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=A4=96=20Documentation=20auto-update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/utils/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/README.md b/packages/utils/README.md index 125e716c8..3e5e680be 100644 --- a/packages/utils/README.md +++ b/packages/utils/README.md @@ -404,7 +404,7 @@ Parameters: - `value`: - A Candid-style variant or `undefined`. -[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/utils/did.utils.ts#L27) +[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/utils/src/utils/did.utils.ts#L50) #### :gear: jsonReplacer