Skip to content

Commit cf3d666

Browse files
committed
improve README.
1 parent 337a878 commit cf3d666

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Optional (like Java) implementation in TypeScript
1212
`Optional<T>` is a type which *may* or *may not* contain a *payload* of type `T`.
1313
It provides a common interface regardless of whether an instance is *present* or is *empty*.
1414

15-
This module is inspired by [Optional class in Java 8](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html).
15+
This module is inspired by [Optional class in Java 8+](https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html).
1616

1717
### Install
1818

@@ -45,7 +45,7 @@ let optionalEmpty2 = Optional.empty<string>(); // or parameterize explicitly
4545
### operations
4646

4747
```ts
48-
let optional: Optional<string> = Optional.ofNullable( /* some optional value */ );
48+
let optional: Optional<string> = Optional.ofNullable( /* some optional value: null | string */ );
4949

5050
// force to retrieve the payload. (or else throws TypeError.)
5151
// this method is not used match.
@@ -57,9 +57,12 @@ optional.isPresent
5757
// be whether this is empty or not. (negation of `isPresent` property)
5858
optional.isEmpty
5959

60-
// if a payload is present, execute the given procedure.
60+
// execute the given consumer if a payload is present.
6161
optional.ifPresent(value => console.log(value));
6262

63+
// execute the first argument (consumer) if a payload is present, execute the second argument (emptyAction) otherwise.
64+
optional.ifPresentOrElse(value => console.log(value), () => console.log("empty"));
65+
6366
// filter a payload with additional predicate.
6467
optional.filter(value => value.length > 0);
6568

@@ -69,16 +72,20 @@ optional.map(value => value.length);
6972
// map a payload with the given mapper which returns value wrapped with Optional type.
7073
let powerIfPositive: (x: Number) => Optional<Number>
7174
= x => (x > 0) ? Optional.ofNonNull(x * x) : Optional.empty();
75+
let numberOptional: Optional<number> = Optional.ofNullable(/* some optional value: null | number */)
76+
numberOptional.flatMap(value => powerIfPositive(value));
7277

73-
optional.flatMap(value => powerIfPositive(value));
78+
// return this if this is present, return the given another optional otherwise.
79+
let another: Optional<string> = Optional.ofNullable(/* ... */);
80+
optional.or(another);
7481

75-
// retrieve a payload if this is present, return the given value or else.
82+
// retrieve a payload if this is present, return the given value otherwise.
7683
optional.orElse("bar");
7784

78-
// retrieve a payload if this is present, return a value supplied by the given function or else.
85+
// retrieve a payload if this is present, return a value supplied by the given function otherwise.
7986
optional.orElseGet(() => "bar");
8087

81-
// retrieve a payload if this is present, throws an exception supplied by the given function or else.
88+
// retrieve a payload if this is present, throws an exception supplied by the given function otherwise.
8289
optional.orElseThrow(() => new Error());
8390
```
8491

0 commit comments

Comments
 (0)