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