effect 3.12.7 → 3.12.8
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/dist/cjs/BigDecimal.js +1 -1
- package/dist/cjs/BigDecimal.js.map +1 -1
- package/dist/cjs/Cause.js +630 -167
- package/dist/cjs/Cause.js.map +1 -1
- package/dist/cjs/Chunk.js +4 -8
- package/dist/cjs/Chunk.js.map +1 -1
- package/dist/cjs/ConfigProvider.js +1 -0
- package/dist/cjs/ConfigProvider.js.map +1 -1
- package/dist/cjs/Cron.js +26 -10
- package/dist/cjs/Cron.js.map +1 -1
- package/dist/cjs/Duration.js +7 -4
- package/dist/cjs/Duration.js.map +1 -1
- package/dist/cjs/Effect.js.map +1 -1
- package/dist/cjs/Either.js +5 -24
- package/dist/cjs/Either.js.map +1 -1
- package/dist/cjs/Layer.js.map +1 -1
- package/dist/cjs/Option.js +1042 -363
- package/dist/cjs/Option.js.map +1 -1
- package/dist/cjs/ParseResult.js +41 -18
- package/dist/cjs/ParseResult.js.map +1 -1
- package/dist/cjs/Schedule.js +8 -1
- package/dist/cjs/Schedule.js.map +1 -1
- package/dist/cjs/Schema.js +51 -10
- package/dist/cjs/Schema.js.map +1 -1
- package/dist/cjs/internal/cause.js +23 -108
- package/dist/cjs/internal/cause.js.map +1 -1
- package/dist/cjs/internal/core.js +4 -5
- package/dist/cjs/internal/core.js.map +1 -1
- package/dist/cjs/internal/layer.js.map +1 -1
- package/dist/cjs/internal/schedule.js.map +1 -1
- package/dist/cjs/internal/secret.js +6 -5
- package/dist/cjs/internal/secret.js.map +1 -1
- package/dist/cjs/internal/stream.js +21 -23
- package/dist/cjs/internal/stream.js.map +1 -1
- package/dist/cjs/internal/version.js +1 -1
- package/dist/dts/BigDecimal.d.ts.map +1 -1
- package/dist/dts/Cause.d.ts +1157 -304
- package/dist/dts/Cause.d.ts.map +1 -1
- package/dist/dts/Chunk.d.ts +3 -6
- package/dist/dts/Chunk.d.ts.map +1 -1
- package/dist/dts/ConfigProvider.d.ts.map +1 -1
- package/dist/dts/Cron.d.ts +25 -9
- package/dist/dts/Cron.d.ts.map +1 -1
- package/dist/dts/Duration.d.ts +1 -1
- package/dist/dts/Duration.d.ts.map +1 -1
- package/dist/dts/Effect.d.ts +3 -2
- package/dist/dts/Effect.d.ts.map +1 -1
- package/dist/dts/Either.d.ts.map +1 -1
- package/dist/dts/Layer.d.ts +1 -1
- package/dist/dts/Layer.d.ts.map +1 -1
- package/dist/dts/Option.d.ts +2288 -784
- package/dist/dts/Option.d.ts.map +1 -1
- package/dist/dts/ParseResult.d.ts +4 -4
- package/dist/dts/ParseResult.d.ts.map +1 -1
- package/dist/dts/Schedule.d.ts +7 -0
- package/dist/dts/Schedule.d.ts.map +1 -1
- package/dist/dts/Schema.d.ts +15 -4
- package/dist/dts/Schema.d.ts.map +1 -1
- package/dist/dts/internal/core.d.ts.map +1 -1
- package/dist/dts/internal/stream.d.ts +1 -1
- package/dist/dts/internal/stream.d.ts.map +1 -1
- package/dist/esm/BigDecimal.js +1 -1
- package/dist/esm/BigDecimal.js.map +1 -1
- package/dist/esm/Cause.js +630 -167
- package/dist/esm/Cause.js.map +1 -1
- package/dist/esm/Chunk.js +4 -8
- package/dist/esm/Chunk.js.map +1 -1
- package/dist/esm/ConfigProvider.js +1 -0
- package/dist/esm/ConfigProvider.js.map +1 -1
- package/dist/esm/Cron.js +27 -11
- package/dist/esm/Cron.js.map +1 -1
- package/dist/esm/Duration.js +7 -4
- package/dist/esm/Duration.js.map +1 -1
- package/dist/esm/Effect.js.map +1 -1
- package/dist/esm/Either.js +5 -24
- package/dist/esm/Either.js.map +1 -1
- package/dist/esm/Layer.js.map +1 -1
- package/dist/esm/Option.js +1041 -363
- package/dist/esm/Option.js.map +1 -1
- package/dist/esm/ParseResult.js +41 -18
- package/dist/esm/ParseResult.js.map +1 -1
- package/dist/esm/Schedule.js +7 -0
- package/dist/esm/Schedule.js.map +1 -1
- package/dist/esm/Schema.js +51 -10
- package/dist/esm/Schema.js.map +1 -1
- package/dist/esm/internal/cause.js +22 -108
- package/dist/esm/internal/cause.js.map +1 -1
- package/dist/esm/internal/core.js +4 -5
- package/dist/esm/internal/core.js.map +1 -1
- package/dist/esm/internal/layer.js.map +1 -1
- package/dist/esm/internal/schedule.js.map +1 -1
- package/dist/esm/internal/secret.js +5 -4
- package/dist/esm/internal/secret.js.map +1 -1
- package/dist/esm/internal/stream.js +21 -23
- package/dist/esm/internal/stream.js.map +1 -1
- package/dist/esm/internal/version.js +1 -1
- package/package.json +1 -1
- package/src/BigDecimal.ts +1 -1
- package/src/Cause.ts +1158 -304
- package/src/Chunk.ts +8 -11
- package/src/ConfigProvider.ts +1 -0
- package/src/Cron.ts +28 -11
- package/src/Duration.ts +10 -5
- package/src/Effect.ts +5 -2
- package/src/Either.ts +11 -28
- package/src/Layer.ts +1 -1
- package/src/Option.ts +2314 -820
- package/src/ParseResult.ts +53 -29
- package/src/Schedule.ts +8 -0
- package/src/Schema.ts +68 -12
- package/src/internal/cause.ts +38 -137
- package/src/internal/core.ts +4 -5
- package/src/internal/layer.ts +1 -1
- package/src/internal/schedule.ts +7 -6
- package/src/internal/secret.ts +6 -4
- package/src/internal/stream.ts +24 -37
- package/src/internal/version.ts +1 -1
package/src/Option.ts
CHANGED
|
@@ -20,25 +20,39 @@ import type * as Unify from "./Unify.js"
|
|
|
20
20
|
import * as Gen from "./Utils.js"
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
23
|
+
* The `Option` data type represents optional values. An `Option<A>` can either
|
|
24
|
+
* be `Some<A>`, containing a value of type `A`, or `None`, representing the
|
|
25
|
+
* absence of a value.
|
|
26
|
+
*
|
|
27
|
+
* **When to Use**
|
|
28
|
+
*
|
|
29
|
+
* You can use `Option` in scenarios like:
|
|
30
|
+
*
|
|
31
|
+
* - Using it for initial values
|
|
32
|
+
* - Returning values from functions that are not defined for all possible
|
|
33
|
+
* inputs (referred to as “partial functions”)
|
|
34
|
+
* - Managing optional fields in data structures
|
|
35
|
+
* - Handling optional function arguments
|
|
36
|
+
*
|
|
37
|
+
* @category Models
|
|
24
38
|
* @since 2.0.0
|
|
25
39
|
*/
|
|
26
40
|
export type Option<A> = None<A> | Some<A>
|
|
27
41
|
|
|
28
42
|
/**
|
|
29
|
-
* @category
|
|
43
|
+
* @category Symbols
|
|
30
44
|
* @since 2.0.0
|
|
31
45
|
*/
|
|
32
46
|
export const TypeId: unique symbol = Symbol.for("effect/Option")
|
|
33
47
|
|
|
34
48
|
/**
|
|
35
|
-
* @category
|
|
49
|
+
* @category Symbols
|
|
36
50
|
* @since 2.0.0
|
|
37
51
|
*/
|
|
38
52
|
export type TypeId = typeof TypeId
|
|
39
53
|
|
|
40
54
|
/**
|
|
41
|
-
* @category
|
|
55
|
+
* @category Models
|
|
42
56
|
* @since 2.0.0
|
|
43
57
|
*/
|
|
44
58
|
export interface None<out A> extends Pipeable, Inspectable {
|
|
@@ -53,7 +67,7 @@ export interface None<out A> extends Pipeable, Inspectable {
|
|
|
53
67
|
}
|
|
54
68
|
|
|
55
69
|
/**
|
|
56
|
-
* @category
|
|
70
|
+
* @category Models
|
|
57
71
|
* @since 2.0.0
|
|
58
72
|
*/
|
|
59
73
|
export interface Some<out A> extends Pipeable, Inspectable {
|
|
@@ -69,7 +83,7 @@ export interface Some<out A> extends Pipeable, Inspectable {
|
|
|
69
83
|
}
|
|
70
84
|
|
|
71
85
|
/**
|
|
72
|
-
* @category
|
|
86
|
+
* @category Models
|
|
73
87
|
* @since 2.0.0
|
|
74
88
|
*/
|
|
75
89
|
export interface OptionUnify<A extends { [Unify.typeSymbol]?: any }> {
|
|
@@ -81,20 +95,37 @@ export interface OptionUnify<A extends { [Unify.typeSymbol]?: any }> {
|
|
|
81
95
|
*/
|
|
82
96
|
export declare namespace Option {
|
|
83
97
|
/**
|
|
98
|
+
* Extracts the type of the value contained in an `Option`.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* ```ts
|
|
102
|
+
* // Title: Getting the Value Type of an Option
|
|
103
|
+
* import { Option } from "effect"
|
|
104
|
+
*
|
|
105
|
+
* // Declare an Option holding a string
|
|
106
|
+
* declare const myOption: Option.Option<string>
|
|
107
|
+
*
|
|
108
|
+
* // Extract the type of the value within the Option
|
|
109
|
+
* //
|
|
110
|
+
* // ┌─── string
|
|
111
|
+
* // ▼
|
|
112
|
+
* type MyType = Option.Option.Value<typeof myOption>
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
84
115
|
* @since 2.0.0
|
|
85
|
-
* @category
|
|
116
|
+
* @category Type-level Utils
|
|
86
117
|
*/
|
|
87
118
|
export type Value<T extends Option<any>> = [T] extends [Option<infer _A>] ? _A : never
|
|
88
119
|
}
|
|
89
120
|
|
|
90
121
|
/**
|
|
91
|
-
* @category
|
|
122
|
+
* @category Models
|
|
92
123
|
* @since 2.0.0
|
|
93
124
|
*/
|
|
94
125
|
export interface OptionUnifyIgnore {}
|
|
95
126
|
|
|
96
127
|
/**
|
|
97
|
-
* @category
|
|
128
|
+
* @category Type Lambdas
|
|
98
129
|
* @since 2.0.0
|
|
99
130
|
*/
|
|
100
131
|
export interface OptionTypeLambda extends TypeLambda {
|
|
@@ -102,129 +133,207 @@ export interface OptionTypeLambda extends TypeLambda {
|
|
|
102
133
|
}
|
|
103
134
|
|
|
104
135
|
/**
|
|
105
|
-
*
|
|
136
|
+
* Represents the absence of a value by creating an empty `Option`.
|
|
137
|
+
*
|
|
138
|
+
* `Option.none` returns an `Option<never>`, which is a subtype of `Option<A>`.
|
|
139
|
+
* This means you can use it in place of any `Option<A>` regardless of the type
|
|
140
|
+
* `A`.
|
|
106
141
|
*
|
|
107
|
-
* @
|
|
142
|
+
* @see {@link some} for the opposite operation.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* // Title: Creating an Option with No Value
|
|
147
|
+
* import { Option } from "effect"
|
|
148
|
+
*
|
|
149
|
+
* // An Option holding no value
|
|
150
|
+
* //
|
|
151
|
+
* // ┌─── Option<never>
|
|
152
|
+
* // ▼
|
|
153
|
+
* const noValue = Option.none()
|
|
154
|
+
*
|
|
155
|
+
* console.log(noValue)
|
|
156
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
157
|
+
* ```
|
|
158
|
+
*
|
|
159
|
+
* @category Constructors
|
|
108
160
|
* @since 2.0.0
|
|
109
161
|
*/
|
|
110
162
|
export const none = <A = never>(): Option<A> => option.none
|
|
111
163
|
|
|
112
164
|
/**
|
|
113
|
-
*
|
|
165
|
+
* Wraps the given value into an `Option` to represent its presence.
|
|
166
|
+
*
|
|
167
|
+
* @see {@link none} for the opposite operation.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* // Title: Creating an Option with a Value
|
|
172
|
+
* import { Option } from "effect"
|
|
173
|
+
*
|
|
174
|
+
* // An Option holding the number 1
|
|
175
|
+
* //
|
|
176
|
+
* // ┌─── Option<number>
|
|
177
|
+
* // ▼
|
|
178
|
+
* const value = Option.some(1)
|
|
114
179
|
*
|
|
115
|
-
*
|
|
180
|
+
* console.log(value)
|
|
181
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
182
|
+
* ```
|
|
116
183
|
*
|
|
117
|
-
* @category
|
|
184
|
+
* @category Constructors
|
|
118
185
|
* @since 2.0.0
|
|
119
186
|
*/
|
|
120
187
|
export const some: <A>(value: A) => Option<A> = option.some
|
|
121
188
|
|
|
122
189
|
/**
|
|
123
|
-
*
|
|
190
|
+
* Determines whether the given value is an `Option`.
|
|
191
|
+
*
|
|
192
|
+
* **Details**
|
|
124
193
|
*
|
|
125
|
-
*
|
|
194
|
+
* This function checks if a value is an instance of `Option`. It returns `true`
|
|
195
|
+
* if the value is either `Option.some` or `Option.none`, and `false` otherwise.
|
|
196
|
+
* This is particularly useful when working with unknown values or when you need
|
|
197
|
+
* to ensure type safety in your code.
|
|
126
198
|
*
|
|
127
199
|
* @example
|
|
128
200
|
* ```ts
|
|
129
201
|
* import { Option } from "effect"
|
|
130
202
|
*
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
*
|
|
203
|
+
* console.log(Option.isOption(Option.some(1)))
|
|
204
|
+
* // Output: true
|
|
205
|
+
*
|
|
206
|
+
* console.log(Option.isOption(Option.none()))
|
|
207
|
+
* // Output: true
|
|
208
|
+
*
|
|
209
|
+
* console.log(Option.isOption({}))
|
|
210
|
+
* // Output: false
|
|
134
211
|
* ```
|
|
135
212
|
*
|
|
136
|
-
* @category
|
|
213
|
+
* @category Guards
|
|
137
214
|
* @since 2.0.0
|
|
138
215
|
*/
|
|
139
216
|
export const isOption: (input: unknown) => input is Option<unknown> = option.isOption
|
|
140
217
|
|
|
141
218
|
/**
|
|
142
|
-
*
|
|
219
|
+
* Checks whether an `Option` represents the absence of a value (`None`).
|
|
143
220
|
*
|
|
144
|
-
* @
|
|
221
|
+
* @see {@link isSome} for the opposite check.
|
|
145
222
|
*
|
|
146
223
|
* @example
|
|
147
224
|
* ```ts
|
|
148
225
|
* import { Option } from "effect"
|
|
149
226
|
*
|
|
150
|
-
*
|
|
151
|
-
*
|
|
227
|
+
* console.log(Option.isNone(Option.some(1)))
|
|
228
|
+
* // Output: false
|
|
229
|
+
*
|
|
230
|
+
* console.log(Option.isNone(Option.none()))
|
|
231
|
+
* // Output: true
|
|
152
232
|
* ```
|
|
153
233
|
*
|
|
154
|
-
* @category
|
|
234
|
+
* @category Guards
|
|
155
235
|
* @since 2.0.0
|
|
156
236
|
*/
|
|
157
237
|
export const isNone: <A>(self: Option<A>) => self is None<A> = option.isNone
|
|
158
238
|
|
|
159
239
|
/**
|
|
160
|
-
*
|
|
240
|
+
* Checks whether an `Option` contains a value (`Some`).
|
|
161
241
|
*
|
|
162
|
-
* @
|
|
242
|
+
* @see {@link isNone} for the opposite check.
|
|
163
243
|
*
|
|
164
244
|
* @example
|
|
165
245
|
* ```ts
|
|
166
246
|
* import { Option } from "effect"
|
|
167
247
|
*
|
|
168
|
-
*
|
|
169
|
-
*
|
|
248
|
+
* console.log(Option.isSome(Option.some(1)))
|
|
249
|
+
* // Output: true
|
|
250
|
+
*
|
|
251
|
+
* console.log(Option.isSome(Option.none()))
|
|
252
|
+
* // Output: false
|
|
170
253
|
* ```
|
|
171
254
|
*
|
|
172
|
-
* @category
|
|
255
|
+
* @category Guards
|
|
173
256
|
* @since 2.0.0
|
|
174
257
|
*/
|
|
175
258
|
export const isSome: <A>(self: Option<A>) => self is Some<A> = option.isSome
|
|
176
259
|
|
|
177
260
|
/**
|
|
178
|
-
*
|
|
179
|
-
*
|
|
261
|
+
* Performs pattern matching on an `Option` to handle both `Some` and `None`
|
|
262
|
+
* cases.
|
|
263
|
+
*
|
|
264
|
+
* **Details**
|
|
265
|
+
*
|
|
266
|
+
* This function allows you to match against an `Option` and handle both
|
|
267
|
+
* scenarios: when the `Option` is `None` (i.e., contains no value), and when
|
|
268
|
+
* the `Option` is `Some` (i.e., contains a value). It executes one of the
|
|
269
|
+
* provided functions based on the case:
|
|
180
270
|
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
271
|
+
* - If the `Option` is `None`, the `onNone` function is executed and its result
|
|
272
|
+
* is returned.
|
|
273
|
+
* - If the `Option` is `Some`, the `onSome` function is executed with the
|
|
274
|
+
* contained value, and its result is returned.
|
|
275
|
+
*
|
|
276
|
+
* This function provides a concise and functional way to handle optional values
|
|
277
|
+
* without resorting to `if` or manual checks, making your code more declarative
|
|
278
|
+
* and readable.
|
|
184
279
|
*
|
|
185
280
|
* @example
|
|
186
281
|
* ```ts
|
|
187
|
-
*
|
|
282
|
+
* // Title: Pattern Matching with Option
|
|
283
|
+
* import { Option } from "effect"
|
|
188
284
|
*
|
|
189
|
-
*
|
|
190
|
-
* pipe(Option.some(1), Option.match({ onNone: () => 'a none', onSome: (a) => `a some containing ${a}` })),
|
|
191
|
-
* 'a some containing 1'
|
|
192
|
-
* )
|
|
285
|
+
* const foo = Option.some(1)
|
|
193
286
|
*
|
|
194
|
-
*
|
|
195
|
-
*
|
|
196
|
-
*
|
|
197
|
-
* )
|
|
287
|
+
* const message = Option.match(foo, {
|
|
288
|
+
* onNone: () => "Option is empty",
|
|
289
|
+
* onSome: (value) => `Option has a value: ${value}`
|
|
290
|
+
* })
|
|
291
|
+
*
|
|
292
|
+
* console.log(message)
|
|
293
|
+
* // Output: "Option has a value: 1"
|
|
198
294
|
* ```
|
|
199
295
|
*
|
|
200
|
-
* @category
|
|
296
|
+
* @category Pattern matching
|
|
201
297
|
* @since 2.0.0
|
|
202
298
|
*/
|
|
203
299
|
export const match: {
|
|
204
300
|
/**
|
|
205
|
-
*
|
|
206
|
-
*
|
|
301
|
+
* Performs pattern matching on an `Option` to handle both `Some` and `None`
|
|
302
|
+
* cases.
|
|
303
|
+
*
|
|
304
|
+
* **Details**
|
|
305
|
+
*
|
|
306
|
+
* This function allows you to match against an `Option` and handle both
|
|
307
|
+
* scenarios: when the `Option` is `None` (i.e., contains no value), and when
|
|
308
|
+
* the `Option` is `Some` (i.e., contains a value). It executes one of the
|
|
309
|
+
* provided functions based on the case:
|
|
207
310
|
*
|
|
208
|
-
*
|
|
209
|
-
*
|
|
210
|
-
*
|
|
311
|
+
* - If the `Option` is `None`, the `onNone` function is executed and its result
|
|
312
|
+
* is returned.
|
|
313
|
+
* - If the `Option` is `Some`, the `onSome` function is executed with the
|
|
314
|
+
* contained value, and its result is returned.
|
|
315
|
+
*
|
|
316
|
+
* This function provides a concise and functional way to handle optional values
|
|
317
|
+
* without resorting to `if` or manual checks, making your code more declarative
|
|
318
|
+
* and readable.
|
|
211
319
|
*
|
|
212
320
|
* @example
|
|
213
321
|
* ```ts
|
|
214
|
-
*
|
|
322
|
+
* // Title: Pattern Matching with Option
|
|
323
|
+
* import { Option } from "effect"
|
|
215
324
|
*
|
|
216
|
-
*
|
|
217
|
-
* pipe(Option.some(1), Option.match({ onNone: () => 'a none', onSome: (a) => `a some containing ${a}` })),
|
|
218
|
-
* 'a some containing 1'
|
|
219
|
-
* )
|
|
325
|
+
* const foo = Option.some(1)
|
|
220
326
|
*
|
|
221
|
-
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* )
|
|
327
|
+
* const message = Option.match(foo, {
|
|
328
|
+
* onNone: () => "Option is empty",
|
|
329
|
+
* onSome: (value) => `Option has a value: ${value}`
|
|
330
|
+
* })
|
|
331
|
+
*
|
|
332
|
+
* console.log(message)
|
|
333
|
+
* // Output: "Option has a value: 1"
|
|
225
334
|
* ```
|
|
226
335
|
*
|
|
227
|
-
* @category
|
|
336
|
+
* @category Pattern matching
|
|
228
337
|
* @since 2.0.0
|
|
229
338
|
*/
|
|
230
339
|
<B, A, C = B>(
|
|
@@ -234,29 +343,42 @@ export const match: {
|
|
|
234
343
|
}
|
|
235
344
|
): (self: Option<A>) => B | C
|
|
236
345
|
/**
|
|
237
|
-
*
|
|
238
|
-
*
|
|
346
|
+
* Performs pattern matching on an `Option` to handle both `Some` and `None`
|
|
347
|
+
* cases.
|
|
348
|
+
*
|
|
349
|
+
* **Details**
|
|
350
|
+
*
|
|
351
|
+
* This function allows you to match against an `Option` and handle both
|
|
352
|
+
* scenarios: when the `Option` is `None` (i.e., contains no value), and when
|
|
353
|
+
* the `Option` is `Some` (i.e., contains a value). It executes one of the
|
|
354
|
+
* provided functions based on the case:
|
|
355
|
+
*
|
|
356
|
+
* - If the `Option` is `None`, the `onNone` function is executed and its result
|
|
357
|
+
* is returned.
|
|
358
|
+
* - If the `Option` is `Some`, the `onSome` function is executed with the
|
|
359
|
+
* contained value, and its result is returned.
|
|
239
360
|
*
|
|
240
|
-
*
|
|
241
|
-
*
|
|
242
|
-
*
|
|
361
|
+
* This function provides a concise and functional way to handle optional values
|
|
362
|
+
* without resorting to `if` or manual checks, making your code more declarative
|
|
363
|
+
* and readable.
|
|
243
364
|
*
|
|
244
365
|
* @example
|
|
245
366
|
* ```ts
|
|
246
|
-
*
|
|
367
|
+
* // Title: Pattern Matching with Option
|
|
368
|
+
* import { Option } from "effect"
|
|
247
369
|
*
|
|
248
|
-
*
|
|
249
|
-
* pipe(Option.some(1), Option.match({ onNone: () => 'a none', onSome: (a) => `a some containing ${a}` })),
|
|
250
|
-
* 'a some containing 1'
|
|
251
|
-
* )
|
|
370
|
+
* const foo = Option.some(1)
|
|
252
371
|
*
|
|
253
|
-
*
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
* )
|
|
372
|
+
* const message = Option.match(foo, {
|
|
373
|
+
* onNone: () => "Option is empty",
|
|
374
|
+
* onSome: (value) => `Option has a value: ${value}`
|
|
375
|
+
* })
|
|
376
|
+
*
|
|
377
|
+
* console.log(message)
|
|
378
|
+
* // Output: "Option has a value: 1"
|
|
257
379
|
* ```
|
|
258
380
|
*
|
|
259
|
-
* @category
|
|
381
|
+
* @category Pattern matching
|
|
260
382
|
* @since 2.0.0
|
|
261
383
|
*/
|
|
262
384
|
<A, B, C = B>(
|
|
@@ -275,42 +397,77 @@ export const match: {
|
|
|
275
397
|
)
|
|
276
398
|
|
|
277
399
|
/**
|
|
278
|
-
*
|
|
279
|
-
*
|
|
400
|
+
* Converts an `Option`-returning function into a type guard.
|
|
401
|
+
*
|
|
402
|
+
* **Details**
|
|
403
|
+
*
|
|
404
|
+
* This function transforms a function that returns an `Option` into a type
|
|
405
|
+
* guard, ensuring type safety when validating or narrowing types. The returned
|
|
406
|
+
* type guard function checks whether the input satisfies the condition defined
|
|
407
|
+
* in the original `Option`-returning function.
|
|
408
|
+
*
|
|
409
|
+
* If the original function returns `Option.some`, the type guard evaluates to
|
|
410
|
+
* `true`, confirming the input is of the desired type. If the function returns
|
|
411
|
+
* `Option.none`, the type guard evaluates to `false`.
|
|
412
|
+
*
|
|
413
|
+
* This utility is especially useful for validating types in union types,
|
|
414
|
+
* filtering arrays, or ensuring safe handling of specific subtypes.
|
|
280
415
|
*
|
|
281
416
|
* @example
|
|
282
417
|
* ```ts
|
|
283
418
|
* import { Option } from "effect"
|
|
284
419
|
*
|
|
285
|
-
*
|
|
286
|
-
*
|
|
420
|
+
* type MyData = string | number
|
|
421
|
+
*
|
|
422
|
+
* const parseString = (data: MyData): Option.Option<string> =>
|
|
423
|
+
* typeof data === "string" ? Option.some(data) : Option.none()
|
|
424
|
+
*
|
|
425
|
+
* // ┌─── (a: MyData) => a is string
|
|
426
|
+
* // ▼
|
|
427
|
+
* const isString = Option.toRefinement(parseString)
|
|
287
428
|
*
|
|
288
|
-
*
|
|
429
|
+
* console.log(isString("a"))
|
|
430
|
+
* // Output: true
|
|
289
431
|
*
|
|
290
|
-
*
|
|
291
|
-
*
|
|
432
|
+
* console.log(isString(1))
|
|
433
|
+
* // Output: false
|
|
292
434
|
* ```
|
|
293
435
|
*
|
|
294
|
-
* @category
|
|
436
|
+
* @category Conversions
|
|
295
437
|
* @since 2.0.0
|
|
296
438
|
*/
|
|
297
439
|
export const toRefinement = <A, B extends A>(f: (a: A) => Option<B>): (a: A) => a is B => (a: A): a is B => isSome(f(a))
|
|
298
440
|
|
|
299
441
|
/**
|
|
300
|
-
* Converts an `Iterable`
|
|
301
|
-
*
|
|
442
|
+
* Converts an `Iterable` into an `Option`, wrapping the first element if it
|
|
443
|
+
* exists.
|
|
444
|
+
*
|
|
445
|
+
* **Details**
|
|
446
|
+
*
|
|
447
|
+
* This function takes an `Iterable` (e.g., an array, a generator, or any object
|
|
448
|
+
* implementing the `Iterable` interface) and returns an `Option` based on its
|
|
449
|
+
* content:
|
|
450
|
+
*
|
|
451
|
+
* - If the `Iterable` contains at least one element, the first element is
|
|
452
|
+
* wrapped in a `Some` and returned.
|
|
453
|
+
* - If the `Iterable` is empty, `None` is returned, representing the absence of
|
|
454
|
+
* a value.
|
|
302
455
|
*
|
|
303
|
-
*
|
|
456
|
+
* This utility is useful for safely handling collections that might be empty,
|
|
457
|
+
* ensuring you explicitly handle both cases where a value exists or doesn't.
|
|
304
458
|
*
|
|
305
459
|
* @example
|
|
306
460
|
* ```ts
|
|
307
461
|
* import { Option } from "effect"
|
|
308
462
|
*
|
|
309
|
-
*
|
|
310
|
-
*
|
|
463
|
+
* console.log(Option.fromIterable([1, 2, 3]))
|
|
464
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
465
|
+
*
|
|
466
|
+
* console.log(Option.fromIterable([]))
|
|
467
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
311
468
|
* ```
|
|
312
469
|
*
|
|
313
|
-
* @category
|
|
470
|
+
* @category Constructors
|
|
314
471
|
* @since 2.0.0
|
|
315
472
|
*/
|
|
316
473
|
export const fromIterable = <A>(collection: Iterable<A>): Option<A> => {
|
|
@@ -321,88 +478,174 @@ export const fromIterable = <A>(collection: Iterable<A>): Option<A> => {
|
|
|
321
478
|
}
|
|
322
479
|
|
|
323
480
|
/**
|
|
324
|
-
* Converts
|
|
481
|
+
* Converts an `Either` into an `Option` by discarding the error and extracting
|
|
482
|
+
* the right value.
|
|
483
|
+
*
|
|
484
|
+
* **Details**
|
|
485
|
+
*
|
|
486
|
+
* This function takes an `Either` and returns an `Option` based on its value:
|
|
487
|
+
*
|
|
488
|
+
* - If the `Either` is a `Right`, its value is wrapped in a `Some` and
|
|
489
|
+
* returned.
|
|
490
|
+
* - If the `Either` is a `Left`, the error is discarded, and `None` is
|
|
491
|
+
* returned.
|
|
492
|
+
*
|
|
493
|
+
* This is particularly useful when you only care about the success case
|
|
494
|
+
* (`Right`) of an `Either` and want to handle the result using `Option`. By
|
|
495
|
+
* using this function, you can convert `Either` into a simpler structure for
|
|
496
|
+
* cases where error handling is not required.
|
|
497
|
+
*
|
|
498
|
+
* @see {@link getLeft} for the opposite operation.
|
|
325
499
|
*
|
|
326
500
|
* @example
|
|
327
501
|
* ```ts
|
|
328
|
-
* import {
|
|
502
|
+
* import { Either, Option } from "effect"
|
|
329
503
|
*
|
|
330
|
-
*
|
|
331
|
-
*
|
|
504
|
+
* console.log(Option.getRight(Either.right("ok")))
|
|
505
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'ok' }
|
|
506
|
+
*
|
|
507
|
+
* console.log(Option.getRight(Either.left("err")))
|
|
508
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
332
509
|
* ```
|
|
333
510
|
*
|
|
334
|
-
* @category
|
|
511
|
+
* @category Conversions
|
|
335
512
|
* @since 2.0.0
|
|
336
513
|
*/
|
|
337
514
|
export const getRight: <R, L>(self: Either<R, L>) => Option<R> = either.getRight
|
|
338
515
|
|
|
339
516
|
/**
|
|
340
|
-
* Converts
|
|
517
|
+
* Converts an `Either` into an `Option` by discarding the right value and
|
|
518
|
+
* extracting the left value.
|
|
519
|
+
*
|
|
520
|
+
* **Details**
|
|
521
|
+
*
|
|
522
|
+
* This function transforms an `Either` into an `Option` as follows:
|
|
523
|
+
*
|
|
524
|
+
* - If the `Either` is a `Left`, its value is wrapped in a `Some` and returned.
|
|
525
|
+
* - If the `Either` is a `Right`, the value is discarded, and `None` is
|
|
526
|
+
* returned.
|
|
527
|
+
*
|
|
528
|
+
* This utility is useful when you only care about the error case (`Left`) of an
|
|
529
|
+
* `Either` and want to handle it as an `Option`. By discarding the right value,
|
|
530
|
+
* it simplifies error-focused workflows.
|
|
531
|
+
*
|
|
532
|
+
* @see {@link getRight} for the opposite operation.
|
|
341
533
|
*
|
|
342
534
|
* @example
|
|
343
535
|
* ```ts
|
|
344
|
-
* import {
|
|
536
|
+
* import { Either, Option } from "effect"
|
|
345
537
|
*
|
|
346
|
-
*
|
|
347
|
-
*
|
|
538
|
+
* console.log(Option.getLeft(Either.right("ok")))
|
|
539
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
540
|
+
*
|
|
541
|
+
* console.log(Option.getLeft(Either.left("err")))
|
|
542
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'err' }
|
|
348
543
|
* ```
|
|
349
544
|
*
|
|
350
|
-
* @category
|
|
545
|
+
* @category Conversions
|
|
351
546
|
* @since 2.0.0
|
|
352
547
|
*/
|
|
353
548
|
export const getLeft: <R, L>(self: Either<R, L>) => Option<L> = either.getLeft
|
|
354
549
|
|
|
355
550
|
/**
|
|
356
|
-
* Returns the value
|
|
551
|
+
* Returns the value contained in the `Option` if it is `Some`, otherwise
|
|
552
|
+
* evaluates and returns the result of `onNone`.
|
|
553
|
+
*
|
|
554
|
+
* **Details**
|
|
555
|
+
*
|
|
556
|
+
* This function allows you to provide a fallback value or computation for when
|
|
557
|
+
* an `Option` is `None`. If the `Option` contains a value (`Some`), that value
|
|
558
|
+
* is returned. If it is empty (`None`), the `onNone` function is executed, and
|
|
559
|
+
* its result is returned instead.
|
|
357
560
|
*
|
|
358
|
-
*
|
|
359
|
-
*
|
|
561
|
+
* This utility is helpful for safely handling `Option` values by ensuring you
|
|
562
|
+
* always receive a meaningful result, whether or not the `Option` contains a
|
|
563
|
+
* value. It is particularly useful for providing default values or alternative
|
|
564
|
+
* logic when working with optional values.
|
|
565
|
+
*
|
|
566
|
+
* @see {@link getOrNull} for a version that returns `null` instead of executing a function.
|
|
567
|
+
* @see {@link getOrUndefined} for a version that returns `undefined` instead of executing a function.
|
|
360
568
|
*
|
|
361
569
|
* @example
|
|
362
570
|
* ```ts
|
|
363
|
-
* import {
|
|
571
|
+
* import { Option } from "effect"
|
|
572
|
+
*
|
|
573
|
+
* console.log(Option.some(1).pipe(Option.getOrElse(() => 0)))
|
|
574
|
+
* // Output: 1
|
|
364
575
|
*
|
|
365
|
-
*
|
|
366
|
-
*
|
|
576
|
+
* console.log(Option.none().pipe(Option.getOrElse(() => 0)))
|
|
577
|
+
* // Output: 0
|
|
367
578
|
* ```
|
|
368
579
|
*
|
|
369
|
-
* @category
|
|
580
|
+
* @category Getters
|
|
370
581
|
* @since 2.0.0
|
|
371
582
|
*/
|
|
372
583
|
export const getOrElse: {
|
|
373
584
|
/**
|
|
374
|
-
* Returns the value
|
|
585
|
+
* Returns the value contained in the `Option` if it is `Some`, otherwise
|
|
586
|
+
* evaluates and returns the result of `onNone`.
|
|
375
587
|
*
|
|
376
|
-
*
|
|
377
|
-
*
|
|
588
|
+
* **Details**
|
|
589
|
+
*
|
|
590
|
+
* This function allows you to provide a fallback value or computation for when
|
|
591
|
+
* an `Option` is `None`. If the `Option` contains a value (`Some`), that value
|
|
592
|
+
* is returned. If it is empty (`None`), the `onNone` function is executed, and
|
|
593
|
+
* its result is returned instead.
|
|
594
|
+
*
|
|
595
|
+
* This utility is helpful for safely handling `Option` values by ensuring you
|
|
596
|
+
* always receive a meaningful result, whether or not the `Option` contains a
|
|
597
|
+
* value. It is particularly useful for providing default values or alternative
|
|
598
|
+
* logic when working with optional values.
|
|
599
|
+
*
|
|
600
|
+
* @see {@link getOrNull} for a version that returns `null` instead of executing a function.
|
|
601
|
+
* @see {@link getOrUndefined} for a version that returns `undefined` instead of executing a function.
|
|
378
602
|
*
|
|
379
603
|
* @example
|
|
380
604
|
* ```ts
|
|
381
|
-
* import {
|
|
605
|
+
* import { Option } from "effect"
|
|
606
|
+
*
|
|
607
|
+
* console.log(Option.some(1).pipe(Option.getOrElse(() => 0)))
|
|
608
|
+
* // Output: 1
|
|
382
609
|
*
|
|
383
|
-
*
|
|
384
|
-
*
|
|
610
|
+
* console.log(Option.none().pipe(Option.getOrElse(() => 0)))
|
|
611
|
+
* // Output: 0
|
|
385
612
|
* ```
|
|
386
613
|
*
|
|
387
|
-
* @category
|
|
614
|
+
* @category Getters
|
|
388
615
|
* @since 2.0.0
|
|
389
616
|
*/
|
|
390
617
|
<B>(onNone: LazyArg<B>): <A>(self: Option<A>) => B | A
|
|
391
618
|
/**
|
|
392
|
-
* Returns the value
|
|
619
|
+
* Returns the value contained in the `Option` if it is `Some`, otherwise
|
|
620
|
+
* evaluates and returns the result of `onNone`.
|
|
621
|
+
*
|
|
622
|
+
* **Details**
|
|
393
623
|
*
|
|
394
|
-
*
|
|
395
|
-
*
|
|
624
|
+
* This function allows you to provide a fallback value or computation for when
|
|
625
|
+
* an `Option` is `None`. If the `Option` contains a value (`Some`), that value
|
|
626
|
+
* is returned. If it is empty (`None`), the `onNone` function is executed, and
|
|
627
|
+
* its result is returned instead.
|
|
628
|
+
*
|
|
629
|
+
* This utility is helpful for safely handling `Option` values by ensuring you
|
|
630
|
+
* always receive a meaningful result, whether or not the `Option` contains a
|
|
631
|
+
* value. It is particularly useful for providing default values or alternative
|
|
632
|
+
* logic when working with optional values.
|
|
633
|
+
*
|
|
634
|
+
* @see {@link getOrNull} for a version that returns `null` instead of executing a function.
|
|
635
|
+
* @see {@link getOrUndefined} for a version that returns `undefined` instead of executing a function.
|
|
396
636
|
*
|
|
397
637
|
* @example
|
|
398
638
|
* ```ts
|
|
399
|
-
* import {
|
|
639
|
+
* import { Option } from "effect"
|
|
400
640
|
*
|
|
401
|
-
*
|
|
402
|
-
*
|
|
641
|
+
* console.log(Option.some(1).pipe(Option.getOrElse(() => 0)))
|
|
642
|
+
* // Output: 1
|
|
643
|
+
*
|
|
644
|
+
* console.log(Option.none().pipe(Option.getOrElse(() => 0)))
|
|
645
|
+
* // Output: 0
|
|
403
646
|
* ```
|
|
404
647
|
*
|
|
405
|
-
* @category
|
|
648
|
+
* @category Getters
|
|
406
649
|
* @since 2.0.0
|
|
407
650
|
*/
|
|
408
651
|
<A, B>(self: Option<A>, onNone: LazyArg<B>): A | B
|
|
@@ -412,134 +655,113 @@ export const getOrElse: {
|
|
|
412
655
|
)
|
|
413
656
|
|
|
414
657
|
/**
|
|
415
|
-
* Returns the provided `Option` `that` if
|
|
658
|
+
* Returns the provided `Option` `that` if the current `Option` (`self`) is
|
|
659
|
+
* `None`; otherwise, it returns `self`.
|
|
660
|
+
*
|
|
661
|
+
* **Details**
|
|
662
|
+
*
|
|
663
|
+
* This function provides a fallback mechanism for `Option` values. If the
|
|
664
|
+
* current `Option` is `None` (i.e., it contains no value), the `that` function
|
|
665
|
+
* is evaluated, and its resulting `Option` is returned. If the current `Option`
|
|
666
|
+
* is `Some` (i.e., it contains a value), the original `Option` is returned
|
|
667
|
+
* unchanged.
|
|
416
668
|
*
|
|
417
|
-
*
|
|
418
|
-
*
|
|
669
|
+
* This is particularly useful for chaining fallback values or computations,
|
|
670
|
+
* allowing you to provide alternative `Option` values when the first one is
|
|
671
|
+
* empty.
|
|
419
672
|
*
|
|
420
673
|
* @example
|
|
421
674
|
* ```ts
|
|
422
|
-
* import {
|
|
675
|
+
* import { Option } from "effect"
|
|
423
676
|
*
|
|
424
|
-
*
|
|
425
|
-
*
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
*
|
|
429
|
-
*
|
|
430
|
-
* )
|
|
431
|
-
*
|
|
432
|
-
*
|
|
433
|
-
*
|
|
434
|
-
*
|
|
435
|
-
* ),
|
|
436
|
-
* Option.some('a')
|
|
437
|
-
* )
|
|
438
|
-
* assert.deepStrictEqual(
|
|
439
|
-
* pipe(
|
|
440
|
-
* Option.none(),
|
|
441
|
-
* Option.orElse(() => Option.some('b'))
|
|
442
|
-
* ),
|
|
443
|
-
* Option.some('b')
|
|
444
|
-
* )
|
|
445
|
-
* assert.deepStrictEqual(
|
|
446
|
-
* pipe(
|
|
447
|
-
* Option.some('a'),
|
|
448
|
-
* Option.orElse(() => Option.some('b'))
|
|
449
|
-
* ),
|
|
450
|
-
* Option.some('a')
|
|
451
|
-
* )
|
|
677
|
+
* console.log(Option.none().pipe(Option.orElse(() => Option.none())))
|
|
678
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
679
|
+
*
|
|
680
|
+
* console.log(Option.some("a").pipe(Option.orElse(() => Option.none())))
|
|
681
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
682
|
+
*
|
|
683
|
+
* console.log(Option.none().pipe(Option.orElse(() => Option.some("b"))))
|
|
684
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'b' }
|
|
685
|
+
*
|
|
686
|
+
* console.log(Option.some("a").pipe(Option.orElse(() => Option.some("b"))))
|
|
687
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
452
688
|
* ```
|
|
453
689
|
*
|
|
454
|
-
* @category
|
|
690
|
+
* @category Error handling
|
|
455
691
|
* @since 2.0.0
|
|
456
692
|
*/
|
|
457
693
|
export const orElse: {
|
|
458
694
|
/**
|
|
459
|
-
* Returns the provided `Option` `that` if
|
|
695
|
+
* Returns the provided `Option` `that` if the current `Option` (`self`) is
|
|
696
|
+
* `None`; otherwise, it returns `self`.
|
|
697
|
+
*
|
|
698
|
+
* **Details**
|
|
460
699
|
*
|
|
461
|
-
*
|
|
462
|
-
*
|
|
700
|
+
* This function provides a fallback mechanism for `Option` values. If the
|
|
701
|
+
* current `Option` is `None` (i.e., it contains no value), the `that` function
|
|
702
|
+
* is evaluated, and its resulting `Option` is returned. If the current `Option`
|
|
703
|
+
* is `Some` (i.e., it contains a value), the original `Option` is returned
|
|
704
|
+
* unchanged.
|
|
705
|
+
*
|
|
706
|
+
* This is particularly useful for chaining fallback values or computations,
|
|
707
|
+
* allowing you to provide alternative `Option` values when the first one is
|
|
708
|
+
* empty.
|
|
463
709
|
*
|
|
464
710
|
* @example
|
|
465
711
|
* ```ts
|
|
466
|
-
* import {
|
|
712
|
+
* import { Option } from "effect"
|
|
467
713
|
*
|
|
468
|
-
*
|
|
469
|
-
*
|
|
470
|
-
*
|
|
471
|
-
*
|
|
472
|
-
*
|
|
473
|
-
*
|
|
474
|
-
* )
|
|
475
|
-
*
|
|
476
|
-
*
|
|
477
|
-
*
|
|
478
|
-
*
|
|
479
|
-
* ),
|
|
480
|
-
* Option.some('a')
|
|
481
|
-
* )
|
|
482
|
-
* assert.deepStrictEqual(
|
|
483
|
-
* pipe(
|
|
484
|
-
* Option.none(),
|
|
485
|
-
* Option.orElse(() => Option.some('b'))
|
|
486
|
-
* ),
|
|
487
|
-
* Option.some('b')
|
|
488
|
-
* )
|
|
489
|
-
* assert.deepStrictEqual(
|
|
490
|
-
* pipe(
|
|
491
|
-
* Option.some('a'),
|
|
492
|
-
* Option.orElse(() => Option.some('b'))
|
|
493
|
-
* ),
|
|
494
|
-
* Option.some('a')
|
|
495
|
-
* )
|
|
714
|
+
* console.log(Option.none().pipe(Option.orElse(() => Option.none())))
|
|
715
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
716
|
+
*
|
|
717
|
+
* console.log(Option.some("a").pipe(Option.orElse(() => Option.none())))
|
|
718
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
719
|
+
*
|
|
720
|
+
* console.log(Option.none().pipe(Option.orElse(() => Option.some("b"))))
|
|
721
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'b' }
|
|
722
|
+
*
|
|
723
|
+
* console.log(Option.some("a").pipe(Option.orElse(() => Option.some("b"))))
|
|
724
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
496
725
|
* ```
|
|
497
726
|
*
|
|
498
|
-
* @category
|
|
727
|
+
* @category Error handling
|
|
499
728
|
* @since 2.0.0
|
|
500
729
|
*/
|
|
501
730
|
<B>(that: LazyArg<Option<B>>): <A>(self: Option<A>) => Option<B | A>
|
|
502
731
|
/**
|
|
503
|
-
* Returns the provided `Option` `that` if
|
|
732
|
+
* Returns the provided `Option` `that` if the current `Option` (`self`) is
|
|
733
|
+
* `None`; otherwise, it returns `self`.
|
|
734
|
+
*
|
|
735
|
+
* **Details**
|
|
736
|
+
*
|
|
737
|
+
* This function provides a fallback mechanism for `Option` values. If the
|
|
738
|
+
* current `Option` is `None` (i.e., it contains no value), the `that` function
|
|
739
|
+
* is evaluated, and its resulting `Option` is returned. If the current `Option`
|
|
740
|
+
* is `Some` (i.e., it contains a value), the original `Option` is returned
|
|
741
|
+
* unchanged.
|
|
504
742
|
*
|
|
505
|
-
*
|
|
506
|
-
*
|
|
743
|
+
* This is particularly useful for chaining fallback values or computations,
|
|
744
|
+
* allowing you to provide alternative `Option` values when the first one is
|
|
745
|
+
* empty.
|
|
507
746
|
*
|
|
508
747
|
* @example
|
|
509
748
|
* ```ts
|
|
510
|
-
* import {
|
|
749
|
+
* import { Option } from "effect"
|
|
511
750
|
*
|
|
512
|
-
*
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
*
|
|
517
|
-
*
|
|
518
|
-
* )
|
|
519
|
-
*
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
*
|
|
523
|
-
* ),
|
|
524
|
-
* Option.some('a')
|
|
525
|
-
* )
|
|
526
|
-
* assert.deepStrictEqual(
|
|
527
|
-
* pipe(
|
|
528
|
-
* Option.none(),
|
|
529
|
-
* Option.orElse(() => Option.some('b'))
|
|
530
|
-
* ),
|
|
531
|
-
* Option.some('b')
|
|
532
|
-
* )
|
|
533
|
-
* assert.deepStrictEqual(
|
|
534
|
-
* pipe(
|
|
535
|
-
* Option.some('a'),
|
|
536
|
-
* Option.orElse(() => Option.some('b'))
|
|
537
|
-
* ),
|
|
538
|
-
* Option.some('a')
|
|
539
|
-
* )
|
|
751
|
+
* console.log(Option.none().pipe(Option.orElse(() => Option.none())))
|
|
752
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
753
|
+
*
|
|
754
|
+
* console.log(Option.some("a").pipe(Option.orElse(() => Option.none())))
|
|
755
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
756
|
+
*
|
|
757
|
+
* console.log(Option.none().pipe(Option.orElse(() => Option.some("b"))))
|
|
758
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'b' }
|
|
759
|
+
*
|
|
760
|
+
* console.log(Option.some("a").pipe(Option.orElse(() => Option.some("b"))))
|
|
761
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
540
762
|
* ```
|
|
541
763
|
*
|
|
542
|
-
* @category
|
|
764
|
+
* @category Error handling
|
|
543
765
|
* @since 2.0.0
|
|
544
766
|
*/
|
|
545
767
|
<A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<A | B>
|
|
@@ -549,92 +771,92 @@ export const orElse: {
|
|
|
549
771
|
)
|
|
550
772
|
|
|
551
773
|
/**
|
|
552
|
-
* Returns the provided default value
|
|
774
|
+
* Returns the provided default value wrapped in `Some` if the current `Option`
|
|
775
|
+
* (`self`) is `None`; otherwise, returns `self`.
|
|
553
776
|
*
|
|
554
|
-
*
|
|
555
|
-
*
|
|
777
|
+
* **Details**
|
|
778
|
+
*
|
|
779
|
+
* This function provides a way to supply a default value for cases where an
|
|
780
|
+
* `Option` is `None`. If the current `Option` is empty (`None`), the `onNone`
|
|
781
|
+
* function is executed to compute the default value, which is then wrapped in a
|
|
782
|
+
* `Some`. If the current `Option` contains a value (`Some`), it is returned as
|
|
783
|
+
* is.
|
|
784
|
+
*
|
|
785
|
+
* This is particularly useful for handling optional values where a fallback
|
|
786
|
+
* default needs to be provided explicitly in case of absence.
|
|
556
787
|
*
|
|
557
788
|
* @example
|
|
558
789
|
* ```ts
|
|
559
|
-
* import {
|
|
790
|
+
* import { Option } from "effect"
|
|
560
791
|
*
|
|
561
|
-
*
|
|
562
|
-
*
|
|
563
|
-
*
|
|
564
|
-
*
|
|
565
|
-
*
|
|
566
|
-
* Option.some('b')
|
|
567
|
-
* )
|
|
568
|
-
* assert.deepStrictEqual(
|
|
569
|
-
* pipe(
|
|
570
|
-
* Option.some('a'),
|
|
571
|
-
* Option.orElseSome(() => 'b')
|
|
572
|
-
* ),
|
|
573
|
-
* Option.some('a')
|
|
574
|
-
* )
|
|
792
|
+
* console.log(Option.none().pipe(Option.orElseSome(() => "b")))
|
|
793
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'b' }
|
|
794
|
+
*
|
|
795
|
+
* console.log(Option.some("a").pipe(Option.orElseSome(() => "b")))
|
|
796
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
575
797
|
* ```
|
|
576
798
|
*
|
|
577
|
-
* @category
|
|
799
|
+
* @category Error handling
|
|
578
800
|
* @since 2.0.0
|
|
579
801
|
*/
|
|
580
802
|
export const orElseSome: {
|
|
581
803
|
/**
|
|
582
|
-
* Returns the provided default value
|
|
804
|
+
* Returns the provided default value wrapped in `Some` if the current `Option`
|
|
805
|
+
* (`self`) is `None`; otherwise, returns `self`.
|
|
806
|
+
*
|
|
807
|
+
* **Details**
|
|
583
808
|
*
|
|
584
|
-
*
|
|
585
|
-
*
|
|
809
|
+
* This function provides a way to supply a default value for cases where an
|
|
810
|
+
* `Option` is `None`. If the current `Option` is empty (`None`), the `onNone`
|
|
811
|
+
* function is executed to compute the default value, which is then wrapped in a
|
|
812
|
+
* `Some`. If the current `Option` contains a value (`Some`), it is returned as
|
|
813
|
+
* is.
|
|
814
|
+
*
|
|
815
|
+
* This is particularly useful for handling optional values where a fallback
|
|
816
|
+
* default needs to be provided explicitly in case of absence.
|
|
586
817
|
*
|
|
587
818
|
* @example
|
|
588
819
|
* ```ts
|
|
589
|
-
* import {
|
|
820
|
+
* import { Option } from "effect"
|
|
590
821
|
*
|
|
591
|
-
*
|
|
592
|
-
*
|
|
593
|
-
*
|
|
594
|
-
*
|
|
595
|
-
*
|
|
596
|
-
* Option.some('b')
|
|
597
|
-
* )
|
|
598
|
-
* assert.deepStrictEqual(
|
|
599
|
-
* pipe(
|
|
600
|
-
* Option.some('a'),
|
|
601
|
-
* Option.orElseSome(() => 'b')
|
|
602
|
-
* ),
|
|
603
|
-
* Option.some('a')
|
|
604
|
-
* )
|
|
822
|
+
* console.log(Option.none().pipe(Option.orElseSome(() => "b")))
|
|
823
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'b' }
|
|
824
|
+
*
|
|
825
|
+
* console.log(Option.some("a").pipe(Option.orElseSome(() => "b")))
|
|
826
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
605
827
|
* ```
|
|
606
828
|
*
|
|
607
|
-
* @category
|
|
829
|
+
* @category Error handling
|
|
608
830
|
* @since 2.0.0
|
|
609
831
|
*/
|
|
610
832
|
<B>(onNone: LazyArg<B>): <A>(self: Option<A>) => Option<B | A>
|
|
611
833
|
/**
|
|
612
|
-
* Returns the provided default value
|
|
834
|
+
* Returns the provided default value wrapped in `Some` if the current `Option`
|
|
835
|
+
* (`self`) is `None`; otherwise, returns `self`.
|
|
613
836
|
*
|
|
614
|
-
*
|
|
615
|
-
*
|
|
837
|
+
* **Details**
|
|
838
|
+
*
|
|
839
|
+
* This function provides a way to supply a default value for cases where an
|
|
840
|
+
* `Option` is `None`. If the current `Option` is empty (`None`), the `onNone`
|
|
841
|
+
* function is executed to compute the default value, which is then wrapped in a
|
|
842
|
+
* `Some`. If the current `Option` contains a value (`Some`), it is returned as
|
|
843
|
+
* is.
|
|
844
|
+
*
|
|
845
|
+
* This is particularly useful for handling optional values where a fallback
|
|
846
|
+
* default needs to be provided explicitly in case of absence.
|
|
616
847
|
*
|
|
617
848
|
* @example
|
|
618
849
|
* ```ts
|
|
619
|
-
* import {
|
|
850
|
+
* import { Option } from "effect"
|
|
620
851
|
*
|
|
621
|
-
*
|
|
622
|
-
*
|
|
623
|
-
*
|
|
624
|
-
*
|
|
625
|
-
*
|
|
626
|
-
* Option.some('b')
|
|
627
|
-
* )
|
|
628
|
-
* assert.deepStrictEqual(
|
|
629
|
-
* pipe(
|
|
630
|
-
* Option.some('a'),
|
|
631
|
-
* Option.orElseSome(() => 'b')
|
|
632
|
-
* ),
|
|
633
|
-
* Option.some('a')
|
|
634
|
-
* )
|
|
852
|
+
* console.log(Option.none().pipe(Option.orElseSome(() => "b")))
|
|
853
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'b' }
|
|
854
|
+
*
|
|
855
|
+
* console.log(Option.some("a").pipe(Option.orElseSome(() => "b")))
|
|
856
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
635
857
|
* ```
|
|
636
858
|
*
|
|
637
|
-
* @category
|
|
859
|
+
* @category Error handling
|
|
638
860
|
* @since 2.0.0
|
|
639
861
|
*/
|
|
640
862
|
<A, B>(self: Option<A>, onNone: LazyArg<B>): Option<A | B>
|
|
@@ -644,41 +866,74 @@ export const orElseSome: {
|
|
|
644
866
|
)
|
|
645
867
|
|
|
646
868
|
/**
|
|
647
|
-
* Similar to
|
|
648
|
-
*
|
|
869
|
+
* Similar to {@link orElse}, but returns an `Either` wrapped in an `Option` to
|
|
870
|
+
* indicate the source of the value.
|
|
871
|
+
*
|
|
872
|
+
* **Details**
|
|
649
873
|
*
|
|
650
|
-
* This
|
|
874
|
+
* This function allows you to provide a fallback `Option` in case the current
|
|
875
|
+
* `Option` (`self`) is `None`. However, unlike `orElse`, it returns the value
|
|
876
|
+
* wrapped in an `Either` object, providing additional information about where
|
|
877
|
+
* the value came from:
|
|
651
878
|
*
|
|
652
|
-
*
|
|
653
|
-
*
|
|
879
|
+
* - If the value is from the fallback `Option` (`that`), it is wrapped in an
|
|
880
|
+
* `Either.right`.
|
|
881
|
+
* - If the value is from the original `Option` (`self`), it is wrapped in an
|
|
882
|
+
* `Either.left`.
|
|
654
883
|
*
|
|
655
|
-
*
|
|
884
|
+
* This is especially useful when you need to differentiate between values
|
|
885
|
+
* originating from the primary `Option` and those coming from the fallback,
|
|
886
|
+
* while still maintaining the `Option`-style handling.
|
|
887
|
+
*
|
|
888
|
+
* @category Error handling
|
|
656
889
|
* @since 2.0.0
|
|
657
890
|
*/
|
|
658
891
|
export const orElseEither: {
|
|
659
892
|
/**
|
|
660
|
-
* Similar to
|
|
661
|
-
*
|
|
893
|
+
* Similar to {@link orElse}, but returns an `Either` wrapped in an `Option` to
|
|
894
|
+
* indicate the source of the value.
|
|
895
|
+
*
|
|
896
|
+
* **Details**
|
|
897
|
+
*
|
|
898
|
+
* This function allows you to provide a fallback `Option` in case the current
|
|
899
|
+
* `Option` (`self`) is `None`. However, unlike `orElse`, it returns the value
|
|
900
|
+
* wrapped in an `Either` object, providing additional information about where
|
|
901
|
+
* the value came from:
|
|
662
902
|
*
|
|
663
|
-
*
|
|
903
|
+
* - If the value is from the fallback `Option` (`that`), it is wrapped in an
|
|
904
|
+
* `Either.right`.
|
|
905
|
+
* - If the value is from the original `Option` (`self`), it is wrapped in an
|
|
906
|
+
* `Either.left`.
|
|
664
907
|
*
|
|
665
|
-
*
|
|
666
|
-
*
|
|
908
|
+
* This is especially useful when you need to differentiate between values
|
|
909
|
+
* originating from the primary `Option` and those coming from the fallback,
|
|
910
|
+
* while still maintaining the `Option`-style handling.
|
|
667
911
|
*
|
|
668
|
-
* @category
|
|
912
|
+
* @category Error handling
|
|
669
913
|
* @since 2.0.0
|
|
670
914
|
*/
|
|
671
915
|
<B>(that: LazyArg<Option<B>>): <A>(self: Option<A>) => Option<Either<B, A>>
|
|
672
916
|
/**
|
|
673
|
-
* Similar to
|
|
674
|
-
*
|
|
917
|
+
* Similar to {@link orElse}, but returns an `Either` wrapped in an `Option` to
|
|
918
|
+
* indicate the source of the value.
|
|
675
919
|
*
|
|
676
|
-
*
|
|
920
|
+
* **Details**
|
|
677
921
|
*
|
|
678
|
-
*
|
|
679
|
-
*
|
|
922
|
+
* This function allows you to provide a fallback `Option` in case the current
|
|
923
|
+
* `Option` (`self`) is `None`. However, unlike `orElse`, it returns the value
|
|
924
|
+
* wrapped in an `Either` object, providing additional information about where
|
|
925
|
+
* the value came from:
|
|
680
926
|
*
|
|
681
|
-
*
|
|
927
|
+
* - If the value is from the fallback `Option` (`that`), it is wrapped in an
|
|
928
|
+
* `Either.right`.
|
|
929
|
+
* - If the value is from the original `Option` (`self`), it is wrapped in an
|
|
930
|
+
* `Either.left`.
|
|
931
|
+
*
|
|
932
|
+
* This is especially useful when you need to differentiate between values
|
|
933
|
+
* originating from the primary `Option` and those coming from the fallback,
|
|
934
|
+
* while still maintaining the `Option`-style handling.
|
|
935
|
+
*
|
|
936
|
+
* @category Error handling
|
|
682
937
|
* @since 2.0.0
|
|
683
938
|
*/
|
|
684
939
|
<A, B>(self: Option<A>, that: LazyArg<Option<B>>): Option<Either<B, A>>
|
|
@@ -689,18 +944,33 @@ export const orElseEither: {
|
|
|
689
944
|
)
|
|
690
945
|
|
|
691
946
|
/**
|
|
692
|
-
*
|
|
947
|
+
* Returns the first `Some` value found in an `Iterable` collection of
|
|
948
|
+
* `Option`s, or `None` if no `Some` is found.
|
|
949
|
+
*
|
|
950
|
+
* **Details**
|
|
693
951
|
*
|
|
694
|
-
*
|
|
952
|
+
* This function iterates over a collection of `Option` values and returns the
|
|
953
|
+
* first `Some` it encounters. If the collection contains only `None` values,
|
|
954
|
+
* the result will also be `None`. This utility is useful for efficiently
|
|
955
|
+
* finding the first valid value in a sequence of potentially empty or invalid
|
|
956
|
+
* options.
|
|
957
|
+
*
|
|
958
|
+
* The iteration stops as soon as a `Some` is found, making this function
|
|
959
|
+
* efficient for large collections.
|
|
695
960
|
*
|
|
696
961
|
* @example
|
|
697
962
|
* ```ts
|
|
698
963
|
* import { Option } from "effect"
|
|
699
964
|
*
|
|
700
|
-
*
|
|
965
|
+
* console.log(Option.firstSomeOf([
|
|
966
|
+
* Option.none(),
|
|
967
|
+
* Option.some(1),
|
|
968
|
+
* Option.some(2)
|
|
969
|
+
* ]))
|
|
970
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
701
971
|
* ```
|
|
702
972
|
*
|
|
703
|
-
* @category
|
|
973
|
+
* @category Error handling
|
|
704
974
|
* @since 2.0.0
|
|
705
975
|
*/
|
|
706
976
|
export const firstSomeOf = <T, C extends Iterable<Option<T>> = Iterable<Option<T>>>(
|
|
@@ -716,31 +986,41 @@ export const firstSomeOf = <T, C extends Iterable<Option<T>> = Iterable<Option<T
|
|
|
716
986
|
}
|
|
717
987
|
|
|
718
988
|
/**
|
|
719
|
-
*
|
|
720
|
-
*
|
|
721
|
-
*
|
|
722
|
-
* @param nullableValue - The nullable value to be converted to an `Option`.
|
|
989
|
+
* Converts a nullable value into an `Option`. Returns `None` if the value is
|
|
990
|
+
* `null` or `undefined`, otherwise wraps the value in a `Some`.
|
|
723
991
|
*
|
|
724
992
|
* @example
|
|
725
993
|
* ```ts
|
|
726
994
|
* import { Option } from "effect"
|
|
727
995
|
*
|
|
728
|
-
*
|
|
729
|
-
*
|
|
730
|
-
*
|
|
996
|
+
* console.log(Option.fromNullable(undefined))
|
|
997
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
998
|
+
*
|
|
999
|
+
* console.log(Option.fromNullable(null))
|
|
1000
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1001
|
+
*
|
|
1002
|
+
* console.log(Option.fromNullable(1))
|
|
1003
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
731
1004
|
* ```
|
|
732
1005
|
*
|
|
733
|
-
* @category
|
|
1006
|
+
* @category Conversions
|
|
734
1007
|
* @since 2.0.0
|
|
735
1008
|
*/
|
|
736
1009
|
export const fromNullable = <A>(
|
|
737
1010
|
nullableValue: A
|
|
738
|
-
): Option<
|
|
739
|
-
NonNullable<A>
|
|
740
|
-
> => (nullableValue == null ? none() : some(nullableValue as NonNullable<A>))
|
|
1011
|
+
): Option<NonNullable<A>> => (nullableValue == null ? none() : some(nullableValue as NonNullable<A>))
|
|
741
1012
|
|
|
742
1013
|
/**
|
|
743
|
-
*
|
|
1014
|
+
* Lifts a function that returns `null` or `undefined` into the `Option`
|
|
1015
|
+
* context.
|
|
1016
|
+
*
|
|
1017
|
+
* **Details**
|
|
1018
|
+
*
|
|
1019
|
+
* This function takes a function `f` that might return `null` or `undefined`
|
|
1020
|
+
* and transforms it into a function that returns an `Option`. The resulting
|
|
1021
|
+
* function will return:
|
|
1022
|
+
* - `Some` if the original function produces a non-null, non-undefined value.
|
|
1023
|
+
* - `None` if the original function produces `null` or `undefined`.
|
|
744
1024
|
*
|
|
745
1025
|
* @example
|
|
746
1026
|
* ```ts
|
|
@@ -753,11 +1033,14 @@ export const fromNullable = <A>(
|
|
|
753
1033
|
*
|
|
754
1034
|
* const parseOption = Option.liftNullable(parse)
|
|
755
1035
|
*
|
|
756
|
-
*
|
|
757
|
-
*
|
|
1036
|
+
* console.log(parseOption("1"))
|
|
1037
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
1038
|
+
*
|
|
1039
|
+
* console.log(parseOption("not a number"))
|
|
1040
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
758
1041
|
* ```
|
|
759
1042
|
*
|
|
760
|
-
* @category
|
|
1043
|
+
* @category Conversions
|
|
761
1044
|
* @since 2.0.0
|
|
762
1045
|
*/
|
|
763
1046
|
export const liftNullable = <A extends ReadonlyArray<unknown>, B>(
|
|
@@ -766,48 +1049,74 @@ export const liftNullable = <A extends ReadonlyArray<unknown>, B>(
|
|
|
766
1049
|
(...a) => fromNullable(f(...a))
|
|
767
1050
|
|
|
768
1051
|
/**
|
|
769
|
-
* Returns the value
|
|
1052
|
+
* Returns the value contained in the `Option` if it is `Some`; otherwise,
|
|
1053
|
+
* returns `null`.
|
|
1054
|
+
*
|
|
1055
|
+
* **Details**
|
|
1056
|
+
*
|
|
1057
|
+
* This function provides a way to extract the value of an `Option` while
|
|
1058
|
+
* falling back to `null` if the `Option` is `None`.
|
|
770
1059
|
*
|
|
771
|
-
*
|
|
1060
|
+
* It is particularly useful in scenarios where `null` is an acceptable
|
|
1061
|
+
* placeholder for the absence of a value, such as when interacting with APIs or
|
|
1062
|
+
* systems that use `null` as a default for missing values.
|
|
772
1063
|
*
|
|
773
1064
|
* @example
|
|
774
1065
|
* ```ts
|
|
775
1066
|
* import { Option } from "effect"
|
|
776
1067
|
*
|
|
777
|
-
*
|
|
778
|
-
*
|
|
1068
|
+
* console.log(Option.getOrNull(Option.some(1)))
|
|
1069
|
+
* // Output: 1
|
|
1070
|
+
*
|
|
1071
|
+
* console.log(Option.getOrNull(Option.none()))
|
|
1072
|
+
* // Output: null
|
|
779
1073
|
* ```
|
|
780
1074
|
*
|
|
781
|
-
* @category
|
|
1075
|
+
* @category Getters
|
|
782
1076
|
* @since 2.0.0
|
|
783
1077
|
*/
|
|
784
1078
|
export const getOrNull: <A>(self: Option<A>) => A | null = getOrElse(constNull)
|
|
785
1079
|
|
|
786
1080
|
/**
|
|
787
|
-
* Returns the value
|
|
1081
|
+
* Returns the value contained in the `Option` if it is `Some`; otherwise,
|
|
1082
|
+
* returns `undefined`.
|
|
1083
|
+
*
|
|
1084
|
+
* **Details**
|
|
1085
|
+
*
|
|
1086
|
+
* This function provides a way to extract the value of an `Option` while
|
|
1087
|
+
* falling back to `undefined` if the `Option` is `None`.
|
|
788
1088
|
*
|
|
789
|
-
*
|
|
1089
|
+
* It is particularly useful in scenarios where `undefined` is an acceptable
|
|
1090
|
+
* placeholder for the absence of a value, such as when interacting with APIs or
|
|
1091
|
+
* systems that use `undefined` as a default for missing values.
|
|
790
1092
|
*
|
|
791
1093
|
* @example
|
|
792
1094
|
* ```ts
|
|
793
1095
|
* import { Option } from "effect"
|
|
794
1096
|
*
|
|
795
|
-
*
|
|
796
|
-
*
|
|
1097
|
+
* console.log(Option.getOrUndefined(Option.some(1)))
|
|
1098
|
+
* // Output: 1
|
|
1099
|
+
*
|
|
1100
|
+
* console.log(Option.getOrUndefined(Option.none()))
|
|
1101
|
+
* // Output: undefined
|
|
797
1102
|
* ```
|
|
798
1103
|
*
|
|
799
|
-
* @category
|
|
1104
|
+
* @category Getters
|
|
800
1105
|
* @since 2.0.0
|
|
801
1106
|
*/
|
|
802
1107
|
export const getOrUndefined: <A>(self: Option<A>) => A | undefined = getOrElse(constUndefined)
|
|
803
1108
|
|
|
804
1109
|
/**
|
|
805
|
-
*
|
|
1110
|
+
* Lifts a function that throws exceptions into a function that returns an
|
|
1111
|
+
* `Option`.
|
|
806
1112
|
*
|
|
807
|
-
*
|
|
808
|
-
* the exception in a more functional way.
|
|
1113
|
+
* **Details**
|
|
809
1114
|
*
|
|
810
|
-
*
|
|
1115
|
+
* This utility function takes a function `f` that might throw an exception and
|
|
1116
|
+
* transforms it into a safer function that returns an `Option`. If the original
|
|
1117
|
+
* function executes successfully, the result is wrapped in a `Some`. If an
|
|
1118
|
+
* exception is thrown, the result is `None`, allowing the developer to handle
|
|
1119
|
+
* errors in a functional, type-safe way.
|
|
811
1120
|
*
|
|
812
1121
|
* @example
|
|
813
1122
|
* ```ts
|
|
@@ -815,11 +1124,14 @@ export const getOrUndefined: <A>(self: Option<A>) => A | undefined = getOrElse(c
|
|
|
815
1124
|
*
|
|
816
1125
|
* const parse = Option.liftThrowable(JSON.parse)
|
|
817
1126
|
*
|
|
818
|
-
*
|
|
819
|
-
*
|
|
1127
|
+
* console.log(parse("1"))
|
|
1128
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
1129
|
+
*
|
|
1130
|
+
* console.log(parse(""))
|
|
1131
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
820
1132
|
* ```
|
|
821
1133
|
*
|
|
822
|
-
* @category
|
|
1134
|
+
* @category Conversions
|
|
823
1135
|
* @since 2.0.0
|
|
824
1136
|
*/
|
|
825
1137
|
export const liftThrowable = <A extends ReadonlyArray<unknown>, B>(
|
|
@@ -834,12 +1146,18 @@ export const liftThrowable = <A extends ReadonlyArray<unknown>, B>(
|
|
|
834
1146
|
}
|
|
835
1147
|
|
|
836
1148
|
/**
|
|
837
|
-
* Extracts the value of an `Option` or throws if the `Option` is
|
|
1149
|
+
* Extracts the value of an `Option` or throws an error if the `Option` is
|
|
1150
|
+
* `None`, using a custom error factory.
|
|
1151
|
+
*
|
|
1152
|
+
* **Details**
|
|
838
1153
|
*
|
|
839
|
-
*
|
|
1154
|
+
* This function allows you to extract the value of an `Option` when it is
|
|
1155
|
+
* `Some`. If the `Option` is `None`, it throws an error generated by the
|
|
1156
|
+
* provided `onNone` function. This utility is particularly useful when you need
|
|
1157
|
+
* a fail-fast behavior for empty `Option` values and want to provide a custom
|
|
1158
|
+
* error message or object.
|
|
840
1159
|
*
|
|
841
|
-
* @
|
|
842
|
-
* @param onNone - A function that will be called if the `Option` is `None`. It returns the error to be thrown.
|
|
1160
|
+
* @see {@link getOrThrow} for a version that throws a default error.
|
|
843
1161
|
*
|
|
844
1162
|
* @example
|
|
845
1163
|
* ```ts
|
|
@@ -852,17 +1170,23 @@ export const liftThrowable = <A extends ReadonlyArray<unknown>, B>(
|
|
|
852
1170
|
* assert.throws(() => Option.getOrThrowWith(Option.none(), () => new Error('Unexpected None')))
|
|
853
1171
|
* ```
|
|
854
1172
|
*
|
|
855
|
-
* @category
|
|
1173
|
+
* @category Conversions
|
|
856
1174
|
* @since 2.0.0
|
|
857
1175
|
*/
|
|
858
1176
|
export const getOrThrowWith: {
|
|
859
1177
|
/**
|
|
860
|
-
* Extracts the value of an `Option` or throws if the `Option` is
|
|
1178
|
+
* Extracts the value of an `Option` or throws an error if the `Option` is
|
|
1179
|
+
* `None`, using a custom error factory.
|
|
861
1180
|
*
|
|
862
|
-
*
|
|
1181
|
+
* **Details**
|
|
863
1182
|
*
|
|
864
|
-
*
|
|
865
|
-
*
|
|
1183
|
+
* This function allows you to extract the value of an `Option` when it is
|
|
1184
|
+
* `Some`. If the `Option` is `None`, it throws an error generated by the
|
|
1185
|
+
* provided `onNone` function. This utility is particularly useful when you need
|
|
1186
|
+
* a fail-fast behavior for empty `Option` values and want to provide a custom
|
|
1187
|
+
* error message or object.
|
|
1188
|
+
*
|
|
1189
|
+
* @see {@link getOrThrow} for a version that throws a default error.
|
|
866
1190
|
*
|
|
867
1191
|
* @example
|
|
868
1192
|
* ```ts
|
|
@@ -875,17 +1199,23 @@ export const getOrThrowWith: {
|
|
|
875
1199
|
* assert.throws(() => Option.getOrThrowWith(Option.none(), () => new Error('Unexpected None')))
|
|
876
1200
|
* ```
|
|
877
1201
|
*
|
|
878
|
-
* @category
|
|
1202
|
+
* @category Conversions
|
|
879
1203
|
* @since 2.0.0
|
|
880
1204
|
*/
|
|
881
1205
|
(onNone: () => unknown): <A>(self: Option<A>) => A
|
|
882
1206
|
/**
|
|
883
|
-
* Extracts the value of an `Option` or throws if the `Option` is
|
|
1207
|
+
* Extracts the value of an `Option` or throws an error if the `Option` is
|
|
1208
|
+
* `None`, using a custom error factory.
|
|
1209
|
+
*
|
|
1210
|
+
* **Details**
|
|
884
1211
|
*
|
|
885
|
-
*
|
|
1212
|
+
* This function allows you to extract the value of an `Option` when it is
|
|
1213
|
+
* `Some`. If the `Option` is `None`, it throws an error generated by the
|
|
1214
|
+
* provided `onNone` function. This utility is particularly useful when you need
|
|
1215
|
+
* a fail-fast behavior for empty `Option` values and want to provide a custom
|
|
1216
|
+
* error message or object.
|
|
886
1217
|
*
|
|
887
|
-
* @
|
|
888
|
-
* @param onNone - A function that will be called if the `Option` is `None`. It returns the error to be thrown.
|
|
1218
|
+
* @see {@link getOrThrow} for a version that throws a default error.
|
|
889
1219
|
*
|
|
890
1220
|
* @example
|
|
891
1221
|
* ```ts
|
|
@@ -898,7 +1228,7 @@ export const getOrThrowWith: {
|
|
|
898
1228
|
* assert.throws(() => Option.getOrThrowWith(Option.none(), () => new Error('Unexpected None')))
|
|
899
1229
|
* ```
|
|
900
1230
|
*
|
|
901
|
-
* @category
|
|
1231
|
+
* @category Conversions
|
|
902
1232
|
* @since 2.0.0
|
|
903
1233
|
*/
|
|
904
1234
|
<A>(self: Option<A>, onNone: () => unknown): A
|
|
@@ -910,12 +1240,17 @@ export const getOrThrowWith: {
|
|
|
910
1240
|
})
|
|
911
1241
|
|
|
912
1242
|
/**
|
|
913
|
-
* Extracts the value of an `Option` or throws if the `Option`
|
|
1243
|
+
* Extracts the value of an `Option` or throws a default error if the `Option`
|
|
1244
|
+
* is `None`.
|
|
1245
|
+
*
|
|
1246
|
+
* **Details**
|
|
914
1247
|
*
|
|
915
|
-
*
|
|
1248
|
+
* This function extracts the value from an `Option` if it is `Some`. If the
|
|
1249
|
+
* `Option` is `None`, it throws a default error. It is useful for fail-fast
|
|
1250
|
+
* scenarios where the absence of a value is treated as an exceptional case and
|
|
1251
|
+
* a default error is sufficient.
|
|
916
1252
|
*
|
|
917
|
-
* @
|
|
918
|
-
* @throws `Error("getOrThrow called on a None")`
|
|
1253
|
+
* @see {@link getOrThrowWith} for a version that allows you to provide a custom error.
|
|
919
1254
|
*
|
|
920
1255
|
* @example
|
|
921
1256
|
* ```ts
|
|
@@ -925,38 +1260,113 @@ export const getOrThrowWith: {
|
|
|
925
1260
|
* assert.throws(() => Option.getOrThrow(Option.none()))
|
|
926
1261
|
* ```
|
|
927
1262
|
*
|
|
928
|
-
* @category
|
|
1263
|
+
* @category Conversions
|
|
929
1264
|
* @since 2.0.0
|
|
930
1265
|
*/
|
|
931
1266
|
export const getOrThrow: <A>(self: Option<A>) => A = getOrThrowWith(() => new Error("getOrThrow called on a None"))
|
|
932
1267
|
|
|
933
1268
|
/**
|
|
934
|
-
*
|
|
1269
|
+
* Transforms the value inside a `Some` to a new value using the provided
|
|
1270
|
+
* function, while leaving `None` unchanged.
|
|
1271
|
+
*
|
|
1272
|
+
* **Details**
|
|
1273
|
+
*
|
|
1274
|
+
* This function applies a mapping function `f` to the value inside an `Option`
|
|
1275
|
+
* if it is a `Some`. If the `Option` is `None`, it remains unchanged. The
|
|
1276
|
+
* result is a new `Option` with the transformed value (if it was a `Some`) or
|
|
1277
|
+
* still `None`.
|
|
1278
|
+
*
|
|
1279
|
+
* This utility is particularly useful for chaining transformations in a
|
|
1280
|
+
* functional way without needing to manually handle `None` cases.
|
|
1281
|
+
*
|
|
1282
|
+
* @example
|
|
1283
|
+
* ```ts
|
|
1284
|
+
* import { Option } from "effect"
|
|
1285
|
+
*
|
|
1286
|
+
* // Mapping over a `Some`
|
|
1287
|
+
* const someValue = Option.some(2)
|
|
1288
|
+
*
|
|
1289
|
+
* console.log(Option.map(someValue, (n) => n * 2))
|
|
1290
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 4 }
|
|
935
1291
|
*
|
|
936
|
-
*
|
|
937
|
-
*
|
|
1292
|
+
* // Mapping over a `None`
|
|
1293
|
+
* const noneValue = Option.none<number>()
|
|
938
1294
|
*
|
|
939
|
-
*
|
|
1295
|
+
* console.log(Option.map(noneValue, (n) => n * 2))
|
|
1296
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1297
|
+
* ```
|
|
1298
|
+
*
|
|
1299
|
+
* @category Mapping
|
|
940
1300
|
* @since 2.0.0
|
|
941
1301
|
*/
|
|
942
1302
|
export const map: {
|
|
943
1303
|
/**
|
|
944
|
-
*
|
|
1304
|
+
* Transforms the value inside a `Some` to a new value using the provided
|
|
1305
|
+
* function, while leaving `None` unchanged.
|
|
1306
|
+
*
|
|
1307
|
+
* **Details**
|
|
1308
|
+
*
|
|
1309
|
+
* This function applies a mapping function `f` to the value inside an `Option`
|
|
1310
|
+
* if it is a `Some`. If the `Option` is `None`, it remains unchanged. The
|
|
1311
|
+
* result is a new `Option` with the transformed value (if it was a `Some`) or
|
|
1312
|
+
* still `None`.
|
|
945
1313
|
*
|
|
946
|
-
*
|
|
947
|
-
*
|
|
1314
|
+
* This utility is particularly useful for chaining transformations in a
|
|
1315
|
+
* functional way without needing to manually handle `None` cases.
|
|
1316
|
+
*
|
|
1317
|
+
* @example
|
|
1318
|
+
* ```ts
|
|
1319
|
+
* import { Option } from "effect"
|
|
1320
|
+
*
|
|
1321
|
+
* // Mapping over a `Some`
|
|
1322
|
+
* const someValue = Option.some(2)
|
|
1323
|
+
*
|
|
1324
|
+
* console.log(Option.map(someValue, (n) => n * 2))
|
|
1325
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 4 }
|
|
1326
|
+
*
|
|
1327
|
+
* // Mapping over a `None`
|
|
1328
|
+
* const noneValue = Option.none<number>()
|
|
1329
|
+
*
|
|
1330
|
+
* console.log(Option.map(noneValue, (n) => n * 2))
|
|
1331
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1332
|
+
* ```
|
|
948
1333
|
*
|
|
949
|
-
* @category
|
|
1334
|
+
* @category Mapping
|
|
950
1335
|
* @since 2.0.0
|
|
951
1336
|
*/
|
|
952
1337
|
<A, B>(f: (a: A) => B): (self: Option<A>) => Option<B>
|
|
953
1338
|
/**
|
|
954
|
-
*
|
|
1339
|
+
* Transforms the value inside a `Some` to a new value using the provided
|
|
1340
|
+
* function, while leaving `None` unchanged.
|
|
955
1341
|
*
|
|
956
|
-
*
|
|
957
|
-
* @param f - The function to map over the value of the `Option`
|
|
1342
|
+
* **Details**
|
|
958
1343
|
*
|
|
959
|
-
*
|
|
1344
|
+
* This function applies a mapping function `f` to the value inside an `Option`
|
|
1345
|
+
* if it is a `Some`. If the `Option` is `None`, it remains unchanged. The
|
|
1346
|
+
* result is a new `Option` with the transformed value (if it was a `Some`) or
|
|
1347
|
+
* still `None`.
|
|
1348
|
+
*
|
|
1349
|
+
* This utility is particularly useful for chaining transformations in a
|
|
1350
|
+
* functional way without needing to manually handle `None` cases.
|
|
1351
|
+
*
|
|
1352
|
+
* @example
|
|
1353
|
+
* ```ts
|
|
1354
|
+
* import { Option } from "effect"
|
|
1355
|
+
*
|
|
1356
|
+
* // Mapping over a `Some`
|
|
1357
|
+
* const someValue = Option.some(2)
|
|
1358
|
+
*
|
|
1359
|
+
* console.log(Option.map(someValue, (n) => n * 2))
|
|
1360
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 4 }
|
|
1361
|
+
*
|
|
1362
|
+
* // Mapping over a `None`
|
|
1363
|
+
* const noneValue = Option.none<number>()
|
|
1364
|
+
*
|
|
1365
|
+
* console.log(Option.map(noneValue, (n) => n * 2))
|
|
1366
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1367
|
+
* ```
|
|
1368
|
+
*
|
|
1369
|
+
* @category Mapping
|
|
960
1370
|
* @since 2.0.0
|
|
961
1371
|
*/
|
|
962
1372
|
<A, B>(self: Option<A>, f: (a: A) => B): Option<B>
|
|
@@ -966,27 +1376,122 @@ export const map: {
|
|
|
966
1376
|
)
|
|
967
1377
|
|
|
968
1378
|
/**
|
|
969
|
-
*
|
|
1379
|
+
* Replaces the value inside a `Some` with the specified constant value, leaving
|
|
1380
|
+
* `None` unchanged.
|
|
1381
|
+
*
|
|
1382
|
+
* **Details**
|
|
970
1383
|
*
|
|
971
|
-
*
|
|
1384
|
+
* This function transforms an `Option` by replacing the value inside a `Some`
|
|
1385
|
+
* with the given constant value `b`. If the `Option` is `None`, it remains
|
|
1386
|
+
* unchanged.
|
|
1387
|
+
*
|
|
1388
|
+
* This is useful when you want to preserve the presence of a value (`Some`) but
|
|
1389
|
+
* replace its content with a fixed value.
|
|
1390
|
+
*
|
|
1391
|
+
* @example
|
|
1392
|
+
* ```ts
|
|
1393
|
+
* import { Option } from "effect"
|
|
1394
|
+
*
|
|
1395
|
+
* // Replacing the value of a `Some`
|
|
1396
|
+
* const someValue = Option.some(42)
|
|
1397
|
+
*
|
|
1398
|
+
* console.log(Option.as(someValue, "new value"))
|
|
1399
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'new value' }
|
|
1400
|
+
*
|
|
1401
|
+
* // Replacing a `None` (no effect)
|
|
1402
|
+
* const noneValue = Option.none<number>()
|
|
1403
|
+
*
|
|
1404
|
+
* console.log(Option.as(noneValue, "new value"))
|
|
1405
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1406
|
+
* ```
|
|
1407
|
+
*
|
|
1408
|
+
* @category Mapping
|
|
972
1409
|
* @since 2.0.0
|
|
973
1410
|
*/
|
|
974
1411
|
export const as: {
|
|
975
1412
|
/**
|
|
976
|
-
*
|
|
1413
|
+
* Replaces the value inside a `Some` with the specified constant value, leaving
|
|
1414
|
+
* `None` unchanged.
|
|
1415
|
+
*
|
|
1416
|
+
* **Details**
|
|
1417
|
+
*
|
|
1418
|
+
* This function transforms an `Option` by replacing the value inside a `Some`
|
|
1419
|
+
* with the given constant value `b`. If the `Option` is `None`, it remains
|
|
1420
|
+
* unchanged.
|
|
977
1421
|
*
|
|
978
|
-
*
|
|
1422
|
+
* This is useful when you want to preserve the presence of a value (`Some`) but
|
|
1423
|
+
* replace its content with a fixed value.
|
|
1424
|
+
*
|
|
1425
|
+
* @example
|
|
1426
|
+
* ```ts
|
|
1427
|
+
* import { Option } from "effect"
|
|
1428
|
+
*
|
|
1429
|
+
* // Replacing the value of a `Some`
|
|
1430
|
+
* const someValue = Option.some(42)
|
|
1431
|
+
*
|
|
1432
|
+
* console.log(Option.as(someValue, "new value"))
|
|
1433
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'new value' }
|
|
1434
|
+
*
|
|
1435
|
+
* // Replacing a `None` (no effect)
|
|
1436
|
+
* const noneValue = Option.none<number>()
|
|
1437
|
+
*
|
|
1438
|
+
* console.log(Option.as(noneValue, "new value"))
|
|
1439
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1440
|
+
* ```
|
|
1441
|
+
*
|
|
1442
|
+
* @category Mapping
|
|
979
1443
|
* @since 2.0.0
|
|
980
1444
|
*/
|
|
981
1445
|
<B>(b: B): <X>(self: Option<X>) => Option<B>
|
|
1446
|
+
/**
|
|
1447
|
+
* Replaces the value inside a `Some` with the specified constant value, leaving
|
|
1448
|
+
* `None` unchanged.
|
|
1449
|
+
*
|
|
1450
|
+
* **Details**
|
|
1451
|
+
*
|
|
1452
|
+
* This function transforms an `Option` by replacing the value inside a `Some`
|
|
1453
|
+
* with the given constant value `b`. If the `Option` is `None`, it remains
|
|
1454
|
+
* unchanged.
|
|
1455
|
+
*
|
|
1456
|
+
* This is useful when you want to preserve the presence of a value (`Some`) but
|
|
1457
|
+
* replace its content with a fixed value.
|
|
1458
|
+
*
|
|
1459
|
+
* @example
|
|
1460
|
+
* ```ts
|
|
1461
|
+
* import { Option } from "effect"
|
|
1462
|
+
*
|
|
1463
|
+
* // Replacing the value of a `Some`
|
|
1464
|
+
* const someValue = Option.some(42)
|
|
1465
|
+
*
|
|
1466
|
+
* console.log(Option.as(someValue, "new value"))
|
|
1467
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'new value' }
|
|
1468
|
+
*
|
|
1469
|
+
* // Replacing a `None` (no effect)
|
|
1470
|
+
* const noneValue = Option.none<number>()
|
|
1471
|
+
*
|
|
1472
|
+
* console.log(Option.as(noneValue, "new value"))
|
|
1473
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1474
|
+
* ```
|
|
1475
|
+
*
|
|
1476
|
+
* @category Mapping
|
|
1477
|
+
* @since 2.0.0
|
|
1478
|
+
*/
|
|
1479
|
+
<X, B>(self: Option<X>, b: B): Option<B>
|
|
982
1480
|
} = dual(2, <X, B>(self: Option<X>, b: B): Option<B> => map(self, () => b))
|
|
983
1481
|
|
|
984
1482
|
/**
|
|
985
|
-
*
|
|
1483
|
+
* Replaces the value inside a `Some` with the constant value `void`, leaving
|
|
1484
|
+
* `None` unchanged.
|
|
1485
|
+
*
|
|
1486
|
+
* **Details**
|
|
1487
|
+
*
|
|
1488
|
+
* This function transforms an `Option` by replacing the value inside a `Some`
|
|
1489
|
+
* with `void`. If the `Option` is `None`, it remains unchanged.
|
|
986
1490
|
*
|
|
987
|
-
* This is useful
|
|
1491
|
+
* This is particularly useful in scenarios where the presence or absence of a
|
|
1492
|
+
* value is significant, but the actual content of the value is irrelevant.
|
|
988
1493
|
*
|
|
989
|
-
* @category
|
|
1494
|
+
* @category Mapping
|
|
990
1495
|
* @since 2.0.0
|
|
991
1496
|
*/
|
|
992
1497
|
export const asVoid: <_>(self: Option<_>) => Option<void> = as(undefined)
|
|
@@ -1000,23 +1505,164 @@ export {
|
|
|
1000
1505
|
}
|
|
1001
1506
|
|
|
1002
1507
|
/**
|
|
1003
|
-
* Applies a function to the value of
|
|
1508
|
+
* Applies a function to the value of a `Some` and flattens the resulting
|
|
1509
|
+
* `Option`. If the input is `None`, it remains `None`.
|
|
1510
|
+
*
|
|
1511
|
+
* **Details**
|
|
1512
|
+
*
|
|
1513
|
+
* This function allows you to chain computations that return `Option` values.
|
|
1514
|
+
* If the input `Option` is `Some`, the provided function `f` is applied to the
|
|
1515
|
+
* contained value, and the resulting `Option` is returned. If the input is
|
|
1516
|
+
* `None`, the function is not applied, and the result remains `None`.
|
|
1517
|
+
*
|
|
1518
|
+
* This utility is particularly useful for sequencing operations that may fail
|
|
1519
|
+
* or produce optional results, enabling clean and concise workflows for
|
|
1520
|
+
* handling such cases.
|
|
1521
|
+
*
|
|
1522
|
+
* @example
|
|
1523
|
+
* ```ts
|
|
1524
|
+
* import { Option } from "effect"
|
|
1525
|
+
*
|
|
1526
|
+
* interface Address {
|
|
1527
|
+
* readonly city: string
|
|
1528
|
+
* readonly street: Option.Option<string>
|
|
1529
|
+
* }
|
|
1004
1530
|
*
|
|
1005
|
-
*
|
|
1531
|
+
* interface User {
|
|
1532
|
+
* readonly id: number
|
|
1533
|
+
* readonly username: string
|
|
1534
|
+
* readonly email: Option.Option<string>
|
|
1535
|
+
* readonly address: Option.Option<Address>
|
|
1536
|
+
* }
|
|
1537
|
+
*
|
|
1538
|
+
* const user: User = {
|
|
1539
|
+
* id: 1,
|
|
1540
|
+
* username: "john_doe",
|
|
1541
|
+
* email: Option.some("john.doe@example.com"),
|
|
1542
|
+
* address: Option.some({
|
|
1543
|
+
* city: "New York",
|
|
1544
|
+
* street: Option.some("123 Main St")
|
|
1545
|
+
* })
|
|
1546
|
+
* }
|
|
1547
|
+
*
|
|
1548
|
+
* // Use flatMap to extract the street value
|
|
1549
|
+
* const street = user.address.pipe(
|
|
1550
|
+
* Option.flatMap((address) => address.street)
|
|
1551
|
+
* )
|
|
1552
|
+
*
|
|
1553
|
+
* console.log(street)
|
|
1554
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: '123 Main St' }
|
|
1555
|
+
* ```
|
|
1556
|
+
*
|
|
1557
|
+
* @category Sequencing
|
|
1006
1558
|
* @since 2.0.0
|
|
1007
1559
|
*/
|
|
1008
1560
|
export const flatMap: {
|
|
1009
1561
|
/**
|
|
1010
|
-
* Applies a function to the value of
|
|
1562
|
+
* Applies a function to the value of a `Some` and flattens the resulting
|
|
1563
|
+
* `Option`. If the input is `None`, it remains `None`.
|
|
1564
|
+
*
|
|
1565
|
+
* **Details**
|
|
1566
|
+
*
|
|
1567
|
+
* This function allows you to chain computations that return `Option` values.
|
|
1568
|
+
* If the input `Option` is `Some`, the provided function `f` is applied to the
|
|
1569
|
+
* contained value, and the resulting `Option` is returned. If the input is
|
|
1570
|
+
* `None`, the function is not applied, and the result remains `None`.
|
|
1571
|
+
*
|
|
1572
|
+
* This utility is particularly useful for sequencing operations that may fail
|
|
1573
|
+
* or produce optional results, enabling clean and concise workflows for
|
|
1574
|
+
* handling such cases.
|
|
1575
|
+
*
|
|
1576
|
+
* @example
|
|
1577
|
+
* ```ts
|
|
1578
|
+
* import { Option } from "effect"
|
|
1579
|
+
*
|
|
1580
|
+
* interface Address {
|
|
1581
|
+
* readonly city: string
|
|
1582
|
+
* readonly street: Option.Option<string>
|
|
1583
|
+
* }
|
|
1584
|
+
*
|
|
1585
|
+
* interface User {
|
|
1586
|
+
* readonly id: number
|
|
1587
|
+
* readonly username: string
|
|
1588
|
+
* readonly email: Option.Option<string>
|
|
1589
|
+
* readonly address: Option.Option<Address>
|
|
1590
|
+
* }
|
|
1591
|
+
*
|
|
1592
|
+
* const user: User = {
|
|
1593
|
+
* id: 1,
|
|
1594
|
+
* username: "john_doe",
|
|
1595
|
+
* email: Option.some("john.doe@example.com"),
|
|
1596
|
+
* address: Option.some({
|
|
1597
|
+
* city: "New York",
|
|
1598
|
+
* street: Option.some("123 Main St")
|
|
1599
|
+
* })
|
|
1600
|
+
* }
|
|
1011
1601
|
*
|
|
1012
|
-
*
|
|
1602
|
+
* // Use flatMap to extract the street value
|
|
1603
|
+
* const street = user.address.pipe(
|
|
1604
|
+
* Option.flatMap((address) => address.street)
|
|
1605
|
+
* )
|
|
1606
|
+
*
|
|
1607
|
+
* console.log(street)
|
|
1608
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: '123 Main St' }
|
|
1609
|
+
* ```
|
|
1610
|
+
*
|
|
1611
|
+
* @category Sequencing
|
|
1013
1612
|
* @since 2.0.0
|
|
1014
1613
|
*/
|
|
1015
1614
|
<A, B>(f: (a: A) => Option<B>): (self: Option<A>) => Option<B>
|
|
1016
1615
|
/**
|
|
1017
|
-
* Applies a function to the value of
|
|
1616
|
+
* Applies a function to the value of a `Some` and flattens the resulting
|
|
1617
|
+
* `Option`. If the input is `None`, it remains `None`.
|
|
1618
|
+
*
|
|
1619
|
+
* **Details**
|
|
1620
|
+
*
|
|
1621
|
+
* This function allows you to chain computations that return `Option` values.
|
|
1622
|
+
* If the input `Option` is `Some`, the provided function `f` is applied to the
|
|
1623
|
+
* contained value, and the resulting `Option` is returned. If the input is
|
|
1624
|
+
* `None`, the function is not applied, and the result remains `None`.
|
|
1625
|
+
*
|
|
1626
|
+
* This utility is particularly useful for sequencing operations that may fail
|
|
1627
|
+
* or produce optional results, enabling clean and concise workflows for
|
|
1628
|
+
* handling such cases.
|
|
1018
1629
|
*
|
|
1019
|
-
* @
|
|
1630
|
+
* @example
|
|
1631
|
+
* ```ts
|
|
1632
|
+
* import { Option } from "effect"
|
|
1633
|
+
*
|
|
1634
|
+
* interface Address {
|
|
1635
|
+
* readonly city: string
|
|
1636
|
+
* readonly street: Option.Option<string>
|
|
1637
|
+
* }
|
|
1638
|
+
*
|
|
1639
|
+
* interface User {
|
|
1640
|
+
* readonly id: number
|
|
1641
|
+
* readonly username: string
|
|
1642
|
+
* readonly email: Option.Option<string>
|
|
1643
|
+
* readonly address: Option.Option<Address>
|
|
1644
|
+
* }
|
|
1645
|
+
*
|
|
1646
|
+
* const user: User = {
|
|
1647
|
+
* id: 1,
|
|
1648
|
+
* username: "john_doe",
|
|
1649
|
+
* email: Option.some("john.doe@example.com"),
|
|
1650
|
+
* address: Option.some({
|
|
1651
|
+
* city: "New York",
|
|
1652
|
+
* street: Option.some("123 Main St")
|
|
1653
|
+
* })
|
|
1654
|
+
* }
|
|
1655
|
+
*
|
|
1656
|
+
* // Use flatMap to extract the street value
|
|
1657
|
+
* const street = user.address.pipe(
|
|
1658
|
+
* Option.flatMap((address) => address.street)
|
|
1659
|
+
* )
|
|
1660
|
+
*
|
|
1661
|
+
* console.log(street)
|
|
1662
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: '123 Main St' }
|
|
1663
|
+
* ```
|
|
1664
|
+
*
|
|
1665
|
+
* @category Sequencing
|
|
1020
1666
|
* @since 2.0.0
|
|
1021
1667
|
*/
|
|
1022
1668
|
<A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>
|
|
@@ -1026,65 +1672,191 @@ export const flatMap: {
|
|
|
1026
1672
|
)
|
|
1027
1673
|
|
|
1028
1674
|
/**
|
|
1029
|
-
*
|
|
1675
|
+
* Chains two `Option`s together. The second `Option` can either be a static
|
|
1676
|
+
* value or depend on the result of the first `Option`.
|
|
1677
|
+
*
|
|
1678
|
+
* **Details**
|
|
1030
1679
|
*
|
|
1031
|
-
*
|
|
1680
|
+
* This function enables sequencing of two `Option` computations. If the first
|
|
1681
|
+
* `Option` is `Some`, the second `Option` is evaluated. The second `Option` can
|
|
1682
|
+
* either:
|
|
1683
|
+
*
|
|
1684
|
+
* - Be a static `Option` value.
|
|
1685
|
+
* - Be a function that produces an `Option`, optionally based on the value of
|
|
1686
|
+
* the first `Option`.
|
|
1687
|
+
*
|
|
1688
|
+
* If the first `Option` is `None`, the function skips the evaluation of the
|
|
1689
|
+
* second `Option` and directly returns `None`.
|
|
1690
|
+
*
|
|
1691
|
+
* @category Sequencing
|
|
1032
1692
|
* @since 2.0.0
|
|
1033
1693
|
*/
|
|
1034
1694
|
export const andThen: {
|
|
1035
1695
|
/**
|
|
1036
|
-
*
|
|
1696
|
+
* Chains two `Option`s together. The second `Option` can either be a static
|
|
1697
|
+
* value or depend on the result of the first `Option`.
|
|
1698
|
+
*
|
|
1699
|
+
* **Details**
|
|
1700
|
+
*
|
|
1701
|
+
* This function enables sequencing of two `Option` computations. If the first
|
|
1702
|
+
* `Option` is `Some`, the second `Option` is evaluated. The second `Option` can
|
|
1703
|
+
* either:
|
|
1704
|
+
*
|
|
1705
|
+
* - Be a static `Option` value.
|
|
1706
|
+
* - Be a function that produces an `Option`, optionally based on the value of
|
|
1707
|
+
* the first `Option`.
|
|
1037
1708
|
*
|
|
1038
|
-
*
|
|
1709
|
+
* If the first `Option` is `None`, the function skips the evaluation of the
|
|
1710
|
+
* second `Option` and directly returns `None`.
|
|
1711
|
+
*
|
|
1712
|
+
* @category Sequencing
|
|
1039
1713
|
* @since 2.0.0
|
|
1040
1714
|
*/
|
|
1041
1715
|
<A, B>(f: (a: A) => Option<B>): (self: Option<A>) => Option<B>
|
|
1042
1716
|
/**
|
|
1043
|
-
*
|
|
1717
|
+
* Chains two `Option`s together. The second `Option` can either be a static
|
|
1718
|
+
* value or depend on the result of the first `Option`.
|
|
1719
|
+
*
|
|
1720
|
+
* **Details**
|
|
1721
|
+
*
|
|
1722
|
+
* This function enables sequencing of two `Option` computations. If the first
|
|
1723
|
+
* `Option` is `Some`, the second `Option` is evaluated. The second `Option` can
|
|
1724
|
+
* either:
|
|
1044
1725
|
*
|
|
1045
|
-
*
|
|
1726
|
+
* - Be a static `Option` value.
|
|
1727
|
+
* - Be a function that produces an `Option`, optionally based on the value of
|
|
1728
|
+
* the first `Option`.
|
|
1729
|
+
*
|
|
1730
|
+
* If the first `Option` is `None`, the function skips the evaluation of the
|
|
1731
|
+
* second `Option` and directly returns `None`.
|
|
1732
|
+
*
|
|
1733
|
+
* @category Sequencing
|
|
1046
1734
|
* @since 2.0.0
|
|
1047
1735
|
*/
|
|
1048
1736
|
<B>(f: Option<B>): <A>(self: Option<A>) => Option<B>
|
|
1049
1737
|
/**
|
|
1050
|
-
*
|
|
1738
|
+
* Chains two `Option`s together. The second `Option` can either be a static
|
|
1739
|
+
* value or depend on the result of the first `Option`.
|
|
1740
|
+
*
|
|
1741
|
+
* **Details**
|
|
1051
1742
|
*
|
|
1052
|
-
*
|
|
1743
|
+
* This function enables sequencing of two `Option` computations. If the first
|
|
1744
|
+
* `Option` is `Some`, the second `Option` is evaluated. The second `Option` can
|
|
1745
|
+
* either:
|
|
1746
|
+
*
|
|
1747
|
+
* - Be a static `Option` value.
|
|
1748
|
+
* - Be a function that produces an `Option`, optionally based on the value of
|
|
1749
|
+
* the first `Option`.
|
|
1750
|
+
*
|
|
1751
|
+
* If the first `Option` is `None`, the function skips the evaluation of the
|
|
1752
|
+
* second `Option` and directly returns `None`.
|
|
1753
|
+
*
|
|
1754
|
+
* @category Sequencing
|
|
1053
1755
|
* @since 2.0.0
|
|
1054
1756
|
*/
|
|
1055
1757
|
<A, B>(f: (a: A) => B): (self: Option<A>) => Option<B>
|
|
1056
1758
|
/**
|
|
1057
|
-
*
|
|
1759
|
+
* Chains two `Option`s together. The second `Option` can either be a static
|
|
1760
|
+
* value or depend on the result of the first `Option`.
|
|
1761
|
+
*
|
|
1762
|
+
* **Details**
|
|
1763
|
+
*
|
|
1764
|
+
* This function enables sequencing of two `Option` computations. If the first
|
|
1765
|
+
* `Option` is `Some`, the second `Option` is evaluated. The second `Option` can
|
|
1766
|
+
* either:
|
|
1767
|
+
*
|
|
1768
|
+
* - Be a static `Option` value.
|
|
1769
|
+
* - Be a function that produces an `Option`, optionally based on the value of
|
|
1770
|
+
* the first `Option`.
|
|
1058
1771
|
*
|
|
1059
|
-
*
|
|
1772
|
+
* If the first `Option` is `None`, the function skips the evaluation of the
|
|
1773
|
+
* second `Option` and directly returns `None`.
|
|
1774
|
+
*
|
|
1775
|
+
* @category Sequencing
|
|
1060
1776
|
* @since 2.0.0
|
|
1061
1777
|
*/
|
|
1062
1778
|
<B>(f: NotFunction<B>): <A>(self: Option<A>) => Option<B>
|
|
1063
1779
|
/**
|
|
1064
|
-
*
|
|
1780
|
+
* Chains two `Option`s together. The second `Option` can either be a static
|
|
1781
|
+
* value or depend on the result of the first `Option`.
|
|
1782
|
+
*
|
|
1783
|
+
* **Details**
|
|
1784
|
+
*
|
|
1785
|
+
* This function enables sequencing of two `Option` computations. If the first
|
|
1786
|
+
* `Option` is `Some`, the second `Option` is evaluated. The second `Option` can
|
|
1787
|
+
* either:
|
|
1065
1788
|
*
|
|
1066
|
-
*
|
|
1789
|
+
* - Be a static `Option` value.
|
|
1790
|
+
* - Be a function that produces an `Option`, optionally based on the value of
|
|
1791
|
+
* the first `Option`.
|
|
1792
|
+
*
|
|
1793
|
+
* If the first `Option` is `None`, the function skips the evaluation of the
|
|
1794
|
+
* second `Option` and directly returns `None`.
|
|
1795
|
+
*
|
|
1796
|
+
* @category Sequencing
|
|
1067
1797
|
* @since 2.0.0
|
|
1068
1798
|
*/
|
|
1069
1799
|
<A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>
|
|
1070
1800
|
/**
|
|
1071
|
-
*
|
|
1801
|
+
* Chains two `Option`s together. The second `Option` can either be a static
|
|
1802
|
+
* value or depend on the result of the first `Option`.
|
|
1803
|
+
*
|
|
1804
|
+
* **Details**
|
|
1072
1805
|
*
|
|
1073
|
-
*
|
|
1806
|
+
* This function enables sequencing of two `Option` computations. If the first
|
|
1807
|
+
* `Option` is `Some`, the second `Option` is evaluated. The second `Option` can
|
|
1808
|
+
* either:
|
|
1809
|
+
*
|
|
1810
|
+
* - Be a static `Option` value.
|
|
1811
|
+
* - Be a function that produces an `Option`, optionally based on the value of
|
|
1812
|
+
* the first `Option`.
|
|
1813
|
+
*
|
|
1814
|
+
* If the first `Option` is `None`, the function skips the evaluation of the
|
|
1815
|
+
* second `Option` and directly returns `None`.
|
|
1816
|
+
*
|
|
1817
|
+
* @category Sequencing
|
|
1074
1818
|
* @since 2.0.0
|
|
1075
1819
|
*/
|
|
1076
1820
|
<A, B>(self: Option<A>, f: Option<B>): Option<B>
|
|
1077
1821
|
/**
|
|
1078
|
-
*
|
|
1822
|
+
* Chains two `Option`s together. The second `Option` can either be a static
|
|
1823
|
+
* value or depend on the result of the first `Option`.
|
|
1824
|
+
*
|
|
1825
|
+
* **Details**
|
|
1826
|
+
*
|
|
1827
|
+
* This function enables sequencing of two `Option` computations. If the first
|
|
1828
|
+
* `Option` is `Some`, the second `Option` is evaluated. The second `Option` can
|
|
1829
|
+
* either:
|
|
1830
|
+
*
|
|
1831
|
+
* - Be a static `Option` value.
|
|
1832
|
+
* - Be a function that produces an `Option`, optionally based on the value of
|
|
1833
|
+
* the first `Option`.
|
|
1079
1834
|
*
|
|
1080
|
-
*
|
|
1835
|
+
* If the first `Option` is `None`, the function skips the evaluation of the
|
|
1836
|
+
* second `Option` and directly returns `None`.
|
|
1837
|
+
*
|
|
1838
|
+
* @category Sequencing
|
|
1081
1839
|
* @since 2.0.0
|
|
1082
1840
|
*/
|
|
1083
1841
|
<A, B>(self: Option<A>, f: (a: A) => B): Option<B>
|
|
1084
1842
|
/**
|
|
1085
|
-
*
|
|
1843
|
+
* Chains two `Option`s together. The second `Option` can either be a static
|
|
1844
|
+
* value or depend on the result of the first `Option`.
|
|
1845
|
+
*
|
|
1846
|
+
* **Details**
|
|
1847
|
+
*
|
|
1848
|
+
* This function enables sequencing of two `Option` computations. If the first
|
|
1849
|
+
* `Option` is `Some`, the second `Option` is evaluated. The second `Option` can
|
|
1850
|
+
* either:
|
|
1851
|
+
*
|
|
1852
|
+
* - Be a static `Option` value.
|
|
1853
|
+
* - Be a function that produces an `Option`, optionally based on the value of
|
|
1854
|
+
* the first `Option`.
|
|
1086
1855
|
*
|
|
1087
|
-
*
|
|
1856
|
+
* If the first `Option` is `None`, the function skips the evaluation of the
|
|
1857
|
+
* second `Option` and directly returns `None`.
|
|
1858
|
+
*
|
|
1859
|
+
* @category Sequencing
|
|
1088
1860
|
* @since 2.0.0
|
|
1089
1861
|
*/
|
|
1090
1862
|
<A, B>(self: Option<A>, f: NotFunction<B>): Option<B>
|
|
@@ -1098,11 +1870,24 @@ export const andThen: {
|
|
|
1098
1870
|
)
|
|
1099
1871
|
|
|
1100
1872
|
/**
|
|
1101
|
-
*
|
|
1873
|
+
* Combines `flatMap` and `fromNullable`, transforming the value inside a `Some`
|
|
1874
|
+
* using a function that may return `null` or `undefined`.
|
|
1875
|
+
*
|
|
1876
|
+
* **Details**
|
|
1877
|
+
*
|
|
1878
|
+
* This function applies a transformation function `f` to the value inside a
|
|
1879
|
+
* `Some`. The function `f` may return a value, `null`, or `undefined`. If `f`
|
|
1880
|
+
* returns a value, it is wrapped in a `Some`. If `f` returns `null` or
|
|
1881
|
+
* `undefined`, the result is `None`. If the input `Option` is `None`, the
|
|
1882
|
+
* function is not applied, and `None` is returned.
|
|
1883
|
+
*
|
|
1884
|
+
* This utility is particularly useful when working with deeply nested optional
|
|
1885
|
+
* values or chaining computations that may result in `null` or `undefined` at
|
|
1886
|
+
* some point.
|
|
1102
1887
|
*
|
|
1103
1888
|
* @example
|
|
1104
1889
|
* ```ts
|
|
1105
|
-
* import {
|
|
1890
|
+
* import { Option } from "effect"
|
|
1106
1891
|
*
|
|
1107
1892
|
* interface Employee {
|
|
1108
1893
|
* company?: {
|
|
@@ -1114,37 +1899,48 @@ export const andThen: {
|
|
|
1114
1899
|
* }
|
|
1115
1900
|
* }
|
|
1116
1901
|
*
|
|
1117
|
-
* const employee1: Employee = { company: { address: { street: { name:
|
|
1902
|
+
* const employee1: Employee = { company: { address: { street: { name: "high street" } } } }
|
|
1118
1903
|
*
|
|
1119
|
-
*
|
|
1120
|
-
*
|
|
1121
|
-
*
|
|
1122
|
-
* Option.flatMapNullable(employee => employee.company?.address?.street?.name)
|
|
1123
|
-
* ),
|
|
1124
|
-
* Option.some('high street')
|
|
1904
|
+
* // Extracting a deeply nested property
|
|
1905
|
+
* console.log(
|
|
1906
|
+
* Option.some(employee1)
|
|
1907
|
+
* .pipe(Option.flatMapNullable((employee) => employee.company?.address?.street?.name))
|
|
1125
1908
|
* )
|
|
1909
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'high street' }
|
|
1126
1910
|
*
|
|
1127
1911
|
* const employee2: Employee = { company: { address: { street: {} } } }
|
|
1128
1912
|
*
|
|
1129
|
-
*
|
|
1130
|
-
*
|
|
1131
|
-
*
|
|
1132
|
-
* Option.flatMapNullable(employee => employee.company?.address?.street?.name)
|
|
1133
|
-
* ),
|
|
1134
|
-
* Option.none()
|
|
1913
|
+
* // Property does not exist
|
|
1914
|
+
* console.log(
|
|
1915
|
+
* Option.some(employee2)
|
|
1916
|
+
* .pipe(Option.flatMapNullable((employee) => employee.company?.address?.street?.name))
|
|
1135
1917
|
* )
|
|
1918
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1136
1919
|
* ```
|
|
1137
1920
|
*
|
|
1138
|
-
* @category
|
|
1921
|
+
* @category Sequencing
|
|
1139
1922
|
* @since 2.0.0
|
|
1140
1923
|
*/
|
|
1141
1924
|
export const flatMapNullable: {
|
|
1142
1925
|
/**
|
|
1143
|
-
*
|
|
1926
|
+
* Combines `flatMap` and `fromNullable`, transforming the value inside a `Some`
|
|
1927
|
+
* using a function that may return `null` or `undefined`.
|
|
1928
|
+
*
|
|
1929
|
+
* **Details**
|
|
1930
|
+
*
|
|
1931
|
+
* This function applies a transformation function `f` to the value inside a
|
|
1932
|
+
* `Some`. The function `f` may return a value, `null`, or `undefined`. If `f`
|
|
1933
|
+
* returns a value, it is wrapped in a `Some`. If `f` returns `null` or
|
|
1934
|
+
* `undefined`, the result is `None`. If the input `Option` is `None`, the
|
|
1935
|
+
* function is not applied, and `None` is returned.
|
|
1936
|
+
*
|
|
1937
|
+
* This utility is particularly useful when working with deeply nested optional
|
|
1938
|
+
* values or chaining computations that may result in `null` or `undefined` at
|
|
1939
|
+
* some point.
|
|
1144
1940
|
*
|
|
1145
1941
|
* @example
|
|
1146
1942
|
* ```ts
|
|
1147
|
-
* import {
|
|
1943
|
+
* import { Option } from "effect"
|
|
1148
1944
|
*
|
|
1149
1945
|
* interface Employee {
|
|
1150
1946
|
* company?: {
|
|
@@ -1156,37 +1952,48 @@ export const flatMapNullable: {
|
|
|
1156
1952
|
* }
|
|
1157
1953
|
* }
|
|
1158
1954
|
*
|
|
1159
|
-
* const employee1: Employee = { company: { address: { street: { name:
|
|
1955
|
+
* const employee1: Employee = { company: { address: { street: { name: "high street" } } } }
|
|
1160
1956
|
*
|
|
1161
|
-
*
|
|
1162
|
-
*
|
|
1163
|
-
*
|
|
1164
|
-
* Option.flatMapNullable(employee => employee.company?.address?.street?.name)
|
|
1165
|
-
* ),
|
|
1166
|
-
* Option.some('high street')
|
|
1957
|
+
* // Extracting a deeply nested property
|
|
1958
|
+
* console.log(
|
|
1959
|
+
* Option.some(employee1)
|
|
1960
|
+
* .pipe(Option.flatMapNullable((employee) => employee.company?.address?.street?.name))
|
|
1167
1961
|
* )
|
|
1962
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'high street' }
|
|
1168
1963
|
*
|
|
1169
1964
|
* const employee2: Employee = { company: { address: { street: {} } } }
|
|
1170
1965
|
*
|
|
1171
|
-
*
|
|
1172
|
-
*
|
|
1173
|
-
*
|
|
1174
|
-
* Option.flatMapNullable(employee => employee.company?.address?.street?.name)
|
|
1175
|
-
* ),
|
|
1176
|
-
* Option.none()
|
|
1966
|
+
* // Property does not exist
|
|
1967
|
+
* console.log(
|
|
1968
|
+
* Option.some(employee2)
|
|
1969
|
+
* .pipe(Option.flatMapNullable((employee) => employee.company?.address?.street?.name))
|
|
1177
1970
|
* )
|
|
1971
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1178
1972
|
* ```
|
|
1179
1973
|
*
|
|
1180
|
-
* @category
|
|
1974
|
+
* @category Sequencing
|
|
1181
1975
|
* @since 2.0.0
|
|
1182
1976
|
*/
|
|
1183
1977
|
<A, B>(f: (a: A) => B | null | undefined): (self: Option<A>) => Option<NonNullable<B>>
|
|
1184
1978
|
/**
|
|
1185
|
-
*
|
|
1979
|
+
* Combines `flatMap` and `fromNullable`, transforming the value inside a `Some`
|
|
1980
|
+
* using a function that may return `null` or `undefined`.
|
|
1981
|
+
*
|
|
1982
|
+
* **Details**
|
|
1983
|
+
*
|
|
1984
|
+
* This function applies a transformation function `f` to the value inside a
|
|
1985
|
+
* `Some`. The function `f` may return a value, `null`, or `undefined`. If `f`
|
|
1986
|
+
* returns a value, it is wrapped in a `Some`. If `f` returns `null` or
|
|
1987
|
+
* `undefined`, the result is `None`. If the input `Option` is `None`, the
|
|
1988
|
+
* function is not applied, and `None` is returned.
|
|
1989
|
+
*
|
|
1990
|
+
* This utility is particularly useful when working with deeply nested optional
|
|
1991
|
+
* values or chaining computations that may result in `null` or `undefined` at
|
|
1992
|
+
* some point.
|
|
1186
1993
|
*
|
|
1187
1994
|
* @example
|
|
1188
1995
|
* ```ts
|
|
1189
|
-
* import {
|
|
1996
|
+
* import { Option } from "effect"
|
|
1190
1997
|
*
|
|
1191
1998
|
* interface Employee {
|
|
1192
1999
|
* company?: {
|
|
@@ -1198,28 +2005,26 @@ export const flatMapNullable: {
|
|
|
1198
2005
|
* }
|
|
1199
2006
|
* }
|
|
1200
2007
|
*
|
|
1201
|
-
* const employee1: Employee = { company: { address: { street: { name:
|
|
2008
|
+
* const employee1: Employee = { company: { address: { street: { name: "high street" } } } }
|
|
1202
2009
|
*
|
|
1203
|
-
*
|
|
1204
|
-
*
|
|
1205
|
-
*
|
|
1206
|
-
* Option.flatMapNullable(employee => employee.company?.address?.street?.name)
|
|
1207
|
-
* ),
|
|
1208
|
-
* Option.some('high street')
|
|
2010
|
+
* // Extracting a deeply nested property
|
|
2011
|
+
* console.log(
|
|
2012
|
+
* Option.some(employee1)
|
|
2013
|
+
* .pipe(Option.flatMapNullable((employee) => employee.company?.address?.street?.name))
|
|
1209
2014
|
* )
|
|
2015
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'high street' }
|
|
1210
2016
|
*
|
|
1211
2017
|
* const employee2: Employee = { company: { address: { street: {} } } }
|
|
1212
2018
|
*
|
|
1213
|
-
*
|
|
1214
|
-
*
|
|
1215
|
-
*
|
|
1216
|
-
* Option.flatMapNullable(employee => employee.company?.address?.street?.name)
|
|
1217
|
-
* ),
|
|
1218
|
-
* Option.none()
|
|
2019
|
+
* // Property does not exist
|
|
2020
|
+
* console.log(
|
|
2021
|
+
* Option.some(employee2)
|
|
2022
|
+
* .pipe(Option.flatMapNullable((employee) => employee.company?.address?.street?.name))
|
|
1219
2023
|
* )
|
|
2024
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1220
2025
|
* ```
|
|
1221
2026
|
*
|
|
1222
|
-
* @category
|
|
2027
|
+
* @category Sequencing
|
|
1223
2028
|
* @since 2.0.0
|
|
1224
2029
|
*/
|
|
1225
2030
|
<A, B>(self: Option<A>, f: (a: A) => B | null | undefined): Option<NonNullable<B>>
|
|
@@ -1230,91 +2035,258 @@ export const flatMapNullable: {
|
|
|
1230
2035
|
)
|
|
1231
2036
|
|
|
1232
2037
|
/**
|
|
1233
|
-
*
|
|
2038
|
+
* Flattens an `Option` of `Option` into a single `Option`.
|
|
2039
|
+
*
|
|
2040
|
+
* **Details**
|
|
2041
|
+
*
|
|
2042
|
+
* This function takes an `Option` that wraps another `Option` and flattens it
|
|
2043
|
+
* into a single `Option`. If the outer `Option` is `Some`, the function
|
|
2044
|
+
* extracts the inner `Option`. If the outer `Option` is `None`, the result
|
|
2045
|
+
* remains `None`.
|
|
2046
|
+
*
|
|
2047
|
+
* This is useful for simplifying nested `Option` structures that may arise
|
|
2048
|
+
* during functional operations.
|
|
2049
|
+
*
|
|
2050
|
+
* @category Sequencing
|
|
1234
2051
|
* @since 2.0.0
|
|
1235
2052
|
*/
|
|
1236
2053
|
export const flatten: <A>(self: Option<Option<A>>) => Option<A> = flatMap(identity)
|
|
1237
2054
|
|
|
1238
2055
|
/**
|
|
1239
|
-
*
|
|
2056
|
+
* Combines two `Option`s, keeping the value from the second `Option` if both
|
|
2057
|
+
* are `Some`.
|
|
2058
|
+
*
|
|
2059
|
+
* **Details**
|
|
2060
|
+
*
|
|
2061
|
+
* This function takes two `Option`s and returns the second one if the first is
|
|
2062
|
+
* `Some`. If the first `Option` is `None`, the result will also be `None`,
|
|
2063
|
+
* regardless of the second `Option`. It effectively "zips" the two `Option`s
|
|
2064
|
+
* while discarding the value from the first `Option`.
|
|
2065
|
+
*
|
|
2066
|
+
* This is particularly useful when sequencing computations where the result of
|
|
2067
|
+
* the first computation is not needed, and you only care about the result of
|
|
2068
|
+
* the second computation.
|
|
2069
|
+
*
|
|
2070
|
+
* @category Zipping
|
|
1240
2071
|
* @since 2.0.0
|
|
1241
2072
|
*/
|
|
1242
2073
|
export const zipRight: {
|
|
1243
2074
|
/**
|
|
1244
|
-
*
|
|
2075
|
+
* Combines two `Option`s, keeping the value from the second `Option` if both
|
|
2076
|
+
* are `Some`.
|
|
2077
|
+
*
|
|
2078
|
+
* **Details**
|
|
2079
|
+
*
|
|
2080
|
+
* This function takes two `Option`s and returns the second one if the first is
|
|
2081
|
+
* `Some`. If the first `Option` is `None`, the result will also be `None`,
|
|
2082
|
+
* regardless of the second `Option`. It effectively "zips" the two `Option`s
|
|
2083
|
+
* while discarding the value from the first `Option`.
|
|
2084
|
+
*
|
|
2085
|
+
* This is particularly useful when sequencing computations where the result of
|
|
2086
|
+
* the first computation is not needed, and you only care about the result of
|
|
2087
|
+
* the second computation.
|
|
2088
|
+
*
|
|
2089
|
+
* @category Zipping
|
|
1245
2090
|
* @since 2.0.0
|
|
1246
2091
|
*/
|
|
1247
2092
|
<B>(that: Option<B>): <_>(self: Option<_>) => Option<B>
|
|
1248
2093
|
/**
|
|
1249
|
-
*
|
|
2094
|
+
* Combines two `Option`s, keeping the value from the second `Option` if both
|
|
2095
|
+
* are `Some`.
|
|
2096
|
+
*
|
|
2097
|
+
* **Details**
|
|
2098
|
+
*
|
|
2099
|
+
* This function takes two `Option`s and returns the second one if the first is
|
|
2100
|
+
* `Some`. If the first `Option` is `None`, the result will also be `None`,
|
|
2101
|
+
* regardless of the second `Option`. It effectively "zips" the two `Option`s
|
|
2102
|
+
* while discarding the value from the first `Option`.
|
|
2103
|
+
*
|
|
2104
|
+
* This is particularly useful when sequencing computations where the result of
|
|
2105
|
+
* the first computation is not needed, and you only care about the result of
|
|
2106
|
+
* the second computation.
|
|
2107
|
+
*
|
|
2108
|
+
* @category Zipping
|
|
1250
2109
|
* @since 2.0.0
|
|
1251
2110
|
*/
|
|
1252
2111
|
<X, B>(self: Option<X>, that: Option<B>): Option<B>
|
|
1253
2112
|
} = dual(2, <X, B>(self: Option<X>, that: Option<B>): Option<B> => flatMap(self, () => that))
|
|
1254
2113
|
|
|
1255
2114
|
/**
|
|
1256
|
-
*
|
|
2115
|
+
* Combines two `Option`s, keeping the value from the first `Option` if both are
|
|
2116
|
+
* `Some`.
|
|
2117
|
+
*
|
|
2118
|
+
* **Details**
|
|
2119
|
+
*
|
|
2120
|
+
* This function takes two `Option`s and returns the first one if it is `Some`.
|
|
2121
|
+
* If either the first `Option` or the second `Option` is `None`, the result
|
|
2122
|
+
* will be `None`. This operation "zips" the two `Option`s while discarding the
|
|
2123
|
+
* value from the second `Option`.
|
|
2124
|
+
*
|
|
2125
|
+
* This is useful when sequencing computations where the second `Option`
|
|
2126
|
+
* represents a dependency or condition that must hold, but its value is
|
|
2127
|
+
* irrelevant.
|
|
2128
|
+
*
|
|
2129
|
+
* @category Zipping
|
|
1257
2130
|
* @since 2.0.0
|
|
1258
2131
|
*/
|
|
1259
|
-
export const
|
|
2132
|
+
export const zipLeft: {
|
|
1260
2133
|
/**
|
|
1261
|
-
*
|
|
2134
|
+
* Combines two `Option`s, keeping the value from the first `Option` if both are
|
|
2135
|
+
* `Some`.
|
|
2136
|
+
*
|
|
2137
|
+
* **Details**
|
|
2138
|
+
*
|
|
2139
|
+
* This function takes two `Option`s and returns the first one if it is `Some`.
|
|
2140
|
+
* If either the first `Option` or the second `Option` is `None`, the result
|
|
2141
|
+
* will be `None`. This operation "zips" the two `Option`s while discarding the
|
|
2142
|
+
* value from the second `Option`.
|
|
2143
|
+
*
|
|
2144
|
+
* This is useful when sequencing computations where the second `Option`
|
|
2145
|
+
* represents a dependency or condition that must hold, but its value is
|
|
2146
|
+
* irrelevant.
|
|
2147
|
+
*
|
|
2148
|
+
* @category Zipping
|
|
1262
2149
|
* @since 2.0.0
|
|
1263
2150
|
*/
|
|
1264
|
-
<
|
|
2151
|
+
<_>(that: Option<_>): <A>(self: Option<A>) => Option<A>
|
|
1265
2152
|
/**
|
|
1266
|
-
*
|
|
2153
|
+
* Combines two `Option`s, keeping the value from the first `Option` if both are
|
|
2154
|
+
* `Some`.
|
|
2155
|
+
*
|
|
2156
|
+
* **Details**
|
|
2157
|
+
*
|
|
2158
|
+
* This function takes two `Option`s and returns the first one if it is `Some`.
|
|
2159
|
+
* If either the first `Option` or the second `Option` is `None`, the result
|
|
2160
|
+
* will be `None`. This operation "zips" the two `Option`s while discarding the
|
|
2161
|
+
* value from the second `Option`.
|
|
2162
|
+
*
|
|
2163
|
+
* This is useful when sequencing computations where the second `Option`
|
|
2164
|
+
* represents a dependency or condition that must hold, but its value is
|
|
2165
|
+
* irrelevant.
|
|
2166
|
+
*
|
|
2167
|
+
* @category Zipping
|
|
1267
2168
|
* @since 2.0.0
|
|
1268
2169
|
*/
|
|
1269
|
-
<A,
|
|
1270
|
-
} = dual(2, <A,
|
|
2170
|
+
<A, X>(self: Option<A>, that: Option<X>): Option<A>
|
|
2171
|
+
} = dual(2, <A, X>(self: Option<A>, that: Option<X>): Option<A> => tap(self, () => that))
|
|
1271
2172
|
|
|
1272
2173
|
/**
|
|
1273
|
-
*
|
|
2174
|
+
* Composes two functions that return `Option` values, creating a new function
|
|
2175
|
+
* that chains them together.
|
|
2176
|
+
*
|
|
2177
|
+
* **Details**
|
|
2178
|
+
*
|
|
2179
|
+
* This function allows you to compose two computations, each represented by a
|
|
2180
|
+
* function that returns an `Option`. The result of the first function is passed
|
|
2181
|
+
* to the second function if it is `Some`. If the first function returns `None`,
|
|
2182
|
+
* the composed function short-circuits and returns `None` without invoking the
|
|
2183
|
+
* second function.
|
|
2184
|
+
*
|
|
2185
|
+
* @example
|
|
2186
|
+
* ```ts
|
|
2187
|
+
* import { Option } from "effect"
|
|
1274
2188
|
*
|
|
1275
|
-
*
|
|
2189
|
+
* const parse = (s: string): Option.Option<number> => isNaN(Number(s)) ? Option.none() : Option.some(Number(s))
|
|
1276
2190
|
*
|
|
1277
|
-
*
|
|
1278
|
-
* @param self - The `Option` we care about
|
|
2191
|
+
* const double = (n: number): Option.Option<number> => n > 0 ? Option.some(n * 2) : Option.none()
|
|
1279
2192
|
*
|
|
1280
|
-
*
|
|
2193
|
+
* const parseAndDouble = Option.composeK(parse, double)
|
|
2194
|
+
*
|
|
2195
|
+
* console.log(parseAndDouble("42"))
|
|
2196
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 84 }
|
|
2197
|
+
*
|
|
2198
|
+
* console.log(parseAndDouble("not a number"))
|
|
2199
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
2200
|
+
* ```
|
|
2201
|
+
*
|
|
2202
|
+
* @category Sequencing
|
|
1281
2203
|
* @since 2.0.0
|
|
1282
2204
|
*/
|
|
1283
|
-
export const
|
|
2205
|
+
export const composeK: {
|
|
1284
2206
|
/**
|
|
1285
|
-
*
|
|
2207
|
+
* Composes two functions that return `Option` values, creating a new function
|
|
2208
|
+
* that chains them together.
|
|
2209
|
+
*
|
|
2210
|
+
* **Details**
|
|
2211
|
+
*
|
|
2212
|
+
* This function allows you to compose two computations, each represented by a
|
|
2213
|
+
* function that returns an `Option`. The result of the first function is passed
|
|
2214
|
+
* to the second function if it is `Some`. If the first function returns `None`,
|
|
2215
|
+
* the composed function short-circuits and returns `None` without invoking the
|
|
2216
|
+
* second function.
|
|
1286
2217
|
*
|
|
1287
|
-
*
|
|
2218
|
+
* @example
|
|
2219
|
+
* ```ts
|
|
2220
|
+
* import { Option } from "effect"
|
|
2221
|
+
*
|
|
2222
|
+
* const parse = (s: string): Option.Option<number> => isNaN(Number(s)) ? Option.none() : Option.some(Number(s))
|
|
1288
2223
|
*
|
|
1289
|
-
*
|
|
1290
|
-
*
|
|
2224
|
+
* const double = (n: number): Option.Option<number> => n > 0 ? Option.some(n * 2) : Option.none()
|
|
2225
|
+
*
|
|
2226
|
+
* const parseAndDouble = Option.composeK(parse, double)
|
|
2227
|
+
*
|
|
2228
|
+
* console.log(parseAndDouble("42"))
|
|
2229
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 84 }
|
|
2230
|
+
*
|
|
2231
|
+
* console.log(parseAndDouble("not a number"))
|
|
2232
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
2233
|
+
* ```
|
|
1291
2234
|
*
|
|
1292
|
-
* @category
|
|
2235
|
+
* @category Sequencing
|
|
1293
2236
|
* @since 2.0.0
|
|
1294
2237
|
*/
|
|
1295
|
-
<
|
|
2238
|
+
<B, C>(bfc: (b: B) => Option<C>): <A>(afb: (a: A) => Option<B>) => (a: A) => Option<C>
|
|
1296
2239
|
/**
|
|
1297
|
-
*
|
|
2240
|
+
* Composes two functions that return `Option` values, creating a new function
|
|
2241
|
+
* that chains them together.
|
|
2242
|
+
*
|
|
2243
|
+
* **Details**
|
|
2244
|
+
*
|
|
2245
|
+
* This function allows you to compose two computations, each represented by a
|
|
2246
|
+
* function that returns an `Option`. The result of the first function is passed
|
|
2247
|
+
* to the second function if it is `Some`. If the first function returns `None`,
|
|
2248
|
+
* the composed function short-circuits and returns `None` without invoking the
|
|
2249
|
+
* second function.
|
|
2250
|
+
*
|
|
2251
|
+
* @example
|
|
2252
|
+
* ```ts
|
|
2253
|
+
* import { Option } from "effect"
|
|
1298
2254
|
*
|
|
1299
|
-
*
|
|
2255
|
+
* const parse = (s: string): Option.Option<number> => isNaN(Number(s)) ? Option.none() : Option.some(Number(s))
|
|
1300
2256
|
*
|
|
1301
|
-
*
|
|
1302
|
-
* @param self - The `Option` we care about
|
|
2257
|
+
* const double = (n: number): Option.Option<number> => n > 0 ? Option.some(n * 2) : Option.none()
|
|
1303
2258
|
*
|
|
1304
|
-
*
|
|
2259
|
+
* const parseAndDouble = Option.composeK(parse, double)
|
|
2260
|
+
*
|
|
2261
|
+
* console.log(parseAndDouble("42"))
|
|
2262
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 84 }
|
|
2263
|
+
*
|
|
2264
|
+
* console.log(parseAndDouble("not a number"))
|
|
2265
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
2266
|
+
* ```
|
|
2267
|
+
*
|
|
2268
|
+
* @category Sequencing
|
|
1305
2269
|
* @since 2.0.0
|
|
1306
2270
|
*/
|
|
1307
|
-
<A,
|
|
1308
|
-
} = dual(2, <A,
|
|
2271
|
+
<A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>): (a: A) => Option<C>
|
|
2272
|
+
} = dual(2, <A, B, C>(afb: (a: A) => Option<B>, bfc: (b: B) => Option<C>) => (a: A): Option<C> => flatMap(afb(a), bfc))
|
|
1309
2273
|
|
|
1310
2274
|
/**
|
|
1311
|
-
* Applies the provided function `f` to the value of the `Option` if it is
|
|
1312
|
-
* unless `f` returns `None`, in which
|
|
2275
|
+
* Applies the provided function `f` to the value of the `Option` if it is
|
|
2276
|
+
* `Some` and returns the original `Option`, unless `f` returns `None`, in which
|
|
2277
|
+
* case it returns `None`.
|
|
1313
2278
|
*
|
|
1314
|
-
*
|
|
2279
|
+
* **Details**
|
|
1315
2280
|
*
|
|
1316
|
-
*
|
|
1317
|
-
*
|
|
2281
|
+
* This function allows you to perform additional computations on the value of
|
|
2282
|
+
* an `Option` without modifying its original value. If the `Option` is `Some`,
|
|
2283
|
+
* the provided function `f` is executed with the value, and its result
|
|
2284
|
+
* determines whether the original `Option` is returned (`Some`) or the result
|
|
2285
|
+
* is `None` if `f` returns `None`. If the input `Option` is `None`, the
|
|
2286
|
+
* function is not executed, and `None` is returned.
|
|
2287
|
+
*
|
|
2288
|
+
* This is particularly useful for applying side conditions or performing
|
|
2289
|
+
* validation checks while retaining the original `Option`'s value.
|
|
1318
2290
|
*
|
|
1319
2291
|
* @example
|
|
1320
2292
|
* ```ts
|
|
@@ -1322,23 +2294,36 @@ export const zipLeft: {
|
|
|
1322
2294
|
*
|
|
1323
2295
|
* const getInteger = (n: number) => Number.isInteger(n) ? Option.some(n) : Option.none()
|
|
1324
2296
|
*
|
|
1325
|
-
*
|
|
1326
|
-
*
|
|
1327
|
-
*
|
|
2297
|
+
* console.log(Option.tap(Option.none(), getInteger))
|
|
2298
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
2299
|
+
*
|
|
2300
|
+
* console.log(Option.tap(Option.some(1), getInteger))
|
|
2301
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
2302
|
+
*
|
|
2303
|
+
* console.log(Option.tap(Option.some(1.14), getInteger))
|
|
2304
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1328
2305
|
* ```
|
|
1329
2306
|
*
|
|
1330
|
-
* @category
|
|
2307
|
+
* @category Sequencing
|
|
1331
2308
|
* @since 2.0.0
|
|
1332
2309
|
*/
|
|
1333
2310
|
export const tap: {
|
|
1334
2311
|
/**
|
|
1335
|
-
* Applies the provided function `f` to the value of the `Option` if it is
|
|
1336
|
-
* unless `f` returns `None`, in which
|
|
2312
|
+
* Applies the provided function `f` to the value of the `Option` if it is
|
|
2313
|
+
* `Some` and returns the original `Option`, unless `f` returns `None`, in which
|
|
2314
|
+
* case it returns `None`.
|
|
2315
|
+
*
|
|
2316
|
+
* **Details**
|
|
1337
2317
|
*
|
|
1338
|
-
* This function
|
|
2318
|
+
* This function allows you to perform additional computations on the value of
|
|
2319
|
+
* an `Option` without modifying its original value. If the `Option` is `Some`,
|
|
2320
|
+
* the provided function `f` is executed with the value, and its result
|
|
2321
|
+
* determines whether the original `Option` is returned (`Some`) or the result
|
|
2322
|
+
* is `None` if `f` returns `None`. If the input `Option` is `None`, the
|
|
2323
|
+
* function is not executed, and `None` is returned.
|
|
1339
2324
|
*
|
|
1340
|
-
*
|
|
1341
|
-
*
|
|
2325
|
+
* This is particularly useful for applying side conditions or performing
|
|
2326
|
+
* validation checks while retaining the original `Option`'s value.
|
|
1342
2327
|
*
|
|
1343
2328
|
* @example
|
|
1344
2329
|
* ```ts
|
|
@@ -1346,23 +2331,36 @@ export const tap: {
|
|
|
1346
2331
|
*
|
|
1347
2332
|
* const getInteger = (n: number) => Number.isInteger(n) ? Option.some(n) : Option.none()
|
|
1348
2333
|
*
|
|
1349
|
-
*
|
|
1350
|
-
*
|
|
1351
|
-
*
|
|
2334
|
+
* console.log(Option.tap(Option.none(), getInteger))
|
|
2335
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
2336
|
+
*
|
|
2337
|
+
* console.log(Option.tap(Option.some(1), getInteger))
|
|
2338
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
2339
|
+
*
|
|
2340
|
+
* console.log(Option.tap(Option.some(1.14), getInteger))
|
|
2341
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1352
2342
|
* ```
|
|
1353
2343
|
*
|
|
1354
|
-
* @category
|
|
2344
|
+
* @category Sequencing
|
|
1355
2345
|
* @since 2.0.0
|
|
1356
2346
|
*/
|
|
1357
2347
|
<A, X>(f: (a: A) => Option<X>): (self: Option<A>) => Option<A>
|
|
1358
2348
|
/**
|
|
1359
|
-
* Applies the provided function `f` to the value of the `Option` if it is
|
|
1360
|
-
* unless `f` returns `None`, in which
|
|
2349
|
+
* Applies the provided function `f` to the value of the `Option` if it is
|
|
2350
|
+
* `Some` and returns the original `Option`, unless `f` returns `None`, in which
|
|
2351
|
+
* case it returns `None`.
|
|
1361
2352
|
*
|
|
1362
|
-
*
|
|
2353
|
+
* **Details**
|
|
1363
2354
|
*
|
|
1364
|
-
*
|
|
1365
|
-
*
|
|
2355
|
+
* This function allows you to perform additional computations on the value of
|
|
2356
|
+
* an `Option` without modifying its original value. If the `Option` is `Some`,
|
|
2357
|
+
* the provided function `f` is executed with the value, and its result
|
|
2358
|
+
* determines whether the original `Option` is returned (`Some`) or the result
|
|
2359
|
+
* is `None` if `f` returns `None`. If the input `Option` is `None`, the
|
|
2360
|
+
* function is not executed, and `None` is returned.
|
|
2361
|
+
*
|
|
2362
|
+
* This is particularly useful for applying side conditions or performing
|
|
2363
|
+
* validation checks while retaining the original `Option`'s value.
|
|
1366
2364
|
*
|
|
1367
2365
|
* @example
|
|
1368
2366
|
* ```ts
|
|
@@ -1370,26 +2368,51 @@ export const tap: {
|
|
|
1370
2368
|
*
|
|
1371
2369
|
* const getInteger = (n: number) => Number.isInteger(n) ? Option.some(n) : Option.none()
|
|
1372
2370
|
*
|
|
1373
|
-
*
|
|
1374
|
-
*
|
|
1375
|
-
*
|
|
2371
|
+
* console.log(Option.tap(Option.none(), getInteger))
|
|
2372
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
2373
|
+
*
|
|
2374
|
+
* console.log(Option.tap(Option.some(1), getInteger))
|
|
2375
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
2376
|
+
*
|
|
2377
|
+
* console.log(Option.tap(Option.some(1.14), getInteger))
|
|
2378
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1376
2379
|
* ```
|
|
1377
2380
|
*
|
|
1378
|
-
* @category
|
|
2381
|
+
* @category Sequencing
|
|
1379
2382
|
* @since 2.0.0
|
|
1380
2383
|
*/
|
|
1381
2384
|
<A, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A>
|
|
1382
2385
|
} = dual(2, <A, X>(self: Option<A>, f: (a: A) => Option<X>): Option<A> => flatMap(self, (a) => map(f(a), () => a)))
|
|
1383
2386
|
|
|
1384
2387
|
/**
|
|
1385
|
-
*
|
|
2388
|
+
* Combines two `Option` values into a single `Option` containing a tuple of
|
|
2389
|
+
* their values if both are `Some`.
|
|
2390
|
+
*
|
|
2391
|
+
* **Details**
|
|
2392
|
+
*
|
|
2393
|
+
* This function takes two `Option`s and combines their values into a tuple `[A,
|
|
2394
|
+
* B]` if both are `Some`. If either of the `Option`s is `None`, the result is
|
|
2395
|
+
* `None`. This is particularly useful for combining multiple `Option` values
|
|
2396
|
+
* into a single one, ensuring both contain valid values.
|
|
2397
|
+
*
|
|
2398
|
+
* @category Combining
|
|
1386
2399
|
* @since 2.0.0
|
|
1387
2400
|
*/
|
|
1388
2401
|
export const product = <A, B>(self: Option<A>, that: Option<B>): Option<[A, B]> =>
|
|
1389
2402
|
isSome(self) && isSome(that) ? some([self.value, that.value]) : none()
|
|
1390
2403
|
|
|
1391
2404
|
/**
|
|
1392
|
-
*
|
|
2405
|
+
* Combines an `Option` with a collection of `Option`s into a single `Option`
|
|
2406
|
+
* containing a tuple of their values if all are `Some`.
|
|
2407
|
+
*
|
|
2408
|
+
* **Details**
|
|
2409
|
+
*
|
|
2410
|
+
* This function takes a primary `Option` and a collection of `Option`s and
|
|
2411
|
+
* combines their values into a tuple `[A, ...Array<A>]` if all are `Some`. If
|
|
2412
|
+
* the primary `Option` or any `Option` in the collection is `None`, the result
|
|
2413
|
+
* is `None`.
|
|
2414
|
+
*
|
|
2415
|
+
* @category Combining
|
|
1393
2416
|
* @since 2.0.0
|
|
1394
2417
|
*/
|
|
1395
2418
|
export const productMany = <A>(
|
|
@@ -1410,24 +2433,47 @@ export const productMany = <A>(
|
|
|
1410
2433
|
}
|
|
1411
2434
|
|
|
1412
2435
|
/**
|
|
1413
|
-
*
|
|
2436
|
+
* Combines a structure of `Option`s into a single `Option` containing the
|
|
2437
|
+
* values with the same structure.
|
|
2438
|
+
*
|
|
2439
|
+
* **Details**
|
|
1414
2440
|
*
|
|
1415
|
-
*
|
|
1416
|
-
*
|
|
1417
|
-
*
|
|
2441
|
+
* This function takes a structure of `Option`s (a tuple, struct, or iterable)
|
|
2442
|
+
* and produces a single `Option` that contains the values from the input
|
|
2443
|
+
* structure if all `Option`s are `Some`. If any `Option` in the input is
|
|
2444
|
+
* `None`, the result is `None`. The structure of the input is preserved in the
|
|
2445
|
+
* output.
|
|
1418
2446
|
*
|
|
1419
|
-
*
|
|
2447
|
+
* - If the input is a tuple (e.g., an array), the result will be an `Option`
|
|
2448
|
+
* containing a tuple with the same length.
|
|
2449
|
+
* - If the input is a struct (e.g., an object), the result will be an `Option`
|
|
2450
|
+
* containing a struct with the same keys.
|
|
2451
|
+
* - If the input is an iterable, the result will be an `Option` containing an
|
|
2452
|
+
* array.
|
|
1420
2453
|
*
|
|
1421
2454
|
* @example
|
|
1422
2455
|
* ```ts
|
|
1423
2456
|
* import { Option } from "effect"
|
|
1424
2457
|
*
|
|
1425
|
-
*
|
|
1426
|
-
*
|
|
1427
|
-
*
|
|
2458
|
+
* const maybeName: Option.Option<string> = Option.some("John")
|
|
2459
|
+
* const maybeAge: Option.Option<number> = Option.some(25)
|
|
2460
|
+
*
|
|
2461
|
+
* // ┌─── Option<[string, number]>
|
|
2462
|
+
* // ▼
|
|
2463
|
+
* const tuple = Option.all([maybeName, maybeAge])
|
|
2464
|
+
* console.log(tuple)
|
|
2465
|
+
* // Output:
|
|
2466
|
+
* // { _id: 'Option', _tag: 'Some', value: [ 'John', 25 ] }
|
|
2467
|
+
*
|
|
2468
|
+
* // ┌─── Option<{ name: string; age: number; }>
|
|
2469
|
+
* // ▼
|
|
2470
|
+
* const struct = Option.all({ name: maybeName, age: maybeAge })
|
|
2471
|
+
* console.log(struct)
|
|
2472
|
+
* // Output:
|
|
2473
|
+
* // { _id: 'Option', _tag: 'Some', value: { name: 'John', age: 25 } }
|
|
1428
2474
|
* ```
|
|
1429
2475
|
*
|
|
1430
|
-
* @category
|
|
2476
|
+
* @category Combining
|
|
1431
2477
|
* @since 2.0.0
|
|
1432
2478
|
*/
|
|
1433
2479
|
// @ts-expect-error
|
|
@@ -1463,83 +2509,110 @@ export const all: <const I extends Iterable<Option<any>> | Record<string, Option
|
|
|
1463
2509
|
}
|
|
1464
2510
|
|
|
1465
2511
|
/**
|
|
1466
|
-
*
|
|
2512
|
+
* Combines two `Option` values into a new `Option` by applying a provided
|
|
2513
|
+
* function to their values.
|
|
1467
2514
|
*
|
|
1468
|
-
*
|
|
1469
|
-
*
|
|
1470
|
-
*
|
|
2515
|
+
* **Details**
|
|
2516
|
+
*
|
|
2517
|
+
* This function takes two `Option` values (`self` and `that`) and a combining
|
|
2518
|
+
* function `f`. If both `Option` values are `Some`, the function `f` is applied
|
|
2519
|
+
* to their values, and the result is wrapped in a new `Some`. If either
|
|
2520
|
+
* `Option` is `None`, the result is `None`.
|
|
2521
|
+
*
|
|
2522
|
+
* This utility is useful for combining two optional computations into a single
|
|
2523
|
+
* result while maintaining type safety and avoiding explicit checks for `None`.
|
|
1471
2524
|
*
|
|
1472
2525
|
* @example
|
|
1473
2526
|
* ```ts
|
|
1474
2527
|
* import { Option } from "effect"
|
|
1475
2528
|
*
|
|
1476
|
-
*
|
|
2529
|
+
* const maybeName: Option.Option<string> = Option.some("John")
|
|
2530
|
+
* const maybeAge: Option.Option<number> = Option.some(25)
|
|
1477
2531
|
*
|
|
1478
|
-
*
|
|
2532
|
+
* // Combine the name and age into a person object
|
|
2533
|
+
* const person = Option.zipWith(maybeName, maybeAge, (name, age) => ({
|
|
2534
|
+
* name: name.toUpperCase(),
|
|
2535
|
+
* age
|
|
2536
|
+
* }))
|
|
1479
2537
|
*
|
|
1480
|
-
*
|
|
1481
|
-
*
|
|
1482
|
-
*
|
|
1483
|
-
* assert.deepStrictEqual(Option.zipWith(Option.some(1), Option.some(2), complex), Option.some([1, 2]))
|
|
1484
|
-
*
|
|
1485
|
-
* assert.deepStrictEqual(Option.zipWith(Option.some(1), complex)(Option.some(2)), Option.some([2, 1]))
|
|
2538
|
+
* console.log(person)
|
|
2539
|
+
* // Output:
|
|
2540
|
+
* // { _id: 'Option', _tag: 'Some', value: { name: 'JOHN', age: 25 } }
|
|
1486
2541
|
* ```
|
|
1487
2542
|
*
|
|
1488
|
-
* @category
|
|
2543
|
+
* @category Zipping
|
|
1489
2544
|
* @since 2.0.0
|
|
1490
2545
|
*/
|
|
1491
2546
|
export const zipWith: {
|
|
1492
2547
|
/**
|
|
1493
|
-
*
|
|
2548
|
+
* Combines two `Option` values into a new `Option` by applying a provided
|
|
2549
|
+
* function to their values.
|
|
2550
|
+
*
|
|
2551
|
+
* **Details**
|
|
1494
2552
|
*
|
|
1495
|
-
*
|
|
1496
|
-
*
|
|
1497
|
-
*
|
|
2553
|
+
* This function takes two `Option` values (`self` and `that`) and a combining
|
|
2554
|
+
* function `f`. If both `Option` values are `Some`, the function `f` is applied
|
|
2555
|
+
* to their values, and the result is wrapped in a new `Some`. If either
|
|
2556
|
+
* `Option` is `None`, the result is `None`.
|
|
2557
|
+
*
|
|
2558
|
+
* This utility is useful for combining two optional computations into a single
|
|
2559
|
+
* result while maintaining type safety and avoiding explicit checks for `None`.
|
|
1498
2560
|
*
|
|
1499
2561
|
* @example
|
|
1500
2562
|
* ```ts
|
|
1501
2563
|
* import { Option } from "effect"
|
|
1502
2564
|
*
|
|
1503
|
-
*
|
|
1504
|
-
*
|
|
1505
|
-
* const complex = (real: number, imaginary: number): Complex => [real, imaginary]
|
|
2565
|
+
* const maybeName: Option.Option<string> = Option.some("John")
|
|
2566
|
+
* const maybeAge: Option.Option<number> = Option.some(25)
|
|
1506
2567
|
*
|
|
1507
|
-
*
|
|
1508
|
-
*
|
|
1509
|
-
*
|
|
1510
|
-
*
|
|
2568
|
+
* // Combine the name and age into a person object
|
|
2569
|
+
* const person = Option.zipWith(maybeName, maybeAge, (name, age) => ({
|
|
2570
|
+
* name: name.toUpperCase(),
|
|
2571
|
+
* age
|
|
2572
|
+
* }))
|
|
1511
2573
|
*
|
|
1512
|
-
*
|
|
2574
|
+
* console.log(person)
|
|
2575
|
+
* // Output:
|
|
2576
|
+
* // { _id: 'Option', _tag: 'Some', value: { name: 'JOHN', age: 25 } }
|
|
1513
2577
|
* ```
|
|
1514
2578
|
*
|
|
1515
|
-
* @category
|
|
2579
|
+
* @category Zipping
|
|
1516
2580
|
* @since 2.0.0
|
|
1517
2581
|
*/
|
|
1518
2582
|
<B, A, C>(that: Option<B>, f: (a: A, b: B) => C): (self: Option<A>) => Option<C>
|
|
1519
2583
|
/**
|
|
1520
|
-
*
|
|
2584
|
+
* Combines two `Option` values into a new `Option` by applying a provided
|
|
2585
|
+
* function to their values.
|
|
2586
|
+
*
|
|
2587
|
+
* **Details**
|
|
2588
|
+
*
|
|
2589
|
+
* This function takes two `Option` values (`self` and `that`) and a combining
|
|
2590
|
+
* function `f`. If both `Option` values are `Some`, the function `f` is applied
|
|
2591
|
+
* to their values, and the result is wrapped in a new `Some`. If either
|
|
2592
|
+
* `Option` is `None`, the result is `None`.
|
|
1521
2593
|
*
|
|
1522
|
-
*
|
|
1523
|
-
*
|
|
1524
|
-
* @param f - The function used to combine the values of the two `Option`s
|
|
2594
|
+
* This utility is useful for combining two optional computations into a single
|
|
2595
|
+
* result while maintaining type safety and avoiding explicit checks for `None`.
|
|
1525
2596
|
*
|
|
1526
2597
|
* @example
|
|
1527
2598
|
* ```ts
|
|
1528
2599
|
* import { Option } from "effect"
|
|
1529
2600
|
*
|
|
1530
|
-
*
|
|
2601
|
+
* const maybeName: Option.Option<string> = Option.some("John")
|
|
2602
|
+
* const maybeAge: Option.Option<number> = Option.some(25)
|
|
1531
2603
|
*
|
|
1532
|
-
*
|
|
2604
|
+
* // Combine the name and age into a person object
|
|
2605
|
+
* const person = Option.zipWith(maybeName, maybeAge, (name, age) => ({
|
|
2606
|
+
* name: name.toUpperCase(),
|
|
2607
|
+
* age
|
|
2608
|
+
* }))
|
|
1533
2609
|
*
|
|
1534
|
-
*
|
|
1535
|
-
*
|
|
1536
|
-
*
|
|
1537
|
-
* assert.deepStrictEqual(Option.zipWith(Option.some(1), Option.some(2), complex), Option.some([1, 2]))
|
|
1538
|
-
*
|
|
1539
|
-
* assert.deepStrictEqual(Option.zipWith(Option.some(1), complex)(Option.some(2)), Option.some([2, 1]))
|
|
2610
|
+
* console.log(person)
|
|
2611
|
+
* // Output:
|
|
2612
|
+
* // { _id: 'Option', _tag: 'Some', value: { name: 'JOHN', age: 25 } }
|
|
1540
2613
|
* ```
|
|
1541
2614
|
*
|
|
1542
|
-
* @category
|
|
2615
|
+
* @category Zipping
|
|
1543
2616
|
* @since 2.0.0
|
|
1544
2617
|
*/
|
|
1545
2618
|
<A, B, C>(self: Option<A>, that: Option<B>, f: (a: A, b: B) => C): Option<C>
|
|
@@ -1550,76 +2623,133 @@ export const zipWith: {
|
|
|
1550
2623
|
)
|
|
1551
2624
|
|
|
1552
2625
|
/**
|
|
1553
|
-
*
|
|
2626
|
+
* Applies a function inside a `Some` to a value inside another `Some`,
|
|
2627
|
+
* combining them into a new `Option`.
|
|
2628
|
+
*
|
|
2629
|
+
* **Details**
|
|
2630
|
+
*
|
|
2631
|
+
* This function allows you to apply a function wrapped in an `Option` (`self`)
|
|
2632
|
+
* to a value wrapped in another `Option` (`that`). If both `Option`s are
|
|
2633
|
+
* `Some`, the function is applied to the value, and the result is wrapped in a
|
|
2634
|
+
* new `Some`. If either `Option` is `None`, the result is `None`.
|
|
2635
|
+
*
|
|
2636
|
+
* @category Combining
|
|
1554
2637
|
* @since 2.0.0
|
|
1555
2638
|
*/
|
|
1556
2639
|
export const ap: {
|
|
1557
2640
|
/**
|
|
1558
|
-
*
|
|
2641
|
+
* Applies a function inside a `Some` to a value inside another `Some`,
|
|
2642
|
+
* combining them into a new `Option`.
|
|
2643
|
+
*
|
|
2644
|
+
* **Details**
|
|
2645
|
+
*
|
|
2646
|
+
* This function allows you to apply a function wrapped in an `Option` (`self`)
|
|
2647
|
+
* to a value wrapped in another `Option` (`that`). If both `Option`s are
|
|
2648
|
+
* `Some`, the function is applied to the value, and the result is wrapped in a
|
|
2649
|
+
* new `Some`. If either `Option` is `None`, the result is `None`.
|
|
2650
|
+
*
|
|
2651
|
+
* @category Combining
|
|
1559
2652
|
* @since 2.0.0
|
|
1560
2653
|
*/
|
|
1561
2654
|
<A>(that: Option<A>): <B>(self: Option<(a: A) => B>) => Option<B>
|
|
1562
2655
|
/**
|
|
1563
|
-
*
|
|
2656
|
+
* Applies a function inside a `Some` to a value inside another `Some`,
|
|
2657
|
+
* combining them into a new `Option`.
|
|
2658
|
+
*
|
|
2659
|
+
* **Details**
|
|
2660
|
+
*
|
|
2661
|
+
* This function allows you to apply a function wrapped in an `Option` (`self`)
|
|
2662
|
+
* to a value wrapped in another `Option` (`that`). If both `Option`s are
|
|
2663
|
+
* `Some`, the function is applied to the value, and the result is wrapped in a
|
|
2664
|
+
* new `Some`. If either `Option` is `None`, the result is `None`.
|
|
2665
|
+
*
|
|
2666
|
+
* @category Combining
|
|
1564
2667
|
* @since 2.0.0
|
|
1565
2668
|
*/
|
|
1566
2669
|
<A, B>(self: Option<(a: A) => B>, that: Option<A>): Option<B>
|
|
1567
2670
|
} = dual(2, <A, B>(self: Option<(a: A) => B>, that: Option<A>): Option<B> => zipWith(self, that, (f, a) => f(a)))
|
|
1568
2671
|
|
|
1569
2672
|
/**
|
|
1570
|
-
* Reduces an `Iterable` of `Option<A>` to a single value of type `B`,
|
|
2673
|
+
* Reduces an `Iterable` of `Option<A>` to a single value of type `B`, ignoring
|
|
2674
|
+
* elements that are `None`.
|
|
2675
|
+
*
|
|
2676
|
+
* **Details**
|
|
2677
|
+
*
|
|
2678
|
+
* This function takes an initial value of type `B` and a reducing function `f`
|
|
2679
|
+
* that combines the accumulator with values of type `A`. It processes an
|
|
2680
|
+
* iterable of `Option<A>`, applying `f` only to the `Some` values while
|
|
2681
|
+
* ignoring the `None` values. The result is a single value of type `B`.
|
|
1571
2682
|
*
|
|
1572
|
-
*
|
|
1573
|
-
*
|
|
1574
|
-
* @param f - The reducing function that takes the current accumulator value and the unwrapped value of an `Option<A>`.
|
|
2683
|
+
* This utility is particularly useful for aggregating values from an iterable
|
|
2684
|
+
* of `Option`s while skipping the absent (`None`) values.
|
|
1575
2685
|
*
|
|
1576
2686
|
* @example
|
|
1577
2687
|
* ```ts
|
|
1578
|
-
* import {
|
|
2688
|
+
* import { Option, pipe } from "effect"
|
|
1579
2689
|
*
|
|
1580
2690
|
* const iterable = [Option.some(1), Option.none(), Option.some(2), Option.none()]
|
|
1581
|
-
*
|
|
2691
|
+
*
|
|
2692
|
+
* console.log(pipe(iterable, Option.reduceCompact(0, (b, a) => b + a)))
|
|
2693
|
+
* // Output: 3
|
|
1582
2694
|
* ```
|
|
1583
2695
|
*
|
|
1584
|
-
* @category
|
|
2696
|
+
* @category Reducing
|
|
1585
2697
|
* @since 2.0.0
|
|
1586
2698
|
*/
|
|
1587
2699
|
export const reduceCompact: {
|
|
1588
2700
|
/**
|
|
1589
|
-
* Reduces an `Iterable` of `Option<A>` to a single value of type `B`,
|
|
2701
|
+
* Reduces an `Iterable` of `Option<A>` to a single value of type `B`, ignoring
|
|
2702
|
+
* elements that are `None`.
|
|
2703
|
+
*
|
|
2704
|
+
* **Details**
|
|
2705
|
+
*
|
|
2706
|
+
* This function takes an initial value of type `B` and a reducing function `f`
|
|
2707
|
+
* that combines the accumulator with values of type `A`. It processes an
|
|
2708
|
+
* iterable of `Option<A>`, applying `f` only to the `Some` values while
|
|
2709
|
+
* ignoring the `None` values. The result is a single value of type `B`.
|
|
1590
2710
|
*
|
|
1591
|
-
*
|
|
1592
|
-
*
|
|
1593
|
-
* @param f - The reducing function that takes the current accumulator value and the unwrapped value of an `Option<A>`.
|
|
2711
|
+
* This utility is particularly useful for aggregating values from an iterable
|
|
2712
|
+
* of `Option`s while skipping the absent (`None`) values.
|
|
1594
2713
|
*
|
|
1595
2714
|
* @example
|
|
1596
2715
|
* ```ts
|
|
1597
|
-
* import {
|
|
2716
|
+
* import { Option, pipe } from "effect"
|
|
1598
2717
|
*
|
|
1599
2718
|
* const iterable = [Option.some(1), Option.none(), Option.some(2), Option.none()]
|
|
1600
|
-
*
|
|
2719
|
+
*
|
|
2720
|
+
* console.log(pipe(iterable, Option.reduceCompact(0, (b, a) => b + a)))
|
|
2721
|
+
* // Output: 3
|
|
1601
2722
|
* ```
|
|
1602
2723
|
*
|
|
1603
|
-
* @category
|
|
2724
|
+
* @category Reducing
|
|
1604
2725
|
* @since 2.0.0
|
|
1605
2726
|
*/
|
|
1606
2727
|
<B, A>(b: B, f: (b: B, a: A) => B): (self: Iterable<Option<A>>) => B
|
|
1607
2728
|
/**
|
|
1608
|
-
* Reduces an `Iterable` of `Option<A>` to a single value of type `B`,
|
|
2729
|
+
* Reduces an `Iterable` of `Option<A>` to a single value of type `B`, ignoring
|
|
2730
|
+
* elements that are `None`.
|
|
2731
|
+
*
|
|
2732
|
+
* **Details**
|
|
2733
|
+
*
|
|
2734
|
+
* This function takes an initial value of type `B` and a reducing function `f`
|
|
2735
|
+
* that combines the accumulator with values of type `A`. It processes an
|
|
2736
|
+
* iterable of `Option<A>`, applying `f` only to the `Some` values while
|
|
2737
|
+
* ignoring the `None` values. The result is a single value of type `B`.
|
|
1609
2738
|
*
|
|
1610
|
-
*
|
|
1611
|
-
*
|
|
1612
|
-
* @param f - The reducing function that takes the current accumulator value and the unwrapped value of an `Option<A>`.
|
|
2739
|
+
* This utility is particularly useful for aggregating values from an iterable
|
|
2740
|
+
* of `Option`s while skipping the absent (`None`) values.
|
|
1613
2741
|
*
|
|
1614
2742
|
* @example
|
|
1615
2743
|
* ```ts
|
|
1616
|
-
* import {
|
|
2744
|
+
* import { Option, pipe } from "effect"
|
|
1617
2745
|
*
|
|
1618
2746
|
* const iterable = [Option.some(1), Option.none(), Option.some(2), Option.none()]
|
|
1619
|
-
*
|
|
2747
|
+
*
|
|
2748
|
+
* console.log(pipe(iterable, Option.reduceCompact(0, (b, a) => b + a)))
|
|
2749
|
+
* // Output: 3
|
|
1620
2750
|
* ```
|
|
1621
2751
|
*
|
|
1622
|
-
* @category
|
|
2752
|
+
* @category Reducing
|
|
1623
2753
|
* @since 2.0.0
|
|
1624
2754
|
*/
|
|
1625
2755
|
<A, B>(self: Iterable<Option<A>>, b: B, f: (b: B, a: A) => B): B
|
|
@@ -1637,37 +2767,92 @@ export const reduceCompact: {
|
|
|
1637
2767
|
)
|
|
1638
2768
|
|
|
1639
2769
|
/**
|
|
1640
|
-
*
|
|
2770
|
+
* Converts an `Option` into an `Array`.
|
|
1641
2771
|
* If the input is `None`, an empty array is returned.
|
|
1642
|
-
* If the input is `Some`,
|
|
1643
|
-
*
|
|
1644
|
-
* @param self - The `Option` to convert to an array.
|
|
2772
|
+
* If the input is `Some`, its value is wrapped in a single-element array.
|
|
1645
2773
|
*
|
|
1646
2774
|
* @example
|
|
1647
2775
|
* ```ts
|
|
1648
2776
|
* import { Option } from "effect"
|
|
1649
2777
|
*
|
|
1650
|
-
*
|
|
1651
|
-
*
|
|
2778
|
+
* console.log(Option.toArray(Option.some(1)))
|
|
2779
|
+
* // Output: [1]
|
|
2780
|
+
*
|
|
2781
|
+
* console.log(Option.toArray(Option.none()))
|
|
2782
|
+
* // Output: []
|
|
1652
2783
|
* ```
|
|
1653
2784
|
*
|
|
1654
|
-
* @category
|
|
2785
|
+
* @category Conversions
|
|
1655
2786
|
* @since 2.0.0
|
|
1656
2787
|
*/
|
|
1657
2788
|
export const toArray = <A>(self: Option<A>): Array<A> => isNone(self) ? [] : [self.value]
|
|
1658
2789
|
|
|
1659
2790
|
/**
|
|
1660
|
-
*
|
|
2791
|
+
* Splits an `Option` into two `Option`s based on the result of a mapping
|
|
2792
|
+
* function that produces an `Either`.
|
|
2793
|
+
*
|
|
2794
|
+
* **Details**
|
|
2795
|
+
*
|
|
2796
|
+
* This function takes an `Option` and a mapping function `f` that converts its
|
|
2797
|
+
* value into an `Either`. It returns a tuple of two `Option`s:
|
|
2798
|
+
*
|
|
2799
|
+
* - The first `Option` (`left`) contains the value from the `Left` side of the
|
|
2800
|
+
* `Either` if it exists, otherwise `None`.
|
|
2801
|
+
* - The second `Option` (`right`) contains the value from the `Right` side of
|
|
2802
|
+
* the `Either` if it exists, otherwise `None`.
|
|
2803
|
+
*
|
|
2804
|
+
* If the input `Option` is `None`, both returned `Option`s are `None`.
|
|
2805
|
+
*
|
|
2806
|
+
* This utility is useful for filtering and categorizing the contents of an
|
|
2807
|
+
* `Option` based on a bifurcating computation.
|
|
2808
|
+
*
|
|
2809
|
+
* @category Filtering
|
|
1661
2810
|
* @since 2.0.0
|
|
1662
2811
|
*/
|
|
1663
2812
|
export const partitionMap: {
|
|
1664
2813
|
/**
|
|
1665
|
-
*
|
|
2814
|
+
* Splits an `Option` into two `Option`s based on the result of a mapping
|
|
2815
|
+
* function that produces an `Either`.
|
|
2816
|
+
*
|
|
2817
|
+
* **Details**
|
|
2818
|
+
*
|
|
2819
|
+
* This function takes an `Option` and a mapping function `f` that converts its
|
|
2820
|
+
* value into an `Either`. It returns a tuple of two `Option`s:
|
|
2821
|
+
*
|
|
2822
|
+
* - The first `Option` (`left`) contains the value from the `Left` side of the
|
|
2823
|
+
* `Either` if it exists, otherwise `None`.
|
|
2824
|
+
* - The second `Option` (`right`) contains the value from the `Right` side of
|
|
2825
|
+
* the `Either` if it exists, otherwise `None`.
|
|
2826
|
+
*
|
|
2827
|
+
* If the input `Option` is `None`, both returned `Option`s are `None`.
|
|
2828
|
+
*
|
|
2829
|
+
* This utility is useful for filtering and categorizing the contents of an
|
|
2830
|
+
* `Option` based on a bifurcating computation.
|
|
2831
|
+
*
|
|
2832
|
+
* @category Filtering
|
|
1666
2833
|
* @since 2.0.0
|
|
1667
2834
|
*/
|
|
1668
2835
|
<A, B, C>(f: (a: A) => Either<C, B>): (self: Option<A>) => [left: Option<B>, right: Option<C>]
|
|
1669
2836
|
/**
|
|
1670
|
-
*
|
|
2837
|
+
* Splits an `Option` into two `Option`s based on the result of a mapping
|
|
2838
|
+
* function that produces an `Either`.
|
|
2839
|
+
*
|
|
2840
|
+
* **Details**
|
|
2841
|
+
*
|
|
2842
|
+
* This function takes an `Option` and a mapping function `f` that converts its
|
|
2843
|
+
* value into an `Either`. It returns a tuple of two `Option`s:
|
|
2844
|
+
*
|
|
2845
|
+
* - The first `Option` (`left`) contains the value from the `Left` side of the
|
|
2846
|
+
* `Either` if it exists, otherwise `None`.
|
|
2847
|
+
* - The second `Option` (`right`) contains the value from the `Right` side of
|
|
2848
|
+
* the `Either` if it exists, otherwise `None`.
|
|
2849
|
+
*
|
|
2850
|
+
* If the input `Option` is `None`, both returned `Option`s are `None`.
|
|
2851
|
+
*
|
|
2852
|
+
* This utility is useful for filtering and categorizing the contents of an
|
|
2853
|
+
* `Option` based on a bifurcating computation.
|
|
2854
|
+
*
|
|
2855
|
+
* @category Filtering
|
|
1671
2856
|
* @since 2.0.0
|
|
1672
2857
|
*/
|
|
1673
2858
|
<A, B, C>(self: Option<A>, f: (a: A) => Either<C, B>): [left: Option<B>, right: Option<C>]
|
|
@@ -1683,71 +2868,116 @@ export const partitionMap: {
|
|
|
1683
2868
|
})
|
|
1684
2869
|
|
|
1685
2870
|
/**
|
|
1686
|
-
* Maps over the value of an `Option`
|
|
2871
|
+
* Maps over the value of an `Option` with a function that may return `None`,
|
|
2872
|
+
* effectively filtering and transforming the value.
|
|
1687
2873
|
*
|
|
1688
|
-
*
|
|
2874
|
+
* **Details**
|
|
1689
2875
|
*
|
|
1690
|
-
*
|
|
1691
|
-
*
|
|
2876
|
+
* This function allows you to both transform the value of a `Some` and filter
|
|
2877
|
+
* it at the same time. The mapping function `f` can either return a new
|
|
2878
|
+
* `Option` (to transform the value) or return `None` to filter it out. If the
|
|
2879
|
+
* input `Option` is `None`, the function is not applied, and the result remains
|
|
2880
|
+
* `None`.
|
|
2881
|
+
*
|
|
2882
|
+
* This utility is particularly useful when you want to apply a transformation
|
|
2883
|
+
* to the value of an `Option` while conditionally removing invalid or unwanted
|
|
2884
|
+
* results.
|
|
1692
2885
|
*
|
|
1693
2886
|
* @example
|
|
1694
2887
|
* ```ts
|
|
1695
2888
|
* import { Option } from "effect"
|
|
1696
2889
|
*
|
|
1697
|
-
*
|
|
2890
|
+
* // Transform and filter numbers
|
|
2891
|
+
* const transformEven = (n: Option.Option<number>): Option.Option<string> =>
|
|
2892
|
+
* Option.flatMap(n, (n) => (n % 2 === 0 ? Option.some(`Even: ${n}`) : Option.none()))
|
|
2893
|
+
*
|
|
2894
|
+
* console.log(transformEven(Option.none()))
|
|
2895
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1698
2896
|
*
|
|
1699
|
-
*
|
|
1700
|
-
*
|
|
1701
|
-
*
|
|
2897
|
+
* console.log(transformEven(Option.some(1)))
|
|
2898
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
2899
|
+
*
|
|
2900
|
+
* console.log(transformEven(Option.some(2)))
|
|
2901
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'Even: 2' }
|
|
1702
2902
|
* ```
|
|
1703
2903
|
*
|
|
1704
|
-
* @category
|
|
2904
|
+
* @category Filtering
|
|
1705
2905
|
* @since 2.0.0
|
|
1706
2906
|
*/
|
|
1707
2907
|
export const filterMap: {
|
|
1708
2908
|
/**
|
|
1709
|
-
* Maps over the value of an `Option`
|
|
2909
|
+
* Maps over the value of an `Option` with a function that may return `None`,
|
|
2910
|
+
* effectively filtering and transforming the value.
|
|
2911
|
+
*
|
|
2912
|
+
* **Details**
|
|
1710
2913
|
*
|
|
1711
|
-
*
|
|
2914
|
+
* This function allows you to both transform the value of a `Some` and filter
|
|
2915
|
+
* it at the same time. The mapping function `f` can either return a new
|
|
2916
|
+
* `Option` (to transform the value) or return `None` to filter it out. If the
|
|
2917
|
+
* input `Option` is `None`, the function is not applied, and the result remains
|
|
2918
|
+
* `None`.
|
|
1712
2919
|
*
|
|
1713
|
-
*
|
|
1714
|
-
*
|
|
2920
|
+
* This utility is particularly useful when you want to apply a transformation
|
|
2921
|
+
* to the value of an `Option` while conditionally removing invalid or unwanted
|
|
2922
|
+
* results.
|
|
1715
2923
|
*
|
|
1716
2924
|
* @example
|
|
1717
2925
|
* ```ts
|
|
1718
2926
|
* import { Option } from "effect"
|
|
1719
2927
|
*
|
|
1720
|
-
*
|
|
2928
|
+
* // Transform and filter numbers
|
|
2929
|
+
* const transformEven = (n: Option.Option<number>): Option.Option<string> =>
|
|
2930
|
+
* Option.flatMap(n, (n) => (n % 2 === 0 ? Option.some(`Even: ${n}`) : Option.none()))
|
|
2931
|
+
*
|
|
2932
|
+
* console.log(transformEven(Option.none()))
|
|
2933
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
2934
|
+
*
|
|
2935
|
+
* console.log(transformEven(Option.some(1)))
|
|
2936
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1721
2937
|
*
|
|
1722
|
-
*
|
|
1723
|
-
*
|
|
1724
|
-
* assert.deepStrictEqual(Option.filterMap(Option.some(2), evenNumber), Option.some(2))
|
|
2938
|
+
* console.log(transformEven(Option.some(2)))
|
|
2939
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'Even: 2' }
|
|
1725
2940
|
* ```
|
|
1726
2941
|
*
|
|
1727
|
-
* @category
|
|
2942
|
+
* @category Filtering
|
|
1728
2943
|
* @since 2.0.0
|
|
1729
2944
|
*/
|
|
1730
2945
|
<A, B>(f: (a: A) => Option<B>): (self: Option<A>) => Option<B>
|
|
1731
2946
|
/**
|
|
1732
|
-
* Maps over the value of an `Option`
|
|
2947
|
+
* Maps over the value of an `Option` with a function that may return `None`,
|
|
2948
|
+
* effectively filtering and transforming the value.
|
|
1733
2949
|
*
|
|
1734
|
-
*
|
|
2950
|
+
* **Details**
|
|
1735
2951
|
*
|
|
1736
|
-
*
|
|
1737
|
-
*
|
|
2952
|
+
* This function allows you to both transform the value of a `Some` and filter
|
|
2953
|
+
* it at the same time. The mapping function `f` can either return a new
|
|
2954
|
+
* `Option` (to transform the value) or return `None` to filter it out. If the
|
|
2955
|
+
* input `Option` is `None`, the function is not applied, and the result remains
|
|
2956
|
+
* `None`.
|
|
2957
|
+
*
|
|
2958
|
+
* This utility is particularly useful when you want to apply a transformation
|
|
2959
|
+
* to the value of an `Option` while conditionally removing invalid or unwanted
|
|
2960
|
+
* results.
|
|
1738
2961
|
*
|
|
1739
2962
|
* @example
|
|
1740
2963
|
* ```ts
|
|
1741
2964
|
* import { Option } from "effect"
|
|
1742
2965
|
*
|
|
1743
|
-
*
|
|
2966
|
+
* // Transform and filter numbers
|
|
2967
|
+
* const transformEven = (n: Option.Option<number>): Option.Option<string> =>
|
|
2968
|
+
* Option.flatMap(n, (n) => (n % 2 === 0 ? Option.some(`Even: ${n}`) : Option.none()))
|
|
2969
|
+
*
|
|
2970
|
+
* console.log(transformEven(Option.none()))
|
|
2971
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1744
2972
|
*
|
|
1745
|
-
*
|
|
1746
|
-
*
|
|
1747
|
-
*
|
|
2973
|
+
* console.log(transformEven(Option.some(1)))
|
|
2974
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
2975
|
+
*
|
|
2976
|
+
* console.log(transformEven(Option.some(2)))
|
|
2977
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'Even: 2' }
|
|
1748
2978
|
* ```
|
|
1749
2979
|
*
|
|
1750
|
-
* @category
|
|
2980
|
+
* @category Filtering
|
|
1751
2981
|
* @since 2.0.0
|
|
1752
2982
|
*/
|
|
1753
2983
|
<A, B>(self: Option<A>, f: (a: A) => Option<B>): Option<B>
|
|
@@ -1761,29 +2991,24 @@ export const filterMap: {
|
|
|
1761
2991
|
*
|
|
1762
2992
|
* If you need to change the type of the `Option` in addition to filtering, see `filterMap`.
|
|
1763
2993
|
*
|
|
1764
|
-
* @param predicate - A predicate function to apply to the `Option` value.
|
|
1765
|
-
* @param fb - The `Option` to filter.
|
|
1766
|
-
*
|
|
1767
2994
|
* @example
|
|
1768
2995
|
* ```ts
|
|
1769
2996
|
* import { Option } from "effect"
|
|
1770
2997
|
*
|
|
1771
|
-
*
|
|
1772
|
-
*
|
|
2998
|
+
* const removeEmptyString = (input: Option.Option<string>) =>
|
|
2999
|
+
* Option.filter(input, (value) => value !== "")
|
|
1773
3000
|
*
|
|
1774
|
-
*
|
|
1775
|
-
*
|
|
1776
|
-
* assert.deepStrictEqual(Option.filter(Option.some(2), isEven), Option.some(2))
|
|
3001
|
+
* console.log(removeEmptyString(Option.none()))
|
|
3002
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1777
3003
|
*
|
|
1778
|
-
*
|
|
1779
|
-
*
|
|
3004
|
+
* console.log(removeEmptyString(Option.some("")))
|
|
3005
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1780
3006
|
*
|
|
1781
|
-
*
|
|
1782
|
-
*
|
|
1783
|
-
* assert.deepStrictEqual(Option.filter(Option.some(2), isNumber), Option.some(2))
|
|
3007
|
+
* console.log(removeEmptyString(Option.some("a")))
|
|
3008
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
1784
3009
|
* ```
|
|
1785
3010
|
*
|
|
1786
|
-
* @category
|
|
3011
|
+
* @category Filtering
|
|
1787
3012
|
* @since 2.0.0
|
|
1788
3013
|
*/
|
|
1789
3014
|
export const filter: {
|
|
@@ -1792,29 +3017,24 @@ export const filter: {
|
|
|
1792
3017
|
*
|
|
1793
3018
|
* If you need to change the type of the `Option` in addition to filtering, see `filterMap`.
|
|
1794
3019
|
*
|
|
1795
|
-
* @param predicate - A predicate function to apply to the `Option` value.
|
|
1796
|
-
* @param fb - The `Option` to filter.
|
|
1797
|
-
*
|
|
1798
3020
|
* @example
|
|
1799
3021
|
* ```ts
|
|
1800
3022
|
* import { Option } from "effect"
|
|
1801
3023
|
*
|
|
1802
|
-
*
|
|
1803
|
-
*
|
|
3024
|
+
* const removeEmptyString = (input: Option.Option<string>) =>
|
|
3025
|
+
* Option.filter(input, (value) => value !== "")
|
|
1804
3026
|
*
|
|
1805
|
-
*
|
|
1806
|
-
*
|
|
1807
|
-
* assert.deepStrictEqual(Option.filter(Option.some(2), isEven), Option.some(2))
|
|
3027
|
+
* console.log(removeEmptyString(Option.none()))
|
|
3028
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1808
3029
|
*
|
|
1809
|
-
*
|
|
1810
|
-
*
|
|
3030
|
+
* console.log(removeEmptyString(Option.some("")))
|
|
3031
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1811
3032
|
*
|
|
1812
|
-
*
|
|
1813
|
-
*
|
|
1814
|
-
* assert.deepStrictEqual(Option.filter(Option.some(2), isNumber), Option.some(2))
|
|
3033
|
+
* console.log(removeEmptyString(Option.some("a")))
|
|
3034
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
1815
3035
|
* ```
|
|
1816
3036
|
*
|
|
1817
|
-
* @category
|
|
3037
|
+
* @category Filtering
|
|
1818
3038
|
* @since 2.0.0
|
|
1819
3039
|
*/
|
|
1820
3040
|
<A, B extends A>(refinement: Refinement<NoInfer<A>, B>): (self: Option<A>) => Option<B>
|
|
@@ -1823,29 +3043,24 @@ export const filter: {
|
|
|
1823
3043
|
*
|
|
1824
3044
|
* If you need to change the type of the `Option` in addition to filtering, see `filterMap`.
|
|
1825
3045
|
*
|
|
1826
|
-
* @param predicate - A predicate function to apply to the `Option` value.
|
|
1827
|
-
* @param fb - The `Option` to filter.
|
|
1828
|
-
*
|
|
1829
3046
|
* @example
|
|
1830
3047
|
* ```ts
|
|
1831
3048
|
* import { Option } from "effect"
|
|
1832
3049
|
*
|
|
1833
|
-
*
|
|
1834
|
-
*
|
|
3050
|
+
* const removeEmptyString = (input: Option.Option<string>) =>
|
|
3051
|
+
* Option.filter(input, (value) => value !== "")
|
|
1835
3052
|
*
|
|
1836
|
-
*
|
|
1837
|
-
*
|
|
1838
|
-
* assert.deepStrictEqual(Option.filter(Option.some(2), isEven), Option.some(2))
|
|
3053
|
+
* console.log(removeEmptyString(Option.none()))
|
|
3054
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1839
3055
|
*
|
|
1840
|
-
*
|
|
1841
|
-
*
|
|
3056
|
+
* console.log(removeEmptyString(Option.some("")))
|
|
3057
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1842
3058
|
*
|
|
1843
|
-
*
|
|
1844
|
-
*
|
|
1845
|
-
* assert.deepStrictEqual(Option.filter(Option.some(2), isNumber), Option.some(2))
|
|
3059
|
+
* console.log(removeEmptyString(Option.some("a")))
|
|
3060
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
1846
3061
|
* ```
|
|
1847
3062
|
*
|
|
1848
|
-
* @category
|
|
3063
|
+
* @category Filtering
|
|
1849
3064
|
* @since 2.0.0
|
|
1850
3065
|
*/
|
|
1851
3066
|
<A>(predicate: Predicate<NoInfer<A>>): (self: Option<A>) => Option<A>
|
|
@@ -1854,29 +3069,24 @@ export const filter: {
|
|
|
1854
3069
|
*
|
|
1855
3070
|
* If you need to change the type of the `Option` in addition to filtering, see `filterMap`.
|
|
1856
3071
|
*
|
|
1857
|
-
* @param predicate - A predicate function to apply to the `Option` value.
|
|
1858
|
-
* @param fb - The `Option` to filter.
|
|
1859
|
-
*
|
|
1860
3072
|
* @example
|
|
1861
3073
|
* ```ts
|
|
1862
3074
|
* import { Option } from "effect"
|
|
1863
3075
|
*
|
|
1864
|
-
*
|
|
1865
|
-
*
|
|
3076
|
+
* const removeEmptyString = (input: Option.Option<string>) =>
|
|
3077
|
+
* Option.filter(input, (value) => value !== "")
|
|
1866
3078
|
*
|
|
1867
|
-
*
|
|
1868
|
-
*
|
|
1869
|
-
* assert.deepStrictEqual(Option.filter(Option.some(2), isEven), Option.some(2))
|
|
3079
|
+
* console.log(removeEmptyString(Option.none()))
|
|
3080
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1870
3081
|
*
|
|
1871
|
-
*
|
|
1872
|
-
*
|
|
3082
|
+
* console.log(removeEmptyString(Option.some("")))
|
|
3083
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1873
3084
|
*
|
|
1874
|
-
*
|
|
1875
|
-
*
|
|
1876
|
-
* assert.deepStrictEqual(Option.filter(Option.some(2), isNumber), Option.some(2))
|
|
3085
|
+
* console.log(removeEmptyString(Option.some("a")))
|
|
3086
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
1877
3087
|
* ```
|
|
1878
3088
|
*
|
|
1879
|
-
* @category
|
|
3089
|
+
* @category Filtering
|
|
1880
3090
|
* @since 2.0.0
|
|
1881
3091
|
*/
|
|
1882
3092
|
<A, B extends A>(self: Option<A>, refinement: Refinement<A, B>): Option<B>
|
|
@@ -1885,29 +3095,24 @@ export const filter: {
|
|
|
1885
3095
|
*
|
|
1886
3096
|
* If you need to change the type of the `Option` in addition to filtering, see `filterMap`.
|
|
1887
3097
|
*
|
|
1888
|
-
* @param predicate - A predicate function to apply to the `Option` value.
|
|
1889
|
-
* @param fb - The `Option` to filter.
|
|
1890
|
-
*
|
|
1891
3098
|
* @example
|
|
1892
3099
|
* ```ts
|
|
1893
3100
|
* import { Option } from "effect"
|
|
1894
3101
|
*
|
|
1895
|
-
*
|
|
1896
|
-
*
|
|
3102
|
+
* const removeEmptyString = (input: Option.Option<string>) =>
|
|
3103
|
+
* Option.filter(input, (value) => value !== "")
|
|
1897
3104
|
*
|
|
1898
|
-
*
|
|
1899
|
-
*
|
|
1900
|
-
* assert.deepStrictEqual(Option.filter(Option.some(2), isEven), Option.some(2))
|
|
3105
|
+
* console.log(removeEmptyString(Option.none()))
|
|
3106
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1901
3107
|
*
|
|
1902
|
-
*
|
|
1903
|
-
*
|
|
3108
|
+
* console.log(removeEmptyString(Option.some("")))
|
|
3109
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1904
3110
|
*
|
|
1905
|
-
*
|
|
1906
|
-
*
|
|
1907
|
-
* assert.deepStrictEqual(Option.filter(Option.some(2), isNumber), Option.some(2))
|
|
3111
|
+
* console.log(removeEmptyString(Option.some("a")))
|
|
3112
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
1908
3113
|
* ```
|
|
1909
3114
|
*
|
|
1910
|
-
* @category
|
|
3115
|
+
* @category Filtering
|
|
1911
3116
|
* @since 2.0.0
|
|
1912
3117
|
*/
|
|
1913
3118
|
<A>(self: Option<A>, predicate: Predicate<A>): Option<A>
|
|
@@ -1918,55 +3123,122 @@ export const filter: {
|
|
|
1918
3123
|
)
|
|
1919
3124
|
|
|
1920
3125
|
/**
|
|
3126
|
+
* Creates an `Equivalence` instance for comparing `Option` values, using a
|
|
3127
|
+
* provided `Equivalence` for the inner type.
|
|
3128
|
+
*
|
|
3129
|
+
* **Details**
|
|
3130
|
+
*
|
|
3131
|
+
* This function takes an `Equivalence` instance for a specific type `A` and
|
|
3132
|
+
* produces an `Equivalence` instance for `Option<A>`. The resulting
|
|
3133
|
+
* `Equivalence` determines whether two `Option` values are equivalent:
|
|
3134
|
+
*
|
|
3135
|
+
* - Two `None`s are considered equivalent.
|
|
3136
|
+
* - A `Some` and a `None` are not equivalent.
|
|
3137
|
+
* - Two `Some` values are equivalent if their inner values are equivalent
|
|
3138
|
+
* according to the provided `Equivalence`.
|
|
3139
|
+
*
|
|
1921
3140
|
* @example
|
|
1922
3141
|
* ```ts
|
|
1923
|
-
*
|
|
3142
|
+
* // Title: Comparing Optional Numbers for Equivalence
|
|
3143
|
+
* import { Number, Option } from "effect"
|
|
1924
3144
|
*
|
|
1925
3145
|
* const isEquivalent = Option.getEquivalence(Number.Equivalence)
|
|
1926
|
-
*
|
|
1927
|
-
*
|
|
1928
|
-
*
|
|
1929
|
-
*
|
|
1930
|
-
*
|
|
3146
|
+
*
|
|
3147
|
+
* console.log(isEquivalent(Option.none(), Option.none()))
|
|
3148
|
+
* // Output: true
|
|
3149
|
+
*
|
|
3150
|
+
* console.log(isEquivalent(Option.none(), Option.some(1)))
|
|
3151
|
+
* // Output: false
|
|
3152
|
+
*
|
|
3153
|
+
* console.log(isEquivalent(Option.some(1), Option.none()))
|
|
3154
|
+
* // Output: false
|
|
3155
|
+
*
|
|
3156
|
+
* console.log(isEquivalent(Option.some(1), Option.some(2)))
|
|
3157
|
+
* // Output: false
|
|
3158
|
+
*
|
|
3159
|
+
* console.log(isEquivalent(Option.some(1), Option.some(1)))
|
|
3160
|
+
* // Output: true
|
|
1931
3161
|
* ```
|
|
1932
3162
|
*
|
|
1933
|
-
* @category
|
|
3163
|
+
* @category Equivalence
|
|
1934
3164
|
* @since 2.0.0
|
|
1935
3165
|
*/
|
|
1936
3166
|
export const getEquivalence = <A>(isEquivalent: Equivalence.Equivalence<A>): Equivalence.Equivalence<Option<A>> =>
|
|
1937
3167
|
Equivalence.make((x, y) => isNone(x) ? isNone(y) : isNone(y) ? false : isEquivalent(x.value, y.value))
|
|
1938
3168
|
|
|
1939
3169
|
/**
|
|
1940
|
-
*
|
|
1941
|
-
* `
|
|
1942
|
-
*
|
|
3170
|
+
* Creates an `Order` instance for comparing `Option` values, using a provided
|
|
3171
|
+
* `Order` for the inner type.
|
|
3172
|
+
*
|
|
3173
|
+
* **Details**
|
|
3174
|
+
*
|
|
3175
|
+
* This function produces an `Order` instance for `Option<A>`, allowing `Option`
|
|
3176
|
+
* values to be compared:
|
|
1943
3177
|
*
|
|
1944
|
-
* `None` is considered
|
|
3178
|
+
* - `None` is always considered less than any `Some` value.
|
|
3179
|
+
* - If both are `Some`, their inner values are compared using the provided
|
|
3180
|
+
* `Order` instance.
|
|
1945
3181
|
*
|
|
1946
3182
|
* @example
|
|
1947
3183
|
* ```ts
|
|
1948
|
-
* import {
|
|
1949
|
-
*
|
|
1950
|
-
* const
|
|
1951
|
-
*
|
|
1952
|
-
*
|
|
1953
|
-
*
|
|
1954
|
-
*
|
|
1955
|
-
*
|
|
3184
|
+
* import { Number, Option } from "effect"
|
|
3185
|
+
*
|
|
3186
|
+
* const order = Option.getOrder(Number.Order)
|
|
3187
|
+
*
|
|
3188
|
+
* console.log(order(Option.none(), Option.none()))
|
|
3189
|
+
* // Output: 0
|
|
3190
|
+
*
|
|
3191
|
+
* console.log(order(Option.none(), Option.some(1)))
|
|
3192
|
+
* // Output: -1
|
|
3193
|
+
*
|
|
3194
|
+
* console.log(order(Option.some(1), Option.none()))
|
|
3195
|
+
* // Output: 1
|
|
3196
|
+
*
|
|
3197
|
+
* console.log(order(Option.some(1), Option.some(2)))
|
|
3198
|
+
* // Output: -1
|
|
3199
|
+
*
|
|
3200
|
+
* console.log(order(Option.some(1), Option.some(1)))
|
|
3201
|
+
* // Output: 0
|
|
1956
3202
|
* ```
|
|
1957
3203
|
*
|
|
1958
|
-
* @category
|
|
3204
|
+
* @category Sorting
|
|
1959
3205
|
* @since 2.0.0
|
|
1960
3206
|
*/
|
|
1961
3207
|
export const getOrder = <A>(O: Order<A>): Order<Option<A>> =>
|
|
1962
3208
|
order.make((self, that) => isSome(self) ? (isSome(that) ? O(self.value, that.value) : 1) : -1)
|
|
1963
3209
|
|
|
1964
3210
|
/**
|
|
1965
|
-
* Lifts a binary function
|
|
3211
|
+
* Lifts a binary function to work with `Option` values, allowing the function
|
|
3212
|
+
* to operate on two `Option`s.
|
|
3213
|
+
*
|
|
3214
|
+
* **Details**
|
|
3215
|
+
*
|
|
3216
|
+
* This function takes a binary function `f` and returns a new function that
|
|
3217
|
+
* applies `f` to the values of two `Option`s (`self` and `that`). If both
|
|
3218
|
+
* `Option`s are `Some`, the binary function `f` is applied to their values, and
|
|
3219
|
+
* the result is wrapped in a new `Some`. If either `Option` is `None`, the
|
|
3220
|
+
* result is `None`.
|
|
1966
3221
|
*
|
|
1967
|
-
* @
|
|
3222
|
+
* @example
|
|
3223
|
+
* ```ts
|
|
3224
|
+
* import { Option } from "effect"
|
|
3225
|
+
*
|
|
3226
|
+
* // A binary function to add two numbers
|
|
3227
|
+
* const add = (a: number, b: number): number => a + b
|
|
3228
|
+
*
|
|
3229
|
+
* // Lift the `add` function to work with `Option` values
|
|
3230
|
+
* const addOptions = Option.lift2(add)
|
|
3231
|
+
*
|
|
3232
|
+
* // Both `Option`s are `Some`
|
|
3233
|
+
* console.log(addOptions(Option.some(2), Option.some(3)))
|
|
3234
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 5 }
|
|
3235
|
+
*
|
|
3236
|
+
* // One `Option` is `None`
|
|
3237
|
+
* console.log(addOptions(Option.some(2), Option.none()))
|
|
3238
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
3239
|
+
* ```
|
|
1968
3240
|
*
|
|
1969
|
-
* @category
|
|
3241
|
+
* @category Lifting
|
|
1970
3242
|
* @since 2.0.0
|
|
1971
3243
|
*/
|
|
1972
3244
|
export const lift2 = <A, B, C>(f: (a: A, b: B) => C): {
|
|
@@ -1975,83 +3247,135 @@ export const lift2 = <A, B, C>(f: (a: A, b: B) => C): {
|
|
|
1975
3247
|
} => dual(2, (self: Option<A>, that: Option<B>): Option<C> => zipWith(self, that, f))
|
|
1976
3248
|
|
|
1977
3249
|
/**
|
|
1978
|
-
*
|
|
1979
|
-
* if the predicate
|
|
3250
|
+
* Lifts a `Predicate` or `Refinement` into the `Option` context, returning a
|
|
3251
|
+
* `Some` of the input value if the predicate is satisfied, or `None` otherwise.
|
|
1980
3252
|
*
|
|
1981
|
-
*
|
|
3253
|
+
* **Details**
|
|
3254
|
+
*
|
|
3255
|
+
* This function transforms a `Predicate` (or a more specific `Refinement`) into
|
|
3256
|
+
* a function that produces an `Option`. If the predicate evaluates to `true`,
|
|
3257
|
+
* the input value is wrapped in a `Some`. If the predicate evaluates to
|
|
3258
|
+
* `false`, the result is `None`.
|
|
1982
3259
|
*
|
|
1983
3260
|
* @example
|
|
1984
3261
|
* ```ts
|
|
1985
3262
|
* import { Option } from "effect"
|
|
1986
3263
|
*
|
|
1987
|
-
*
|
|
3264
|
+
* // Check if a number is positive
|
|
3265
|
+
* const isPositive = (n: number) => n > 0
|
|
3266
|
+
*
|
|
3267
|
+
* // ┌─── (b: number) => Option<number>
|
|
3268
|
+
* // ▼
|
|
3269
|
+
* const parsePositive = Option.liftPredicate(isPositive)
|
|
1988
3270
|
*
|
|
1989
|
-
*
|
|
1990
|
-
*
|
|
3271
|
+
* console.log(parsePositive(1))
|
|
3272
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
3273
|
+
*
|
|
3274
|
+
* console.log(parsePositive(-1))
|
|
3275
|
+
* // OUtput: { _id: 'Option', _tag: 'None' }
|
|
1991
3276
|
* ```
|
|
1992
3277
|
*
|
|
1993
|
-
* @category
|
|
3278
|
+
* @category Lifting
|
|
1994
3279
|
* @since 2.0.0
|
|
1995
3280
|
*/
|
|
1996
3281
|
export const liftPredicate: { // Note: I intentionally avoid using the NoInfer pattern here.
|
|
1997
3282
|
<A, B extends A>(refinement: Refinement<A, B>): (a: A) => Option<B>
|
|
1998
3283
|
/**
|
|
1999
|
-
*
|
|
2000
|
-
* if the predicate
|
|
3284
|
+
* Lifts a `Predicate` or `Refinement` into the `Option` context, returning a
|
|
3285
|
+
* `Some` of the input value if the predicate is satisfied, or `None` otherwise.
|
|
3286
|
+
*
|
|
3287
|
+
* **Details**
|
|
2001
3288
|
*
|
|
2002
|
-
*
|
|
3289
|
+
* This function transforms a `Predicate` (or a more specific `Refinement`) into
|
|
3290
|
+
* a function that produces an `Option`. If the predicate evaluates to `true`,
|
|
3291
|
+
* the input value is wrapped in a `Some`. If the predicate evaluates to
|
|
3292
|
+
* `false`, the result is `None`.
|
|
2003
3293
|
*
|
|
2004
3294
|
* @example
|
|
2005
3295
|
* ```ts
|
|
2006
3296
|
* import { Option } from "effect"
|
|
2007
3297
|
*
|
|
2008
|
-
*
|
|
3298
|
+
* // Check if a number is positive
|
|
3299
|
+
* const isPositive = (n: number) => n > 0
|
|
2009
3300
|
*
|
|
2010
|
-
*
|
|
2011
|
-
*
|
|
3301
|
+
* // ┌─── (b: number) => Option<number>
|
|
3302
|
+
* // ▼
|
|
3303
|
+
* const parsePositive = Option.liftPredicate(isPositive)
|
|
3304
|
+
*
|
|
3305
|
+
* console.log(parsePositive(1))
|
|
3306
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
3307
|
+
*
|
|
3308
|
+
* console.log(parsePositive(-1))
|
|
3309
|
+
* // OUtput: { _id: 'Option', _tag: 'None' }
|
|
2012
3310
|
* ```
|
|
2013
3311
|
*
|
|
2014
|
-
* @category
|
|
3312
|
+
* @category Lifting
|
|
2015
3313
|
* @since 2.0.0
|
|
2016
3314
|
*/
|
|
2017
3315
|
<B extends A, A = B>(predicate: Predicate<A>): (b: B) => Option<B>
|
|
2018
3316
|
/**
|
|
2019
|
-
*
|
|
2020
|
-
* if the predicate
|
|
3317
|
+
* Lifts a `Predicate` or `Refinement` into the `Option` context, returning a
|
|
3318
|
+
* `Some` of the input value if the predicate is satisfied, or `None` otherwise.
|
|
2021
3319
|
*
|
|
2022
|
-
*
|
|
3320
|
+
* **Details**
|
|
3321
|
+
*
|
|
3322
|
+
* This function transforms a `Predicate` (or a more specific `Refinement`) into
|
|
3323
|
+
* a function that produces an `Option`. If the predicate evaluates to `true`,
|
|
3324
|
+
* the input value is wrapped in a `Some`. If the predicate evaluates to
|
|
3325
|
+
* `false`, the result is `None`.
|
|
2023
3326
|
*
|
|
2024
3327
|
* @example
|
|
2025
3328
|
* ```ts
|
|
2026
3329
|
* import { Option } from "effect"
|
|
2027
3330
|
*
|
|
2028
|
-
*
|
|
3331
|
+
* // Check if a number is positive
|
|
3332
|
+
* const isPositive = (n: number) => n > 0
|
|
3333
|
+
*
|
|
3334
|
+
* // ┌─── (b: number) => Option<number>
|
|
3335
|
+
* // ▼
|
|
3336
|
+
* const parsePositive = Option.liftPredicate(isPositive)
|
|
2029
3337
|
*
|
|
2030
|
-
*
|
|
2031
|
-
*
|
|
3338
|
+
* console.log(parsePositive(1))
|
|
3339
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
3340
|
+
*
|
|
3341
|
+
* console.log(parsePositive(-1))
|
|
3342
|
+
* // OUtput: { _id: 'Option', _tag: 'None' }
|
|
2032
3343
|
* ```
|
|
2033
3344
|
*
|
|
2034
|
-
* @category
|
|
3345
|
+
* @category Lifting
|
|
2035
3346
|
* @since 2.0.0
|
|
2036
3347
|
*/
|
|
2037
3348
|
<A, B extends A>(self: A, refinement: Refinement<A, B>): Option<B>
|
|
2038
3349
|
/**
|
|
2039
|
-
*
|
|
2040
|
-
* if the predicate
|
|
3350
|
+
* Lifts a `Predicate` or `Refinement` into the `Option` context, returning a
|
|
3351
|
+
* `Some` of the input value if the predicate is satisfied, or `None` otherwise.
|
|
3352
|
+
*
|
|
3353
|
+
* **Details**
|
|
2041
3354
|
*
|
|
2042
|
-
*
|
|
3355
|
+
* This function transforms a `Predicate` (or a more specific `Refinement`) into
|
|
3356
|
+
* a function that produces an `Option`. If the predicate evaluates to `true`,
|
|
3357
|
+
* the input value is wrapped in a `Some`. If the predicate evaluates to
|
|
3358
|
+
* `false`, the result is `None`.
|
|
2043
3359
|
*
|
|
2044
3360
|
* @example
|
|
2045
3361
|
* ```ts
|
|
2046
3362
|
* import { Option } from "effect"
|
|
2047
3363
|
*
|
|
2048
|
-
*
|
|
3364
|
+
* // Check if a number is positive
|
|
3365
|
+
* const isPositive = (n: number) => n > 0
|
|
3366
|
+
*
|
|
3367
|
+
* // ┌─── (b: number) => Option<number>
|
|
3368
|
+
* // ▼
|
|
3369
|
+
* const parsePositive = Option.liftPredicate(isPositive)
|
|
3370
|
+
*
|
|
3371
|
+
* console.log(parsePositive(1))
|
|
3372
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
2049
3373
|
*
|
|
2050
|
-
*
|
|
2051
|
-
*
|
|
3374
|
+
* console.log(parsePositive(-1))
|
|
3375
|
+
* // OUtput: { _id: 'Option', _tag: 'None' }
|
|
2052
3376
|
* ```
|
|
2053
3377
|
*
|
|
2054
|
-
* @category
|
|
3378
|
+
* @category Lifting
|
|
2055
3379
|
* @since 2.0.0
|
|
2056
3380
|
*/
|
|
2057
3381
|
<B extends A, A = B>(self: B, predicate: Predicate<A>): Option<B>
|
|
@@ -2061,22 +3385,36 @@ export const liftPredicate: { // Note: I intentionally avoid using the NoInfer p
|
|
|
2061
3385
|
)
|
|
2062
3386
|
|
|
2063
3387
|
/**
|
|
2064
|
-
* Returns a function that checks if
|
|
3388
|
+
* Returns a function that checks if an `Option` contains a specified value,
|
|
3389
|
+
* using a provided equivalence function.
|
|
2065
3390
|
*
|
|
2066
|
-
*
|
|
2067
|
-
*
|
|
2068
|
-
*
|
|
3391
|
+
* **Details**
|
|
3392
|
+
*
|
|
3393
|
+
* This function allows you to check whether an `Option` contains a specific
|
|
3394
|
+
* value. It uses an equivalence function `isEquivalent` to compare the value
|
|
3395
|
+
* inside the `Option` to the provided value. If the `Option` is `Some` and the
|
|
3396
|
+
* equivalence function returns `true`, the result is `true`. If the `Option` is
|
|
3397
|
+
* `None` or the values are not equivalent, the result is `false`.
|
|
3398
|
+
*
|
|
3399
|
+
* @see {@link contains} for a version that uses the default `Equivalence`.
|
|
2069
3400
|
*
|
|
2070
3401
|
* @example
|
|
2071
3402
|
* ```ts
|
|
2072
|
-
* import {
|
|
3403
|
+
* import { Number, Option } from "effect"
|
|
3404
|
+
*
|
|
3405
|
+
* const contains = Option.containsWith(Number.Equivalence)
|
|
3406
|
+
*
|
|
3407
|
+
* console.log(Option.some(2).pipe(contains(2)))
|
|
3408
|
+
* // Output: true
|
|
2073
3409
|
*
|
|
2074
|
-
*
|
|
2075
|
-
*
|
|
2076
|
-
*
|
|
3410
|
+
* console.log(Option.some(1).pipe(contains(2)))
|
|
3411
|
+
* // Output: false
|
|
3412
|
+
*
|
|
3413
|
+
* console.log(Option.none().pipe(contains(2)))
|
|
3414
|
+
* // Output: false
|
|
2077
3415
|
* ```
|
|
2078
3416
|
*
|
|
2079
|
-
* @category
|
|
3417
|
+
* @category Elements
|
|
2080
3418
|
* @since 2.0.0
|
|
2081
3419
|
*/
|
|
2082
3420
|
export const containsWith = <A>(isEquivalent: (self: A, that: A) => boolean): {
|
|
@@ -2087,125 +3425,265 @@ export const containsWith = <A>(isEquivalent: (self: A, that: A) => boolean): {
|
|
|
2087
3425
|
const _equivalence = Equal.equivalence()
|
|
2088
3426
|
|
|
2089
3427
|
/**
|
|
2090
|
-
* Returns a function that checks if an `Option` contains a
|
|
3428
|
+
* Returns a function that checks if an `Option` contains a specified value
|
|
3429
|
+
* using the default `Equivalence`.
|
|
3430
|
+
*
|
|
3431
|
+
* **Details**
|
|
3432
|
+
*
|
|
3433
|
+
* This function allows you to check whether an `Option` contains a specific
|
|
3434
|
+
* value. It uses the default `Equivalence` for equality comparison. If the
|
|
3435
|
+
* `Option` is `Some` and its value is equivalent to the provided value, the
|
|
3436
|
+
* result is `true`. If the `Option` is `None` or the values are not equivalent,
|
|
3437
|
+
* the result is `false`.
|
|
3438
|
+
*
|
|
3439
|
+
* @see {@link containsWith} for a version that allows you to specify a custom equivalence function.
|
|
3440
|
+
*
|
|
3441
|
+
* @example
|
|
3442
|
+
* ```ts
|
|
3443
|
+
* import { Option } from "effect"
|
|
2091
3444
|
*
|
|
2092
|
-
*
|
|
3445
|
+
* console.log(Option.some(2).pipe(Option.contains(2)))
|
|
3446
|
+
* // Output: true
|
|
3447
|
+
*
|
|
3448
|
+
* console.log(Option.some(1).pipe(Option.contains(2)))
|
|
3449
|
+
* // Output: false
|
|
3450
|
+
*
|
|
3451
|
+
* console.log(Option.none().pipe(Option.contains(2)))
|
|
3452
|
+
* // Output: false
|
|
3453
|
+
* ```
|
|
3454
|
+
*
|
|
3455
|
+
* @category Elements
|
|
2093
3456
|
* @since 2.0.0
|
|
2094
3457
|
*/
|
|
2095
3458
|
export const contains: {
|
|
2096
3459
|
/**
|
|
2097
|
-
* Returns a function that checks if an `Option` contains a
|
|
3460
|
+
* Returns a function that checks if an `Option` contains a specified value
|
|
3461
|
+
* using the default `Equivalence`.
|
|
3462
|
+
*
|
|
3463
|
+
* **Details**
|
|
3464
|
+
*
|
|
3465
|
+
* This function allows you to check whether an `Option` contains a specific
|
|
3466
|
+
* value. It uses the default `Equivalence` for equality comparison. If the
|
|
3467
|
+
* `Option` is `Some` and its value is equivalent to the provided value, the
|
|
3468
|
+
* result is `true`. If the `Option` is `None` or the values are not equivalent,
|
|
3469
|
+
* the result is `false`.
|
|
3470
|
+
*
|
|
3471
|
+
* @see {@link containsWith} for a version that allows you to specify a custom equivalence function.
|
|
3472
|
+
*
|
|
3473
|
+
* @example
|
|
3474
|
+
* ```ts
|
|
3475
|
+
* import { Option } from "effect"
|
|
3476
|
+
*
|
|
3477
|
+
* console.log(Option.some(2).pipe(Option.contains(2)))
|
|
3478
|
+
* // Output: true
|
|
2098
3479
|
*
|
|
2099
|
-
*
|
|
3480
|
+
* console.log(Option.some(1).pipe(Option.contains(2)))
|
|
3481
|
+
* // Output: false
|
|
3482
|
+
*
|
|
3483
|
+
* console.log(Option.none().pipe(Option.contains(2)))
|
|
3484
|
+
* // Output: false
|
|
3485
|
+
* ```
|
|
3486
|
+
*
|
|
3487
|
+
* @category Elements
|
|
2100
3488
|
* @since 2.0.0
|
|
2101
3489
|
*/
|
|
2102
3490
|
<A>(a: A): (self: Option<A>) => boolean
|
|
2103
3491
|
/**
|
|
2104
|
-
* Returns a function that checks if an `Option` contains a
|
|
3492
|
+
* Returns a function that checks if an `Option` contains a specified value
|
|
3493
|
+
* using the default `Equivalence`.
|
|
3494
|
+
*
|
|
3495
|
+
* **Details**
|
|
3496
|
+
*
|
|
3497
|
+
* This function allows you to check whether an `Option` contains a specific
|
|
3498
|
+
* value. It uses the default `Equivalence` for equality comparison. If the
|
|
3499
|
+
* `Option` is `Some` and its value is equivalent to the provided value, the
|
|
3500
|
+
* result is `true`. If the `Option` is `None` or the values are not equivalent,
|
|
3501
|
+
* the result is `false`.
|
|
3502
|
+
*
|
|
3503
|
+
* @see {@link containsWith} for a version that allows you to specify a custom equivalence function.
|
|
3504
|
+
*
|
|
3505
|
+
* @example
|
|
3506
|
+
* ```ts
|
|
3507
|
+
* import { Option } from "effect"
|
|
3508
|
+
*
|
|
3509
|
+
* console.log(Option.some(2).pipe(Option.contains(2)))
|
|
3510
|
+
* // Output: true
|
|
3511
|
+
*
|
|
3512
|
+
* console.log(Option.some(1).pipe(Option.contains(2)))
|
|
3513
|
+
* // Output: false
|
|
2105
3514
|
*
|
|
2106
|
-
*
|
|
3515
|
+
* console.log(Option.none().pipe(Option.contains(2)))
|
|
3516
|
+
* // Output: false
|
|
3517
|
+
* ```
|
|
3518
|
+
*
|
|
3519
|
+
* @category Elements
|
|
2107
3520
|
* @since 2.0.0
|
|
2108
3521
|
*/
|
|
2109
3522
|
<A>(self: Option<A>, a: A): boolean
|
|
2110
3523
|
} = containsWith(_equivalence)
|
|
2111
3524
|
|
|
2112
3525
|
/**
|
|
2113
|
-
*
|
|
3526
|
+
* Checks if a value in an `Option` satisfies a given predicate or refinement.
|
|
3527
|
+
*
|
|
3528
|
+
* **Details**
|
|
3529
|
+
*
|
|
3530
|
+
* This function allows you to check if a value inside a `Some` meets a
|
|
3531
|
+
* specified condition. If the `Option` is `None`, the result is `false`. If the
|
|
3532
|
+
* `Option` is `Some`, the provided predicate or refinement is applied to the
|
|
3533
|
+
* value:
|
|
2114
3534
|
*
|
|
2115
|
-
*
|
|
2116
|
-
*
|
|
3535
|
+
* - If the condition is met, the result is `true`.
|
|
3536
|
+
* - If the condition is not met, the result is `false`.
|
|
2117
3537
|
*
|
|
2118
3538
|
* @example
|
|
2119
3539
|
* ```ts
|
|
2120
|
-
* import {
|
|
3540
|
+
* import { Option } from "effect"
|
|
2121
3541
|
*
|
|
2122
3542
|
* const isEven = (n: number) => n % 2 === 0
|
|
2123
3543
|
*
|
|
2124
|
-
*
|
|
2125
|
-
*
|
|
2126
|
-
*
|
|
3544
|
+
* console.log(Option.some(2).pipe(Option.exists(isEven)))
|
|
3545
|
+
* // Output: true
|
|
3546
|
+
*
|
|
3547
|
+
* console.log(Option.some(1).pipe(Option.exists(isEven)))
|
|
3548
|
+
* // Output: false
|
|
3549
|
+
*
|
|
3550
|
+
* console.log(Option.none().pipe(Option.exists(isEven)))
|
|
3551
|
+
* // Output: false
|
|
2127
3552
|
* ```
|
|
2128
3553
|
*
|
|
3554
|
+
* @category Elements
|
|
2129
3555
|
* @since 2.0.0
|
|
2130
3556
|
*/
|
|
2131
3557
|
export const exists: {
|
|
2132
3558
|
/**
|
|
2133
|
-
*
|
|
3559
|
+
* Checks if a value in an `Option` satisfies a given predicate or refinement.
|
|
3560
|
+
*
|
|
3561
|
+
* **Details**
|
|
2134
3562
|
*
|
|
2135
|
-
*
|
|
2136
|
-
*
|
|
3563
|
+
* This function allows you to check if a value inside a `Some` meets a
|
|
3564
|
+
* specified condition. If the `Option` is `None`, the result is `false`. If the
|
|
3565
|
+
* `Option` is `Some`, the provided predicate or refinement is applied to the
|
|
3566
|
+
* value:
|
|
3567
|
+
*
|
|
3568
|
+
* - If the condition is met, the result is `true`.
|
|
3569
|
+
* - If the condition is not met, the result is `false`.
|
|
2137
3570
|
*
|
|
2138
3571
|
* @example
|
|
2139
3572
|
* ```ts
|
|
2140
|
-
* import {
|
|
3573
|
+
* import { Option } from "effect"
|
|
2141
3574
|
*
|
|
2142
3575
|
* const isEven = (n: number) => n % 2 === 0
|
|
2143
3576
|
*
|
|
2144
|
-
*
|
|
2145
|
-
*
|
|
2146
|
-
*
|
|
3577
|
+
* console.log(Option.some(2).pipe(Option.exists(isEven)))
|
|
3578
|
+
* // Output: true
|
|
3579
|
+
*
|
|
3580
|
+
* console.log(Option.some(1).pipe(Option.exists(isEven)))
|
|
3581
|
+
* // Output: false
|
|
3582
|
+
*
|
|
3583
|
+
* console.log(Option.none().pipe(Option.exists(isEven)))
|
|
3584
|
+
* // Output: false
|
|
2147
3585
|
* ```
|
|
2148
3586
|
*
|
|
3587
|
+
* @category Elements
|
|
2149
3588
|
* @since 2.0.0
|
|
2150
3589
|
*/
|
|
2151
3590
|
<A, B extends A>(refinement: Refinement<NoInfer<A>, B>): (self: Option<A>) => self is Option<B>
|
|
2152
3591
|
/**
|
|
2153
|
-
*
|
|
3592
|
+
* Checks if a value in an `Option` satisfies a given predicate or refinement.
|
|
2154
3593
|
*
|
|
2155
|
-
*
|
|
2156
|
-
*
|
|
3594
|
+
* **Details**
|
|
3595
|
+
*
|
|
3596
|
+
* This function allows you to check if a value inside a `Some` meets a
|
|
3597
|
+
* specified condition. If the `Option` is `None`, the result is `false`. If the
|
|
3598
|
+
* `Option` is `Some`, the provided predicate or refinement is applied to the
|
|
3599
|
+
* value:
|
|
3600
|
+
*
|
|
3601
|
+
* - If the condition is met, the result is `true`.
|
|
3602
|
+
* - If the condition is not met, the result is `false`.
|
|
2157
3603
|
*
|
|
2158
3604
|
* @example
|
|
2159
3605
|
* ```ts
|
|
2160
|
-
* import {
|
|
3606
|
+
* import { Option } from "effect"
|
|
2161
3607
|
*
|
|
2162
3608
|
* const isEven = (n: number) => n % 2 === 0
|
|
2163
3609
|
*
|
|
2164
|
-
*
|
|
2165
|
-
*
|
|
2166
|
-
*
|
|
3610
|
+
* console.log(Option.some(2).pipe(Option.exists(isEven)))
|
|
3611
|
+
* // Output: true
|
|
3612
|
+
*
|
|
3613
|
+
* console.log(Option.some(1).pipe(Option.exists(isEven)))
|
|
3614
|
+
* // Output: false
|
|
3615
|
+
*
|
|
3616
|
+
* console.log(Option.none().pipe(Option.exists(isEven)))
|
|
3617
|
+
* // Output: false
|
|
2167
3618
|
* ```
|
|
2168
3619
|
*
|
|
3620
|
+
* @category Elements
|
|
2169
3621
|
* @since 2.0.0
|
|
2170
3622
|
*/
|
|
2171
3623
|
<A>(predicate: Predicate<NoInfer<A>>): (self: Option<A>) => boolean
|
|
2172
3624
|
/**
|
|
2173
|
-
*
|
|
3625
|
+
* Checks if a value in an `Option` satisfies a given predicate or refinement.
|
|
3626
|
+
*
|
|
3627
|
+
* **Details**
|
|
2174
3628
|
*
|
|
2175
|
-
*
|
|
2176
|
-
*
|
|
3629
|
+
* This function allows you to check if a value inside a `Some` meets a
|
|
3630
|
+
* specified condition. If the `Option` is `None`, the result is `false`. If the
|
|
3631
|
+
* `Option` is `Some`, the provided predicate or refinement is applied to the
|
|
3632
|
+
* value:
|
|
3633
|
+
*
|
|
3634
|
+
* - If the condition is met, the result is `true`.
|
|
3635
|
+
* - If the condition is not met, the result is `false`.
|
|
2177
3636
|
*
|
|
2178
3637
|
* @example
|
|
2179
3638
|
* ```ts
|
|
2180
|
-
* import {
|
|
3639
|
+
* import { Option } from "effect"
|
|
2181
3640
|
*
|
|
2182
3641
|
* const isEven = (n: number) => n % 2 === 0
|
|
2183
3642
|
*
|
|
2184
|
-
*
|
|
2185
|
-
*
|
|
2186
|
-
*
|
|
3643
|
+
* console.log(Option.some(2).pipe(Option.exists(isEven)))
|
|
3644
|
+
* // Output: true
|
|
3645
|
+
*
|
|
3646
|
+
* console.log(Option.some(1).pipe(Option.exists(isEven)))
|
|
3647
|
+
* // Output: false
|
|
3648
|
+
*
|
|
3649
|
+
* console.log(Option.none().pipe(Option.exists(isEven)))
|
|
3650
|
+
* // Output: false
|
|
2187
3651
|
* ```
|
|
2188
3652
|
*
|
|
3653
|
+
* @category Elements
|
|
2189
3654
|
* @since 2.0.0
|
|
2190
3655
|
*/
|
|
2191
3656
|
<A, B extends A>(self: Option<A>, refinement: Refinement<A, B>): self is Option<B>
|
|
2192
3657
|
/**
|
|
2193
|
-
*
|
|
3658
|
+
* Checks if a value in an `Option` satisfies a given predicate or refinement.
|
|
3659
|
+
*
|
|
3660
|
+
* **Details**
|
|
3661
|
+
*
|
|
3662
|
+
* This function allows you to check if a value inside a `Some` meets a
|
|
3663
|
+
* specified condition. If the `Option` is `None`, the result is `false`. If the
|
|
3664
|
+
* `Option` is `Some`, the provided predicate or refinement is applied to the
|
|
3665
|
+
* value:
|
|
2194
3666
|
*
|
|
2195
|
-
*
|
|
2196
|
-
*
|
|
3667
|
+
* - If the condition is met, the result is `true`.
|
|
3668
|
+
* - If the condition is not met, the result is `false`.
|
|
2197
3669
|
*
|
|
2198
3670
|
* @example
|
|
2199
3671
|
* ```ts
|
|
2200
|
-
* import {
|
|
3672
|
+
* import { Option } from "effect"
|
|
2201
3673
|
*
|
|
2202
3674
|
* const isEven = (n: number) => n % 2 === 0
|
|
2203
3675
|
*
|
|
2204
|
-
*
|
|
2205
|
-
*
|
|
2206
|
-
*
|
|
3676
|
+
* console.log(Option.some(2).pipe(Option.exists(isEven)))
|
|
3677
|
+
* // Output: true
|
|
3678
|
+
*
|
|
3679
|
+
* console.log(Option.some(1).pipe(Option.exists(isEven)))
|
|
3680
|
+
* // Output: false
|
|
3681
|
+
*
|
|
3682
|
+
* console.log(Option.none().pipe(Option.exists(isEven)))
|
|
3683
|
+
* // Output: false
|
|
2207
3684
|
* ```
|
|
2208
3685
|
*
|
|
3686
|
+
* @category Elements
|
|
2209
3687
|
* @since 2.0.0
|
|
2210
3688
|
*/
|
|
2211
3689
|
<A>(self: Option<A>, predicate: Predicate<A>): boolean
|
|
@@ -2248,7 +3726,7 @@ export const exists: {
|
|
|
2248
3726
|
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
2249
3727
|
* ```
|
|
2250
3728
|
*
|
|
2251
|
-
* @category
|
|
3729
|
+
* @category Do notation
|
|
2252
3730
|
* @since 2.0.0
|
|
2253
3731
|
*/
|
|
2254
3732
|
export const bindTo: {
|
|
@@ -2285,7 +3763,7 @@ export const bindTo: {
|
|
|
2285
3763
|
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
2286
3764
|
* ```
|
|
2287
3765
|
*
|
|
2288
|
-
* @category
|
|
3766
|
+
* @category Do notation
|
|
2289
3767
|
* @since 2.0.0
|
|
2290
3768
|
*/
|
|
2291
3769
|
<N extends string>(name: N): <A>(self: Option<A>) => Option<{ [K in N]: A }>
|
|
@@ -2322,7 +3800,7 @@ export const bindTo: {
|
|
|
2322
3800
|
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
2323
3801
|
* ```
|
|
2324
3802
|
*
|
|
2325
|
-
* @category
|
|
3803
|
+
* @category Do notation
|
|
2326
3804
|
* @since 2.0.0
|
|
2327
3805
|
*/
|
|
2328
3806
|
<A, N extends string>(self: Option<A>, name: N): Option<{ [K in N]: A }>
|
|
@@ -2370,7 +3848,7 @@ export {
|
|
|
2370
3848
|
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
2371
3849
|
*
|
|
2372
3850
|
* ```
|
|
2373
|
-
* @category
|
|
3851
|
+
* @category Do notation
|
|
2374
3852
|
* @since 2.0.0
|
|
2375
3853
|
*/
|
|
2376
3854
|
let_ as let
|
|
@@ -2405,7 +3883,7 @@ export {
|
|
|
2405
3883
|
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
2406
3884
|
* ```
|
|
2407
3885
|
*
|
|
2408
|
-
* @category
|
|
3886
|
+
* @category Do notation
|
|
2409
3887
|
* @since 2.0.0
|
|
2410
3888
|
*/
|
|
2411
3889
|
export const bind: {
|
|
@@ -2438,7 +3916,7 @@ export const bind: {
|
|
|
2438
3916
|
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
2439
3917
|
* ```
|
|
2440
3918
|
*
|
|
2441
|
-
* @category
|
|
3919
|
+
* @category Do notation
|
|
2442
3920
|
* @since 2.0.0
|
|
2443
3921
|
*/
|
|
2444
3922
|
<N extends string, A extends object, B>(
|
|
@@ -2474,7 +3952,7 @@ export const bind: {
|
|
|
2474
3952
|
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
2475
3953
|
* ```
|
|
2476
3954
|
*
|
|
2477
|
-
* @category
|
|
3955
|
+
* @category Do notation
|
|
2478
3956
|
* @since 2.0.0
|
|
2479
3957
|
*/
|
|
2480
3958
|
<A extends object, N extends string, B>(
|
|
@@ -2513,7 +3991,7 @@ export const bind: {
|
|
|
2513
3991
|
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
2514
3992
|
* ```
|
|
2515
3993
|
*
|
|
2516
|
-
* @category
|
|
3994
|
+
* @category Do notation
|
|
2517
3995
|
* @since 2.0.0
|
|
2518
3996
|
*/
|
|
2519
3997
|
export const Do: Option<{}> = some({})
|
|
@@ -2521,44 +3999,60 @@ export const Do: Option<{}> = some({})
|
|
|
2521
3999
|
const adapter = Gen.adapter<OptionTypeLambda>()
|
|
2522
4000
|
|
|
2523
4001
|
/**
|
|
2524
|
-
*
|
|
4002
|
+
* Similar to `Effect.gen`, `Option.gen` provides a more readable,
|
|
4003
|
+
* generator-based syntax for working with `Option` values, making code that
|
|
4004
|
+
* involves `Option` easier to write and understand. This approach is similar to
|
|
4005
|
+
* using `async/await` but tailored for `Option`.
|
|
4006
|
+
*
|
|
4007
|
+
* @example
|
|
4008
|
+
* ```ts
|
|
4009
|
+
* // Title: Using Option.gen to Create a Combined Value
|
|
4010
|
+
* import { Option } from "effect"
|
|
4011
|
+
*
|
|
4012
|
+
* const maybeName: Option.Option<string> = Option.some("John")
|
|
4013
|
+
* const maybeAge: Option.Option<number> = Option.some(25)
|
|
4014
|
+
*
|
|
4015
|
+
* const person = Option.gen(function* () {
|
|
4016
|
+
* const name = (yield* maybeName).toUpperCase()
|
|
4017
|
+
* const age = yield* maybeAge
|
|
4018
|
+
* return { name, age }
|
|
4019
|
+
* })
|
|
4020
|
+
*
|
|
4021
|
+
* console.log(person)
|
|
4022
|
+
* // Output:
|
|
4023
|
+
* // { _id: 'Option', _tag: 'Some', value: { name: 'JOHN', age: 25 } }
|
|
4024
|
+
* ```
|
|
4025
|
+
*
|
|
4026
|
+
* @category Generators
|
|
2525
4027
|
* @since 2.0.0
|
|
2526
4028
|
*/
|
|
2527
4029
|
export const gen: Gen.Gen<OptionTypeLambda, Gen.Adapter<OptionTypeLambda>> = (...args) => {
|
|
2528
|
-
|
|
2529
|
-
if (args.length === 1) {
|
|
2530
|
-
f = args[0]
|
|
2531
|
-
} else {
|
|
2532
|
-
f = args[1].bind(args[0])
|
|
2533
|
-
}
|
|
4030
|
+
const f = args.length === 1 ? args[0] : args[1].bind(args[0])
|
|
2534
4031
|
const iterator = f(adapter)
|
|
2535
|
-
let state:
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2540
|
-
if (Gen.isGenKind(current)) {
|
|
2541
|
-
current = current.value
|
|
2542
|
-
} else {
|
|
2543
|
-
current = Gen.yieldWrapGet(current)
|
|
2544
|
-
}
|
|
4032
|
+
let state: IteratorResult<any> = iterator.next()
|
|
4033
|
+
while (!state.done) {
|
|
4034
|
+
const current = Gen.isGenKind(state.value)
|
|
4035
|
+
? state.value.value
|
|
4036
|
+
: Gen.yieldWrapGet(state.value)
|
|
2545
4037
|
if (isNone(current)) {
|
|
2546
4038
|
return current
|
|
2547
4039
|
}
|
|
2548
|
-
|
|
2549
|
-
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
|
|
2553
|
-
|
|
2554
|
-
|
|
2555
|
-
|
|
2556
|
-
|
|
2557
|
-
|
|
2558
|
-
|
|
2559
|
-
|
|
2560
|
-
|
|
2561
|
-
|
|
2562
|
-
|
|
4040
|
+
state = iterator.next(current.value as never)
|
|
4041
|
+
}
|
|
4042
|
+
return some(state.value)
|
|
4043
|
+
}
|
|
4044
|
+
|
|
4045
|
+
/**
|
|
4046
|
+
* Merges two optional values, applying a function if both exist.
|
|
4047
|
+
* Unlike {@link zipWith}, this function returns `None` only if both inputs are `None`.
|
|
4048
|
+
*
|
|
4049
|
+
* @internal
|
|
4050
|
+
*/
|
|
4051
|
+
export const mergeWith = <A>(f: (a1: A, a2: A) => A) => (o1: Option<A>, o2: Option<A>): Option<A> => {
|
|
4052
|
+
if (isNone(o1)) {
|
|
4053
|
+
return o2
|
|
4054
|
+
} else if (isNone(o2)) {
|
|
4055
|
+
return o1
|
|
2563
4056
|
}
|
|
4057
|
+
return some(f(o1.value, o2.value))
|
|
2564
4058
|
}
|