@ekz/option 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -232
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -1,242 +1,23 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @ekz/option
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Option/Maybe type for TypeScript — a port of Scala's `Option`.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
**Documentation:** [https://docs.ekz.io/option/](https://docs.ekz.io/option/)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Install
|
|
8
8
|
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
let valueA = Option.of(null).map(x => x + 2).getOrElse(() => 0);
|
|
13
|
-
// valueA = 0
|
|
14
|
-
|
|
15
|
-
let valueB = Some('any object').flatMap(x => Some(`${x}!!!`)).getOrUndefined();
|
|
16
|
-
// valueB = 'any object!!!'
|
|
17
|
-
|
|
18
|
-
let valueC = Some(null).flatMap(() => Option.of(2)).map(x => x * 3).getOrElse(() => 0);
|
|
19
|
-
// valueC = 6
|
|
20
|
-
|
|
21
|
-
let valueD = Some(1).mapNullable(() => null).getOrReturn(-1);
|
|
22
|
-
// valueD = -1
|
|
23
|
-
|
|
24
|
-
Some(1).equals(Some(1))
|
|
25
|
-
// true
|
|
26
|
-
|
|
27
|
-
None.equals(None)
|
|
28
|
-
// true
|
|
29
|
-
|
|
30
|
-
Some('abc').equals(None)
|
|
31
|
-
// false
|
|
32
|
-
|
|
33
|
-
None.isDefined; // false
|
|
34
|
-
Some(1).isDefined; // true
|
|
35
|
-
Some(1).isEmpty; // false
|
|
36
|
-
Some(null).isEmpty; // false! Option<null> is valid!
|
|
37
|
-
|
|
38
|
-
Some('foo').get(); // 'foo'
|
|
39
|
-
None.get(); // throws an error, use getOrElse or getOrUndefined instead
|
|
40
|
-
|
|
41
|
-
Some(3).filter(x => x % 2 === 0); // None
|
|
42
|
-
|
|
43
|
-
Some('bar').forEach(x => {
|
|
44
|
-
console.log(`my value: ${x}`);
|
|
45
|
-
});
|
|
9
|
+
```bash
|
|
10
|
+
npm install @ekz/option
|
|
46
11
|
```
|
|
47
12
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
|
|
51
|
-
|
|
52
|
-
### Table of Contents
|
|
53
|
-
|
|
54
|
-
* [Option](#option)
|
|
55
|
-
* [Parameters](#parameters)
|
|
56
|
-
* [get](#get)
|
|
57
|
-
* [isEmpty](#isempty)
|
|
58
|
-
* [isDefined](#isdefined)
|
|
59
|
-
* [map](#map)
|
|
60
|
-
* [Parameters](#parameters-1)
|
|
61
|
-
* [mapNullable](#mapnullable)
|
|
62
|
-
* [Parameters](#parameters-2)
|
|
63
|
-
* [flatMap](#flatmap)
|
|
64
|
-
* [Parameters](#parameters-3)
|
|
65
|
-
* [forEach](#foreach)
|
|
66
|
-
* [Parameters](#parameters-4)
|
|
67
|
-
* [filter](#filter)
|
|
68
|
-
* [Parameters](#parameters-5)
|
|
69
|
-
* [getOrElse](#getorelse)
|
|
70
|
-
* [Parameters](#parameters-6)
|
|
71
|
-
* [getOrReturn](#getorreturn)
|
|
72
|
-
* [Parameters](#parameters-7)
|
|
73
|
-
* [getOrUndefined](#getorundefined)
|
|
74
|
-
* [equals](#equals)
|
|
75
|
-
* [Parameters](#parameters-8)
|
|
76
|
-
* [toJSON](#tojson)
|
|
77
|
-
* [of](#of)
|
|
78
|
-
* [Parameters](#parameters-9)
|
|
79
|
-
* [None](#none)
|
|
80
|
-
* [Some](#some)
|
|
81
|
-
* [None](#none-1)
|
|
82
|
-
* [Some](#some-1)
|
|
83
|
-
* [Parameters](#parameters-10)
|
|
84
|
-
|
|
85
|
-
## Option
|
|
86
|
-
|
|
87
|
-
Represents optional values. Instances of Option are either an instance of Some or the object None.
|
|
88
|
-
|
|
89
|
-
### Parameters
|
|
90
|
-
|
|
91
|
-
* `$privateToken` **any** 
|
|
92
|
-
|
|
93
|
-
### get
|
|
94
|
-
|
|
95
|
-
Returns the option's value.
|
|
96
|
-
|
|
97
|
-
Type: function (): A
|
|
98
|
-
|
|
99
|
-
### isEmpty
|
|
100
|
-
|
|
101
|
-
Returns true if the option is None, false otherwise.
|
|
102
|
-
|
|
103
|
-
Type: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)
|
|
104
|
-
|
|
105
|
-
### isDefined
|
|
106
|
-
|
|
107
|
-
Returns true if the option is an instance of Some, false otherwise.
|
|
108
|
-
|
|
109
|
-
Type: [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)
|
|
110
|
-
|
|
111
|
-
Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 
|
|
112
|
-
|
|
113
|
-
### map
|
|
114
|
-
|
|
115
|
-
Returns a Some containing the result of applying f to this Option's value if this Option is nonempty.
|
|
116
|
-
|
|
117
|
-
#### Parameters
|
|
118
|
-
|
|
119
|
-
* `f` **function (A): B** 
|
|
120
|
-
|
|
121
|
-
Returns **[Option](#option)\<B>** 
|
|
122
|
-
|
|
123
|
-
### mapNullable
|
|
124
|
-
|
|
125
|
-
Like map, but if resulting value is null, then returns None.
|
|
126
|
-
|
|
127
|
-
#### Parameters
|
|
128
|
-
|
|
129
|
-
* `f` **function (A): B?** 
|
|
130
|
-
|
|
131
|
-
Returns **[Option](#option)\<B>** 
|
|
132
|
-
|
|
133
|
-
### flatMap
|
|
134
|
-
|
|
135
|
-
Returns the result of applying f to this Option's value if this Option is nonempty. Returns None if this Option is empty. Slightly different from map in that f is expected to return an Option (which could be None).
|
|
136
|
-
|
|
137
|
-
#### Parameters
|
|
138
|
-
|
|
139
|
-
* `f` **function (A): [Option](#option)\<B>** 
|
|
140
|
-
|
|
141
|
-
Returns **[Option](#option)\<B>** 
|
|
142
|
-
|
|
143
|
-
### forEach
|
|
144
|
-
|
|
145
|
-
Apply the given procedure f to the option's value, if it is nonempty.
|
|
13
|
+
## Quick example
|
|
146
14
|
|
|
147
|
-
|
|
15
|
+
```typescript
|
|
16
|
+
import { Option, Some, None } from '@ekz/option';
|
|
148
17
|
|
|
149
|
-
|
|
18
|
+
const label = Option.of(maybeName).map((n) => n.trim()).getOrElse(() => 'Anonymous');
|
|
150
19
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
### filter
|
|
154
|
-
|
|
155
|
-
Returns this Option if it is nonempty and applying the predicate to this Option's value returns true.
|
|
156
|
-
|
|
157
|
-
#### Parameters
|
|
158
|
-
|
|
159
|
-
* `predicate` **function (A): [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 
|
|
160
|
-
|
|
161
|
-
Returns **[Option](#option)\<A>** 
|
|
162
|
-
|
|
163
|
-
### getOrElse
|
|
164
|
-
|
|
165
|
-
Returns the option's value if the option is nonempty, otherwise return the result of evaluating other.
|
|
166
|
-
|
|
167
|
-
#### Parameters
|
|
168
|
-
|
|
169
|
-
* `other` **function (): B** 
|
|
170
|
-
|
|
171
|
-
Returns **(A | B)** 
|
|
172
|
-
|
|
173
|
-
### getOrReturn
|
|
174
|
-
|
|
175
|
-
Returns the option's value if the option is nonempty, otherwise return other.
|
|
176
|
-
|
|
177
|
-
#### Parameters
|
|
178
|
-
|
|
179
|
-
* `other` **B** 
|
|
180
|
-
|
|
181
|
-
Returns **(A | B)** 
|
|
182
|
-
|
|
183
|
-
### getOrUndefined
|
|
184
|
-
|
|
185
|
-
Returns the option's value if the option is nonempty, otherwise returns undefined.
|
|
186
|
-
|
|
187
|
-
Returns **(A | void)** 
|
|
188
|
-
|
|
189
|
-
### equals
|
|
190
|
-
|
|
191
|
-
Compares the option's value with other option's value and returns true when they match. None always matches other None.
|
|
192
|
-
|
|
193
|
-
#### Parameters
|
|
194
|
-
|
|
195
|
-
* `other` **[Option](#option)\<B>** 
|
|
196
|
-
|
|
197
|
-
Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** 
|
|
198
|
-
|
|
199
|
-
### toJSON
|
|
200
|
-
|
|
201
|
-
Returns value if present, null otherwise. If value contains a method `toJSON`,
|
|
202
|
-
returns the result of method call.
|
|
203
|
-
|
|
204
|
-
Returns **any** 
|
|
205
|
-
|
|
206
|
-
### of
|
|
207
|
-
|
|
208
|
-
An Option factory which creates Some(x) if the argument is not null, and None if it is null.
|
|
209
|
-
|
|
210
|
-
#### Parameters
|
|
211
|
-
|
|
212
|
-
* `value` **V??** 
|
|
213
|
-
|
|
214
|
-
Returns **[Option](#option)\<V>** 
|
|
215
|
-
|
|
216
|
-
### None
|
|
217
|
-
|
|
218
|
-
The empty None object
|
|
219
|
-
|
|
220
|
-
Type: [Option](#option)\<any>
|
|
221
|
-
|
|
222
|
-
### Some
|
|
223
|
-
|
|
224
|
-
Creates Some(x). Note that Some(null) is valid.
|
|
225
|
-
|
|
226
|
-
Type: function (value: A): [Option](#option)\<A>
|
|
227
|
-
|
|
228
|
-
## None
|
|
229
|
-
|
|
230
|
-
The empty None object.
|
|
231
|
-
|
|
232
|
-
Type: [Option](#option)\<any>
|
|
233
|
-
|
|
234
|
-
## Some
|
|
235
|
-
|
|
236
|
-
Creates Some(x). Note that Some(null) is valid.
|
|
237
|
-
|
|
238
|
-
### Parameters
|
|
239
|
-
|
|
240
|
-
* `value` **A** 
|
|
20
|
+
Some(1).flatMap((n) => (n > 0 ? Some(n) : None)); // Some(1)
|
|
21
|
+
```
|
|
241
22
|
|
|
242
|
-
|
|
23
|
+
See the [docs](https://docs.ekz.io/option/) for constructors, transformations, and safe unwrapping.
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ekz/option",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Option/Maybe type for TypeScript",
|
|
6
|
+
"homepage": "https://docs.ekz.io/option/",
|
|
6
7
|
"repository": {
|
|
7
8
|
"type": "git",
|
|
8
9
|
"url": "https://github.com/erkez/ekz.git",
|
|
@@ -29,11 +30,11 @@
|
|
|
29
30
|
],
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"npm-run-all": "^4.1.5",
|
|
32
|
-
"typescript": "^
|
|
33
|
+
"typescript": "^6.0.3"
|
|
33
34
|
},
|
|
34
35
|
"scripts": {
|
|
35
36
|
"clean": "rm -rf lib/* || true",
|
|
36
|
-
"lint": "npx eslint
|
|
37
|
+
"lint": "npx eslint ./src/main/web",
|
|
37
38
|
"lint:fix": "yarn lint --fix",
|
|
38
39
|
"compile": "run-p \"compile:*\"",
|
|
39
40
|
"compile:esm": "tsc -p ./src/main/web --outDir ./lib/esm",
|