Skip to content

Commit e729a67

Browse files
committed
add document.
1 parent a04c2ae commit e729a67

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ 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/10/docs/api/java/util/Optional.html).
16+
17+
The following methods are currently not supported:
18+
19+
- `equals`
20+
- `toString`
21+
- `hashCode`
22+
- `stream`
1623

1724
### Install
1825

lib/index.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
import { Option, Cases } from "./types";
22

3+
/**
4+
* `Optional` (like Java) implementation in TypeScript.
5+
*
6+
* `Optional<T>` is a type which *may* or *may not* contain a *payload* of type `T`.
7+
* It provides a common interface regardless of whether an instance is *present* or is *empty*.
8+
*
9+
* This module is inspired by [Optional class in Java 8+](https://docs.oracle.com/javase/10/docs/api/java/util/Optional.html).
10+
*
11+
* The following methods are currently not supported:
12+
*
13+
* - `equals`
14+
* - `toString`
15+
* - `hashCode`
16+
* - `stream`
17+
*/
318
export default abstract class Optional<T> {
419
/**
520
* Returns `true` if this is present, otherwise `false`.
@@ -95,12 +110,30 @@ export default abstract class Optional<T> {
95110
*/
96111
abstract orElseThrow<U>(errorSupplier: () => U): T;
97112

113+
/**
114+
* Converts this to an `Option`.
115+
*/
98116
abstract toOption(): Option<T>;
99117

118+
/**
119+
* If a payload is present, returns the payload,
120+
* otherwise returns `null`.
121+
*/
100122
abstract orNull(): T | null;
101123

124+
/**
125+
* If a payload is present, returns the payload,
126+
* otherwise returns `undefined`.
127+
*/
102128
abstract orUndefined(): T | undefined;
103129

130+
/**
131+
* Returns an appropriate result by emulating pattern matching with the given `cases`.
132+
* If a payload is present, returns the result of `present` case,
133+
* otherwise returns the result of `empty` case.
134+
*
135+
* @param cases cases for this `Optional`
136+
*/
104137
abstract matches<U>(cases: Cases<T, U>): U;
105138

106139
/**
@@ -147,6 +180,12 @@ export default abstract class Optional<T> {
147180
return new EmptyOptional();
148181
}
149182

183+
/**
184+
* Retrieve the given `option` as an Optional.
185+
*
186+
* @param option an `Option` object to retrieve
187+
* @throws {TypeError} when the given `option` does not have a valid `kind` attribute.
188+
*/
150189
static from<T>(option: Option<T>): Optional<T> {
151190
switch (option.kind) {
152191
case "present": return Optional.of(option.value);

lib/types.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1+
/**
2+
* An interface that represents respective cases of pattern matching of `Optional`.
3+
*/
14
export interface Cases<T, U> {
2-
present: (value: T) => U
3-
empty: () => U
5+
/**
6+
* A mapper that maps a payload for case of an `Optional` is present.
7+
*/
8+
present: (value: T) => U;
9+
10+
/**
11+
* A supplier that provides a value for case of an `Optional` is empty.
12+
*/
13+
empty: () => U;
414
}
515

16+
/**
17+
* An alias of algebraic, prototype-free JavaScript object which represents `Optional`.
18+
* Objects of this type are provided by `Optional.toOption`
19+
* and they can be retrieved as `Optional` by `Optional.from`.
20+
*/
621
export type Option<T> = Present<T> | Empty<T>
722

823
export interface Present<T> {

0 commit comments

Comments
 (0)