Skip to content

Commit 4a35bb9

Browse files
committed
add static method Optional.of as a superset of Optional in Java 8+. Optional.ofNonNull is now an alias of Optional.of. (1.6.0)
1 parent 8707159 commit 4a35bb9

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

lib/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export default abstract class Optional<T> {
3333
}
3434

3535
static ofNonNull<T>(payload: T): Optional<T> {
36+
return Optional.of(payload);
37+
}
38+
39+
static of<T>(payload: T): Optional<T> {
3640
if (payload !== null && payload !== undefined)
3741
return new PresentOptional<T>(payload);
3842
else

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typescript-optional",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"description": "Optional (like Java) implementation in TypeScript",
55
"repository": {
66
"type": "git",

test/OptionalTest.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe("Optional", () => {
2727
});
2828
});
2929

30-
describe("#ofNonNull", () => {
30+
describe("#of", () => {
3131
it("returns a present optional when it is given a non-null value.", () => {
3232
let sut = Optional.ofNonNull("foo");
3333
assert(sut.isPresent);
@@ -42,6 +42,21 @@ describe("Optional", () => {
4242
});
4343
});
4444

45+
describe("#of", () => {
46+
it("returns a present optional when it is given a non-null value.", () => {
47+
let sut = Optional.of("foo");
48+
assert(sut.isPresent);
49+
});
50+
51+
it("throws an exception when it is given null.", () => {
52+
assert.throws(() => Optional.of<string | null>(null))
53+
});
54+
55+
it("throws an exception when it is given undefined.", () => {
56+
assert.throws(() => Optional.of<string | undefined>(undefined))
57+
});
58+
});
59+
4560
describe("#empty", () => {
4661
it("returns an empty optional.", () => {
4762
let sut: Optional<string> = Optional.empty();

0 commit comments

Comments
 (0)