-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Motivation In Juno, I often encounter a pattern where an optional Candid variant of an optional Candid variant needs to be transformed into a nullish value. To simplify this, I introduced a utility ([PR](junobuild/juno#1068)) to shorten the pattern since each `fromNullable` of an `undefined` value requires the chaining operation `?? []`. I took a quick look at the NNS dapp and OISY, and it seems to follow a similar pattern. Therefore, I considered adding the utility to `@dfinity/utils`. # Notes Not against better naming? # Changes - Implement `fromNullishNullable` - Extract reused test data and add two blocks `describe` (in addition to the test for the new function) --------- Signed-off-by: David Dal Busco <[email protected]> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
a2f74ea
commit 90d7241
Showing
4 changed files
with
83 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,79 @@ | ||
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); | ||
}); | ||
|
||
it("should throw on undefined", () => { | ||
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters