Skip to content

Commit

Permalink
add static method Optional.of as a superset of Optional in Java 8+. O…
Browse files Browse the repository at this point in the history
…ptional.ofNonNull is now an alias of Optional.of. (1.6.0)
  • Loading branch information
bromne committed Apr 27, 2018
1 parent 8707159 commit 4a35bb9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export default abstract class Optional<T> {
}

static ofNonNull<T>(payload: T): Optional<T> {
return Optional.of(payload);
}

static of<T>(payload: T): Optional<T> {
if (payload !== null && payload !== undefined)
return new PresentOptional<T>(payload);
else
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typescript-optional",
"version": "1.5.0",
"version": "1.6.0",
"description": "Optional (like Java) implementation in TypeScript",
"repository": {
"type": "git",
Expand Down
17 changes: 16 additions & 1 deletion test/OptionalTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("Optional", () => {
});
});

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

describe("#of", () => {
it("returns a present optional when it is given a non-null value.", () => {
let sut = Optional.of("foo");
assert(sut.isPresent);
});

it("throws an exception when it is given null.", () => {
assert.throws(() => Optional.of<string | null>(null))
});

it("throws an exception when it is given undefined.", () => {
assert.throws(() => Optional.of<string | undefined>(undefined))
});
});

describe("#empty", () => {
it("returns an empty optional.", () => {
let sut: Optional<string> = Optional.empty();
Expand Down

0 comments on commit 4a35bb9

Please sign in to comment.