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/cjs/Option.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.zipWith = exports.zipRight = exports.zipLeft = exports.void = exports.toRefinement = exports.toArray = exports.tap = exports.some = exports.reduceCompact = exports.productMany = exports.product = exports.partitionMap = exports.orElseSome = exports.orElseEither = exports.orElse = exports.none = exports.match = exports.map = exports.liftThrowable = exports.liftPredicate = exports.liftNullable = exports.lift2 = exports.let = exports.isSome = exports.isOption = exports.isNone = exports.getRight = exports.getOrder = exports.getOrUndefined = exports.getOrThrowWith = exports.getOrThrow = exports.getOrNull = exports.getOrElse = exports.getLeft = exports.getEquivalence = exports.gen = exports.fromNullable = exports.fromIterable = exports.flatten = exports.flatMapNullable = exports.flatMap = exports.firstSomeOf = exports.filterMap = exports.filter = exports.exists = exports.containsWith = exports.contains = exports.composeK = exports.bindTo = exports.bind = exports.asVoid = exports.as = exports.ap = exports.andThen = exports.all = exports.TypeId = exports.Do = void 0;
|
|
6
|
+
exports.zipWith = exports.zipRight = exports.zipLeft = exports.void = exports.toRefinement = exports.toArray = exports.tap = exports.some = exports.reduceCompact = exports.productMany = exports.product = exports.partitionMap = exports.orElseSome = exports.orElseEither = exports.orElse = exports.none = exports.mergeWith = exports.match = exports.map = exports.liftThrowable = exports.liftPredicate = exports.liftNullable = exports.lift2 = exports.let = exports.isSome = exports.isOption = exports.isNone = exports.getRight = exports.getOrder = exports.getOrUndefined = exports.getOrThrowWith = exports.getOrThrow = exports.getOrNull = exports.getOrElse = exports.getLeft = exports.getEquivalence = exports.gen = exports.fromNullable = exports.fromIterable = exports.flatten = exports.flatMapNullable = exports.flatMap = exports.firstSomeOf = exports.filterMap = exports.filter = exports.exists = exports.containsWith = exports.contains = exports.composeK = exports.bindTo = exports.bind = exports.asVoid = exports.as = exports.ap = exports.andThen = exports.all = exports.TypeId = exports.Do = void 0;
|
|
7
7
|
var Equal = _interopRequireWildcard(require("./Equal.js"));
|
|
8
8
|
var Equivalence = _interopRequireWildcard(require("./Equivalence.js"));
|
|
9
9
|
var _Function = require("./Function.js");
|
|
@@ -15,103 +15,168 @@ var Gen = _interopRequireWildcard(require("./Utils.js"));
|
|
|
15
15
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
16
16
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
17
17
|
/**
|
|
18
|
-
* @category
|
|
18
|
+
* @category Symbols
|
|
19
19
|
* @since 2.0.0
|
|
20
20
|
*/
|
|
21
21
|
const TypeId = exports.TypeId = /*#__PURE__*/Symbol.for("effect/Option");
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
23
|
+
* Represents the absence of a value by creating an empty `Option`.
|
|
24
24
|
*
|
|
25
|
-
*
|
|
25
|
+
* `Option.none` returns an `Option<never>`, which is a subtype of `Option<A>`.
|
|
26
|
+
* This means you can use it in place of any `Option<A>` regardless of the type
|
|
27
|
+
* `A`.
|
|
28
|
+
*
|
|
29
|
+
* @see {@link some} for the opposite operation.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* // Title: Creating an Option with No Value
|
|
34
|
+
* import { Option } from "effect"
|
|
35
|
+
*
|
|
36
|
+
* // An Option holding no value
|
|
37
|
+
* //
|
|
38
|
+
* // ┌─── Option<never>
|
|
39
|
+
* // ▼
|
|
40
|
+
* const noValue = Option.none()
|
|
41
|
+
*
|
|
42
|
+
* console.log(noValue)
|
|
43
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @category Constructors
|
|
26
47
|
* @since 2.0.0
|
|
27
48
|
*/
|
|
28
49
|
const none = () => option.none;
|
|
29
50
|
/**
|
|
30
|
-
*
|
|
51
|
+
* Wraps the given value into an `Option` to represent its presence.
|
|
31
52
|
*
|
|
32
|
-
* @
|
|
53
|
+
* @see {@link none} for the opposite operation.
|
|
33
54
|
*
|
|
34
|
-
* @
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* // Title: Creating an Option with a Value
|
|
58
|
+
* import { Option } from "effect"
|
|
59
|
+
*
|
|
60
|
+
* // An Option holding the number 1
|
|
61
|
+
* //
|
|
62
|
+
* // ┌─── Option<number>
|
|
63
|
+
* // ▼
|
|
64
|
+
* const value = Option.some(1)
|
|
65
|
+
*
|
|
66
|
+
* console.log(value)
|
|
67
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
70
|
+
* @category Constructors
|
|
35
71
|
* @since 2.0.0
|
|
36
72
|
*/
|
|
37
73
|
exports.none = none;
|
|
38
74
|
const some = exports.some = option.some;
|
|
39
75
|
/**
|
|
40
|
-
*
|
|
76
|
+
* Determines whether the given value is an `Option`.
|
|
41
77
|
*
|
|
42
|
-
*
|
|
78
|
+
* **Details**
|
|
79
|
+
*
|
|
80
|
+
* This function checks if a value is an instance of `Option`. It returns `true`
|
|
81
|
+
* if the value is either `Option.some` or `Option.none`, and `false` otherwise.
|
|
82
|
+
* This is particularly useful when working with unknown values or when you need
|
|
83
|
+
* to ensure type safety in your code.
|
|
43
84
|
*
|
|
44
85
|
* @example
|
|
45
86
|
* ```ts
|
|
46
87
|
* import { Option } from "effect"
|
|
47
88
|
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
89
|
+
* console.log(Option.isOption(Option.some(1)))
|
|
90
|
+
* // Output: true
|
|
91
|
+
*
|
|
92
|
+
* console.log(Option.isOption(Option.none()))
|
|
93
|
+
* // Output: true
|
|
94
|
+
*
|
|
95
|
+
* console.log(Option.isOption({}))
|
|
96
|
+
* // Output: false
|
|
51
97
|
* ```
|
|
52
98
|
*
|
|
53
|
-
* @category
|
|
99
|
+
* @category Guards
|
|
54
100
|
* @since 2.0.0
|
|
55
101
|
*/
|
|
56
102
|
const isOption = exports.isOption = option.isOption;
|
|
57
103
|
/**
|
|
58
|
-
*
|
|
104
|
+
* Checks whether an `Option` represents the absence of a value (`None`).
|
|
59
105
|
*
|
|
60
|
-
* @
|
|
106
|
+
* @see {@link isSome} for the opposite check.
|
|
61
107
|
*
|
|
62
108
|
* @example
|
|
63
109
|
* ```ts
|
|
64
110
|
* import { Option } from "effect"
|
|
65
111
|
*
|
|
66
|
-
*
|
|
67
|
-
*
|
|
112
|
+
* console.log(Option.isNone(Option.some(1)))
|
|
113
|
+
* // Output: false
|
|
114
|
+
*
|
|
115
|
+
* console.log(Option.isNone(Option.none()))
|
|
116
|
+
* // Output: true
|
|
68
117
|
* ```
|
|
69
118
|
*
|
|
70
|
-
* @category
|
|
119
|
+
* @category Guards
|
|
71
120
|
* @since 2.0.0
|
|
72
121
|
*/
|
|
73
122
|
const isNone = exports.isNone = option.isNone;
|
|
74
123
|
/**
|
|
75
|
-
*
|
|
124
|
+
* Checks whether an `Option` contains a value (`Some`).
|
|
76
125
|
*
|
|
77
|
-
* @
|
|
126
|
+
* @see {@link isNone} for the opposite check.
|
|
78
127
|
*
|
|
79
128
|
* @example
|
|
80
129
|
* ```ts
|
|
81
130
|
* import { Option } from "effect"
|
|
82
131
|
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
132
|
+
* console.log(Option.isSome(Option.some(1)))
|
|
133
|
+
* // Output: true
|
|
134
|
+
*
|
|
135
|
+
* console.log(Option.isSome(Option.none()))
|
|
136
|
+
* // Output: false
|
|
85
137
|
* ```
|
|
86
138
|
*
|
|
87
|
-
* @category
|
|
139
|
+
* @category Guards
|
|
88
140
|
* @since 2.0.0
|
|
89
141
|
*/
|
|
90
142
|
const isSome = exports.isSome = option.isSome;
|
|
91
143
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
144
|
+
* Performs pattern matching on an `Option` to handle both `Some` and `None`
|
|
145
|
+
* cases.
|
|
146
|
+
*
|
|
147
|
+
* **Details**
|
|
148
|
+
*
|
|
149
|
+
* This function allows you to match against an `Option` and handle both
|
|
150
|
+
* scenarios: when the `Option` is `None` (i.e., contains no value), and when
|
|
151
|
+
* the `Option` is `Some` (i.e., contains a value). It executes one of the
|
|
152
|
+
* provided functions based on the case:
|
|
94
153
|
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
154
|
+
* - If the `Option` is `None`, the `onNone` function is executed and its result
|
|
155
|
+
* is returned.
|
|
156
|
+
* - If the `Option` is `Some`, the `onSome` function is executed with the
|
|
157
|
+
* contained value, and its result is returned.
|
|
158
|
+
*
|
|
159
|
+
* This function provides a concise and functional way to handle optional values
|
|
160
|
+
* without resorting to `if` or manual checks, making your code more declarative
|
|
161
|
+
* and readable.
|
|
98
162
|
*
|
|
99
163
|
* @example
|
|
100
164
|
* ```ts
|
|
101
|
-
*
|
|
165
|
+
* // Title: Pattern Matching with Option
|
|
166
|
+
* import { Option } from "effect"
|
|
102
167
|
*
|
|
103
|
-
*
|
|
104
|
-
* pipe(Option.some(1), Option.match({ onNone: () => 'a none', onSome: (a) => `a some containing ${a}` })),
|
|
105
|
-
* 'a some containing 1'
|
|
106
|
-
* )
|
|
168
|
+
* const foo = Option.some(1)
|
|
107
169
|
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* )
|
|
170
|
+
* const message = Option.match(foo, {
|
|
171
|
+
* onNone: () => "Option is empty",
|
|
172
|
+
* onSome: (value) => `Option has a value: ${value}`
|
|
173
|
+
* })
|
|
174
|
+
*
|
|
175
|
+
* console.log(message)
|
|
176
|
+
* // Output: "Option has a value: 1"
|
|
112
177
|
* ```
|
|
113
178
|
*
|
|
114
|
-
* @category
|
|
179
|
+
* @category Pattern matching
|
|
115
180
|
* @since 2.0.0
|
|
116
181
|
*/
|
|
117
182
|
const match = exports.match = /*#__PURE__*/(0, _Function.dual)(2, (self, {
|
|
@@ -119,41 +184,76 @@ const match = exports.match = /*#__PURE__*/(0, _Function.dual)(2, (self, {
|
|
|
119
184
|
onSome
|
|
120
185
|
}) => isNone(self) ? onNone() : onSome(self.value));
|
|
121
186
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
187
|
+
* Converts an `Option`-returning function into a type guard.
|
|
188
|
+
*
|
|
189
|
+
* **Details**
|
|
190
|
+
*
|
|
191
|
+
* This function transforms a function that returns an `Option` into a type
|
|
192
|
+
* guard, ensuring type safety when validating or narrowing types. The returned
|
|
193
|
+
* type guard function checks whether the input satisfies the condition defined
|
|
194
|
+
* in the original `Option`-returning function.
|
|
195
|
+
*
|
|
196
|
+
* If the original function returns `Option.some`, the type guard evaluates to
|
|
197
|
+
* `true`, confirming the input is of the desired type. If the function returns
|
|
198
|
+
* `Option.none`, the type guard evaluates to `false`.
|
|
199
|
+
*
|
|
200
|
+
* This utility is especially useful for validating types in union types,
|
|
201
|
+
* filtering arrays, or ensuring safe handling of specific subtypes.
|
|
124
202
|
*
|
|
125
203
|
* @example
|
|
126
204
|
* ```ts
|
|
127
205
|
* import { Option } from "effect"
|
|
128
206
|
*
|
|
129
|
-
*
|
|
130
|
-
*
|
|
207
|
+
* type MyData = string | number
|
|
208
|
+
*
|
|
209
|
+
* const parseString = (data: MyData): Option.Option<string> =>
|
|
210
|
+
* typeof data === "string" ? Option.some(data) : Option.none()
|
|
131
211
|
*
|
|
132
|
-
*
|
|
212
|
+
* // ┌─── (a: MyData) => a is string
|
|
213
|
+
* // ▼
|
|
214
|
+
* const isString = Option.toRefinement(parseString)
|
|
133
215
|
*
|
|
134
|
-
*
|
|
135
|
-
*
|
|
216
|
+
* console.log(isString("a"))
|
|
217
|
+
* // Output: true
|
|
218
|
+
*
|
|
219
|
+
* console.log(isString(1))
|
|
220
|
+
* // Output: false
|
|
136
221
|
* ```
|
|
137
222
|
*
|
|
138
|
-
* @category
|
|
223
|
+
* @category Conversions
|
|
139
224
|
* @since 2.0.0
|
|
140
225
|
*/
|
|
141
226
|
const toRefinement = f => a => isSome(f(a));
|
|
142
227
|
/**
|
|
143
|
-
* Converts an `Iterable`
|
|
144
|
-
*
|
|
228
|
+
* Converts an `Iterable` into an `Option`, wrapping the first element if it
|
|
229
|
+
* exists.
|
|
230
|
+
*
|
|
231
|
+
* **Details**
|
|
232
|
+
*
|
|
233
|
+
* This function takes an `Iterable` (e.g., an array, a generator, or any object
|
|
234
|
+
* implementing the `Iterable` interface) and returns an `Option` based on its
|
|
235
|
+
* content:
|
|
145
236
|
*
|
|
146
|
-
*
|
|
237
|
+
* - If the `Iterable` contains at least one element, the first element is
|
|
238
|
+
* wrapped in a `Some` and returned.
|
|
239
|
+
* - If the `Iterable` is empty, `None` is returned, representing the absence of
|
|
240
|
+
* a value.
|
|
241
|
+
*
|
|
242
|
+
* This utility is useful for safely handling collections that might be empty,
|
|
243
|
+
* ensuring you explicitly handle both cases where a value exists or doesn't.
|
|
147
244
|
*
|
|
148
245
|
* @example
|
|
149
246
|
* ```ts
|
|
150
247
|
* import { Option } from "effect"
|
|
151
248
|
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
249
|
+
* console.log(Option.fromIterable([1, 2, 3]))
|
|
250
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
251
|
+
*
|
|
252
|
+
* console.log(Option.fromIterable([]))
|
|
253
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
154
254
|
* ```
|
|
155
255
|
*
|
|
156
|
-
* @category
|
|
256
|
+
* @category Constructors
|
|
157
257
|
* @since 2.0.0
|
|
158
258
|
*/
|
|
159
259
|
exports.toRefinement = toRefinement;
|
|
@@ -164,154 +264,227 @@ const fromIterable = collection => {
|
|
|
164
264
|
return none();
|
|
165
265
|
};
|
|
166
266
|
/**
|
|
167
|
-
* Converts
|
|
267
|
+
* Converts an `Either` into an `Option` by discarding the error and extracting
|
|
268
|
+
* the right value.
|
|
269
|
+
*
|
|
270
|
+
* **Details**
|
|
271
|
+
*
|
|
272
|
+
* This function takes an `Either` and returns an `Option` based on its value:
|
|
273
|
+
*
|
|
274
|
+
* - If the `Either` is a `Right`, its value is wrapped in a `Some` and
|
|
275
|
+
* returned.
|
|
276
|
+
* - If the `Either` is a `Left`, the error is discarded, and `None` is
|
|
277
|
+
* returned.
|
|
278
|
+
*
|
|
279
|
+
* This is particularly useful when you only care about the success case
|
|
280
|
+
* (`Right`) of an `Either` and want to handle the result using `Option`. By
|
|
281
|
+
* using this function, you can convert `Either` into a simpler structure for
|
|
282
|
+
* cases where error handling is not required.
|
|
283
|
+
*
|
|
284
|
+
* @see {@link getLeft} for the opposite operation.
|
|
168
285
|
*
|
|
169
286
|
* @example
|
|
170
287
|
* ```ts
|
|
171
|
-
* import {
|
|
288
|
+
* import { Either, Option } from "effect"
|
|
289
|
+
*
|
|
290
|
+
* console.log(Option.getRight(Either.right("ok")))
|
|
291
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'ok' }
|
|
172
292
|
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
293
|
+
* console.log(Option.getRight(Either.left("err")))
|
|
294
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
175
295
|
* ```
|
|
176
296
|
*
|
|
177
|
-
* @category
|
|
297
|
+
* @category Conversions
|
|
178
298
|
* @since 2.0.0
|
|
179
299
|
*/
|
|
180
300
|
exports.fromIterable = fromIterable;
|
|
181
301
|
const getRight = exports.getRight = either.getRight;
|
|
182
302
|
/**
|
|
183
|
-
* Converts
|
|
303
|
+
* Converts an `Either` into an `Option` by discarding the right value and
|
|
304
|
+
* extracting the left value.
|
|
305
|
+
*
|
|
306
|
+
* **Details**
|
|
307
|
+
*
|
|
308
|
+
* This function transforms an `Either` into an `Option` as follows:
|
|
309
|
+
*
|
|
310
|
+
* - If the `Either` is a `Left`, its value is wrapped in a `Some` and returned.
|
|
311
|
+
* - If the `Either` is a `Right`, the value is discarded, and `None` is
|
|
312
|
+
* returned.
|
|
313
|
+
*
|
|
314
|
+
* This utility is useful when you only care about the error case (`Left`) of an
|
|
315
|
+
* `Either` and want to handle it as an `Option`. By discarding the right value,
|
|
316
|
+
* it simplifies error-focused workflows.
|
|
317
|
+
*
|
|
318
|
+
* @see {@link getRight} for the opposite operation.
|
|
184
319
|
*
|
|
185
320
|
* @example
|
|
186
321
|
* ```ts
|
|
187
|
-
* import {
|
|
322
|
+
* import { Either, Option } from "effect"
|
|
188
323
|
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
324
|
+
* console.log(Option.getLeft(Either.right("ok")))
|
|
325
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
326
|
+
*
|
|
327
|
+
* console.log(Option.getLeft(Either.left("err")))
|
|
328
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'err' }
|
|
191
329
|
* ```
|
|
192
330
|
*
|
|
193
|
-
* @category
|
|
331
|
+
* @category Conversions
|
|
194
332
|
* @since 2.0.0
|
|
195
333
|
*/
|
|
196
334
|
const getLeft = exports.getLeft = either.getLeft;
|
|
197
335
|
/**
|
|
198
|
-
* Returns the value
|
|
336
|
+
* Returns the value contained in the `Option` if it is `Some`, otherwise
|
|
337
|
+
* evaluates and returns the result of `onNone`.
|
|
338
|
+
*
|
|
339
|
+
* **Details**
|
|
199
340
|
*
|
|
200
|
-
*
|
|
201
|
-
*
|
|
341
|
+
* This function allows you to provide a fallback value or computation for when
|
|
342
|
+
* an `Option` is `None`. If the `Option` contains a value (`Some`), that value
|
|
343
|
+
* is returned. If it is empty (`None`), the `onNone` function is executed, and
|
|
344
|
+
* its result is returned instead.
|
|
345
|
+
*
|
|
346
|
+
* This utility is helpful for safely handling `Option` values by ensuring you
|
|
347
|
+
* always receive a meaningful result, whether or not the `Option` contains a
|
|
348
|
+
* value. It is particularly useful for providing default values or alternative
|
|
349
|
+
* logic when working with optional values.
|
|
350
|
+
*
|
|
351
|
+
* @see {@link getOrNull} for a version that returns `null` instead of executing a function.
|
|
352
|
+
* @see {@link getOrUndefined} for a version that returns `undefined` instead of executing a function.
|
|
202
353
|
*
|
|
203
354
|
* @example
|
|
204
355
|
* ```ts
|
|
205
|
-
* import {
|
|
356
|
+
* import { Option } from "effect"
|
|
206
357
|
*
|
|
207
|
-
*
|
|
208
|
-
*
|
|
358
|
+
* console.log(Option.some(1).pipe(Option.getOrElse(() => 0)))
|
|
359
|
+
* // Output: 1
|
|
360
|
+
*
|
|
361
|
+
* console.log(Option.none().pipe(Option.getOrElse(() => 0)))
|
|
362
|
+
* // Output: 0
|
|
209
363
|
* ```
|
|
210
364
|
*
|
|
211
|
-
* @category
|
|
365
|
+
* @category Getters
|
|
212
366
|
* @since 2.0.0
|
|
213
367
|
*/
|
|
214
368
|
const getOrElse = exports.getOrElse = /*#__PURE__*/(0, _Function.dual)(2, (self, onNone) => isNone(self) ? onNone() : self.value);
|
|
215
369
|
/**
|
|
216
|
-
* Returns the provided `Option` `that` if
|
|
370
|
+
* Returns the provided `Option` `that` if the current `Option` (`self`) is
|
|
371
|
+
* `None`; otherwise, it returns `self`.
|
|
372
|
+
*
|
|
373
|
+
* **Details**
|
|
217
374
|
*
|
|
218
|
-
*
|
|
219
|
-
*
|
|
375
|
+
* This function provides a fallback mechanism for `Option` values. If the
|
|
376
|
+
* current `Option` is `None` (i.e., it contains no value), the `that` function
|
|
377
|
+
* is evaluated, and its resulting `Option` is returned. If the current `Option`
|
|
378
|
+
* is `Some` (i.e., it contains a value), the original `Option` is returned
|
|
379
|
+
* unchanged.
|
|
380
|
+
*
|
|
381
|
+
* This is particularly useful for chaining fallback values or computations,
|
|
382
|
+
* allowing you to provide alternative `Option` values when the first one is
|
|
383
|
+
* empty.
|
|
220
384
|
*
|
|
221
385
|
* @example
|
|
222
386
|
* ```ts
|
|
223
|
-
* import {
|
|
387
|
+
* import { Option } from "effect"
|
|
224
388
|
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
* )
|
|
232
|
-
*
|
|
233
|
-
*
|
|
234
|
-
*
|
|
235
|
-
*
|
|
236
|
-
* ),
|
|
237
|
-
* Option.some('a')
|
|
238
|
-
* )
|
|
239
|
-
* assert.deepStrictEqual(
|
|
240
|
-
* pipe(
|
|
241
|
-
* Option.none(),
|
|
242
|
-
* Option.orElse(() => Option.some('b'))
|
|
243
|
-
* ),
|
|
244
|
-
* Option.some('b')
|
|
245
|
-
* )
|
|
246
|
-
* assert.deepStrictEqual(
|
|
247
|
-
* pipe(
|
|
248
|
-
* Option.some('a'),
|
|
249
|
-
* Option.orElse(() => Option.some('b'))
|
|
250
|
-
* ),
|
|
251
|
-
* Option.some('a')
|
|
252
|
-
* )
|
|
389
|
+
* console.log(Option.none().pipe(Option.orElse(() => Option.none())))
|
|
390
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
391
|
+
*
|
|
392
|
+
* console.log(Option.some("a").pipe(Option.orElse(() => Option.none())))
|
|
393
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
394
|
+
*
|
|
395
|
+
* console.log(Option.none().pipe(Option.orElse(() => Option.some("b"))))
|
|
396
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'b' }
|
|
397
|
+
*
|
|
398
|
+
* console.log(Option.some("a").pipe(Option.orElse(() => Option.some("b"))))
|
|
399
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
253
400
|
* ```
|
|
254
401
|
*
|
|
255
|
-
* @category
|
|
402
|
+
* @category Error handling
|
|
256
403
|
* @since 2.0.0
|
|
257
404
|
*/
|
|
258
405
|
const orElse = exports.orElse = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => isNone(self) ? that() : self);
|
|
259
406
|
/**
|
|
260
|
-
* Returns the provided default value
|
|
407
|
+
* Returns the provided default value wrapped in `Some` if the current `Option`
|
|
408
|
+
* (`self`) is `None`; otherwise, returns `self`.
|
|
409
|
+
*
|
|
410
|
+
* **Details**
|
|
411
|
+
*
|
|
412
|
+
* This function provides a way to supply a default value for cases where an
|
|
413
|
+
* `Option` is `None`. If the current `Option` is empty (`None`), the `onNone`
|
|
414
|
+
* function is executed to compute the default value, which is then wrapped in a
|
|
415
|
+
* `Some`. If the current `Option` contains a value (`Some`), it is returned as
|
|
416
|
+
* is.
|
|
261
417
|
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
418
|
+
* This is particularly useful for handling optional values where a fallback
|
|
419
|
+
* default needs to be provided explicitly in case of absence.
|
|
264
420
|
*
|
|
265
421
|
* @example
|
|
266
422
|
* ```ts
|
|
267
|
-
* import {
|
|
423
|
+
* import { Option } from "effect"
|
|
268
424
|
*
|
|
269
|
-
*
|
|
270
|
-
*
|
|
271
|
-
*
|
|
272
|
-
*
|
|
273
|
-
*
|
|
274
|
-
* Option.some('b')
|
|
275
|
-
* )
|
|
276
|
-
* assert.deepStrictEqual(
|
|
277
|
-
* pipe(
|
|
278
|
-
* Option.some('a'),
|
|
279
|
-
* Option.orElseSome(() => 'b')
|
|
280
|
-
* ),
|
|
281
|
-
* Option.some('a')
|
|
282
|
-
* )
|
|
425
|
+
* console.log(Option.none().pipe(Option.orElseSome(() => "b")))
|
|
426
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'b' }
|
|
427
|
+
*
|
|
428
|
+
* console.log(Option.some("a").pipe(Option.orElseSome(() => "b")))
|
|
429
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
283
430
|
* ```
|
|
284
431
|
*
|
|
285
|
-
* @category
|
|
432
|
+
* @category Error handling
|
|
286
433
|
* @since 2.0.0
|
|
287
434
|
*/
|
|
288
435
|
const orElseSome = exports.orElseSome = /*#__PURE__*/(0, _Function.dual)(2, (self, onNone) => isNone(self) ? some(onNone()) : self);
|
|
289
436
|
/**
|
|
290
|
-
* Similar to
|
|
291
|
-
*
|
|
437
|
+
* Similar to {@link orElse}, but returns an `Either` wrapped in an `Option` to
|
|
438
|
+
* indicate the source of the value.
|
|
292
439
|
*
|
|
293
|
-
*
|
|
440
|
+
* **Details**
|
|
294
441
|
*
|
|
295
|
-
*
|
|
296
|
-
*
|
|
442
|
+
* This function allows you to provide a fallback `Option` in case the current
|
|
443
|
+
* `Option` (`self`) is `None`. However, unlike `orElse`, it returns the value
|
|
444
|
+
* wrapped in an `Either` object, providing additional information about where
|
|
445
|
+
* the value came from:
|
|
297
446
|
*
|
|
298
|
-
*
|
|
447
|
+
* - If the value is from the fallback `Option` (`that`), it is wrapped in an
|
|
448
|
+
* `Either.right`.
|
|
449
|
+
* - If the value is from the original `Option` (`self`), it is wrapped in an
|
|
450
|
+
* `Either.left`.
|
|
451
|
+
*
|
|
452
|
+
* This is especially useful when you need to differentiate between values
|
|
453
|
+
* originating from the primary `Option` and those coming from the fallback,
|
|
454
|
+
* while still maintaining the `Option`-style handling.
|
|
455
|
+
*
|
|
456
|
+
* @category Error handling
|
|
299
457
|
* @since 2.0.0
|
|
300
458
|
*/
|
|
301
459
|
const orElseEither = exports.orElseEither = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => isNone(self) ? map(that(), either.right) : map(self, either.left));
|
|
302
460
|
/**
|
|
303
|
-
*
|
|
461
|
+
* Returns the first `Some` value found in an `Iterable` collection of
|
|
462
|
+
* `Option`s, or `None` if no `Some` is found.
|
|
463
|
+
*
|
|
464
|
+
* **Details**
|
|
304
465
|
*
|
|
305
|
-
*
|
|
466
|
+
* This function iterates over a collection of `Option` values and returns the
|
|
467
|
+
* first `Some` it encounters. If the collection contains only `None` values,
|
|
468
|
+
* the result will also be `None`. This utility is useful for efficiently
|
|
469
|
+
* finding the first valid value in a sequence of potentially empty or invalid
|
|
470
|
+
* options.
|
|
471
|
+
*
|
|
472
|
+
* The iteration stops as soon as a `Some` is found, making this function
|
|
473
|
+
* efficient for large collections.
|
|
306
474
|
*
|
|
307
475
|
* @example
|
|
308
476
|
* ```ts
|
|
309
477
|
* import { Option } from "effect"
|
|
310
478
|
*
|
|
311
|
-
*
|
|
479
|
+
* console.log(Option.firstSomeOf([
|
|
480
|
+
* Option.none(),
|
|
481
|
+
* Option.some(1),
|
|
482
|
+
* Option.some(2)
|
|
483
|
+
* ]))
|
|
484
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
312
485
|
* ```
|
|
313
486
|
*
|
|
314
|
-
* @category
|
|
487
|
+
* @category Error handling
|
|
315
488
|
* @since 2.0.0
|
|
316
489
|
*/
|
|
317
490
|
const firstSomeOf = collection => {
|
|
@@ -324,27 +497,39 @@ const firstSomeOf = collection => {
|
|
|
324
497
|
return out;
|
|
325
498
|
};
|
|
326
499
|
/**
|
|
327
|
-
*
|
|
328
|
-
*
|
|
329
|
-
*
|
|
330
|
-
* @param nullableValue - The nullable value to be converted to an `Option`.
|
|
500
|
+
* Converts a nullable value into an `Option`. Returns `None` if the value is
|
|
501
|
+
* `null` or `undefined`, otherwise wraps the value in a `Some`.
|
|
331
502
|
*
|
|
332
503
|
* @example
|
|
333
504
|
* ```ts
|
|
334
505
|
* import { Option } from "effect"
|
|
335
506
|
*
|
|
336
|
-
*
|
|
337
|
-
*
|
|
338
|
-
*
|
|
507
|
+
* console.log(Option.fromNullable(undefined))
|
|
508
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
509
|
+
*
|
|
510
|
+
* console.log(Option.fromNullable(null))
|
|
511
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
512
|
+
*
|
|
513
|
+
* console.log(Option.fromNullable(1))
|
|
514
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
339
515
|
* ```
|
|
340
516
|
*
|
|
341
|
-
* @category
|
|
517
|
+
* @category Conversions
|
|
342
518
|
* @since 2.0.0
|
|
343
519
|
*/
|
|
344
520
|
exports.firstSomeOf = firstSomeOf;
|
|
345
521
|
const fromNullable = nullableValue => nullableValue == null ? none() : some(nullableValue);
|
|
346
522
|
/**
|
|
347
|
-
*
|
|
523
|
+
* Lifts a function that returns `null` or `undefined` into the `Option`
|
|
524
|
+
* context.
|
|
525
|
+
*
|
|
526
|
+
* **Details**
|
|
527
|
+
*
|
|
528
|
+
* This function takes a function `f` that might return `null` or `undefined`
|
|
529
|
+
* and transforms it into a function that returns an `Option`. The resulting
|
|
530
|
+
* function will return:
|
|
531
|
+
* - `Some` if the original function produces a non-null, non-undefined value.
|
|
532
|
+
* - `None` if the original function produces `null` or `undefined`.
|
|
348
533
|
*
|
|
349
534
|
* @example
|
|
350
535
|
* ```ts
|
|
@@ -357,57 +542,86 @@ const fromNullable = nullableValue => nullableValue == null ? none() : some(null
|
|
|
357
542
|
*
|
|
358
543
|
* const parseOption = Option.liftNullable(parse)
|
|
359
544
|
*
|
|
360
|
-
*
|
|
361
|
-
*
|
|
545
|
+
* console.log(parseOption("1"))
|
|
546
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
547
|
+
*
|
|
548
|
+
* console.log(parseOption("not a number"))
|
|
549
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
362
550
|
* ```
|
|
363
551
|
*
|
|
364
|
-
* @category
|
|
552
|
+
* @category Conversions
|
|
365
553
|
* @since 2.0.0
|
|
366
554
|
*/
|
|
367
555
|
exports.fromNullable = fromNullable;
|
|
368
556
|
const liftNullable = f => (...a) => fromNullable(f(...a));
|
|
369
557
|
/**
|
|
370
|
-
* Returns the value
|
|
558
|
+
* Returns the value contained in the `Option` if it is `Some`; otherwise,
|
|
559
|
+
* returns `null`.
|
|
560
|
+
*
|
|
561
|
+
* **Details**
|
|
562
|
+
*
|
|
563
|
+
* This function provides a way to extract the value of an `Option` while
|
|
564
|
+
* falling back to `null` if the `Option` is `None`.
|
|
371
565
|
*
|
|
372
|
-
*
|
|
566
|
+
* It is particularly useful in scenarios where `null` is an acceptable
|
|
567
|
+
* placeholder for the absence of a value, such as when interacting with APIs or
|
|
568
|
+
* systems that use `null` as a default for missing values.
|
|
373
569
|
*
|
|
374
570
|
* @example
|
|
375
571
|
* ```ts
|
|
376
572
|
* import { Option } from "effect"
|
|
377
573
|
*
|
|
378
|
-
*
|
|
379
|
-
*
|
|
574
|
+
* console.log(Option.getOrNull(Option.some(1)))
|
|
575
|
+
* // Output: 1
|
|
576
|
+
*
|
|
577
|
+
* console.log(Option.getOrNull(Option.none()))
|
|
578
|
+
* // Output: null
|
|
380
579
|
* ```
|
|
381
580
|
*
|
|
382
|
-
* @category
|
|
581
|
+
* @category Getters
|
|
383
582
|
* @since 2.0.0
|
|
384
583
|
*/
|
|
385
584
|
exports.liftNullable = liftNullable;
|
|
386
585
|
const getOrNull = exports.getOrNull = /*#__PURE__*/getOrElse(_Function.constNull);
|
|
387
586
|
/**
|
|
388
|
-
* Returns the value
|
|
587
|
+
* Returns the value contained in the `Option` if it is `Some`; otherwise,
|
|
588
|
+
* returns `undefined`.
|
|
589
|
+
*
|
|
590
|
+
* **Details**
|
|
389
591
|
*
|
|
390
|
-
*
|
|
592
|
+
* This function provides a way to extract the value of an `Option` while
|
|
593
|
+
* falling back to `undefined` if the `Option` is `None`.
|
|
594
|
+
*
|
|
595
|
+
* It is particularly useful in scenarios where `undefined` is an acceptable
|
|
596
|
+
* placeholder for the absence of a value, such as when interacting with APIs or
|
|
597
|
+
* systems that use `undefined` as a default for missing values.
|
|
391
598
|
*
|
|
392
599
|
* @example
|
|
393
600
|
* ```ts
|
|
394
601
|
* import { Option } from "effect"
|
|
395
602
|
*
|
|
396
|
-
*
|
|
397
|
-
*
|
|
603
|
+
* console.log(Option.getOrUndefined(Option.some(1)))
|
|
604
|
+
* // Output: 1
|
|
605
|
+
*
|
|
606
|
+
* console.log(Option.getOrUndefined(Option.none()))
|
|
607
|
+
* // Output: undefined
|
|
398
608
|
* ```
|
|
399
609
|
*
|
|
400
|
-
* @category
|
|
610
|
+
* @category Getters
|
|
401
611
|
* @since 2.0.0
|
|
402
612
|
*/
|
|
403
613
|
const getOrUndefined = exports.getOrUndefined = /*#__PURE__*/getOrElse(_Function.constUndefined);
|
|
404
614
|
/**
|
|
405
|
-
*
|
|
615
|
+
* Lifts a function that throws exceptions into a function that returns an
|
|
616
|
+
* `Option`.
|
|
406
617
|
*
|
|
407
|
-
*
|
|
408
|
-
* the exception in a more functional way.
|
|
618
|
+
* **Details**
|
|
409
619
|
*
|
|
410
|
-
*
|
|
620
|
+
* This utility function takes a function `f` that might throw an exception and
|
|
621
|
+
* transforms it into a safer function that returns an `Option`. If the original
|
|
622
|
+
* function executes successfully, the result is wrapped in a `Some`. If an
|
|
623
|
+
* exception is thrown, the result is `None`, allowing the developer to handle
|
|
624
|
+
* errors in a functional, type-safe way.
|
|
411
625
|
*
|
|
412
626
|
* @example
|
|
413
627
|
* ```ts
|
|
@@ -415,11 +629,14 @@ const getOrUndefined = exports.getOrUndefined = /*#__PURE__*/getOrElse(_Function
|
|
|
415
629
|
*
|
|
416
630
|
* const parse = Option.liftThrowable(JSON.parse)
|
|
417
631
|
*
|
|
418
|
-
*
|
|
419
|
-
*
|
|
632
|
+
* console.log(parse("1"))
|
|
633
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
634
|
+
*
|
|
635
|
+
* console.log(parse(""))
|
|
636
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
420
637
|
* ```
|
|
421
638
|
*
|
|
422
|
-
* @category
|
|
639
|
+
* @category Conversions
|
|
423
640
|
* @since 2.0.0
|
|
424
641
|
*/
|
|
425
642
|
const liftThrowable = f => (...a) => {
|
|
@@ -430,12 +647,18 @@ const liftThrowable = f => (...a) => {
|
|
|
430
647
|
}
|
|
431
648
|
};
|
|
432
649
|
/**
|
|
433
|
-
* Extracts the value of an `Option` or throws if the `Option` is
|
|
650
|
+
* Extracts the value of an `Option` or throws an error if the `Option` is
|
|
651
|
+
* `None`, using a custom error factory.
|
|
652
|
+
*
|
|
653
|
+
* **Details**
|
|
434
654
|
*
|
|
435
|
-
*
|
|
655
|
+
* This function allows you to extract the value of an `Option` when it is
|
|
656
|
+
* `Some`. If the `Option` is `None`, it throws an error generated by the
|
|
657
|
+
* provided `onNone` function. This utility is particularly useful when you need
|
|
658
|
+
* a fail-fast behavior for empty `Option` values and want to provide a custom
|
|
659
|
+
* error message or object.
|
|
436
660
|
*
|
|
437
|
-
* @
|
|
438
|
-
* @param onNone - A function that will be called if the `Option` is `None`. It returns the error to be thrown.
|
|
661
|
+
* @see {@link getOrThrow} for a version that throws a default error.
|
|
439
662
|
*
|
|
440
663
|
* @example
|
|
441
664
|
* ```ts
|
|
@@ -448,7 +671,7 @@ const liftThrowable = f => (...a) => {
|
|
|
448
671
|
* assert.throws(() => Option.getOrThrowWith(Option.none(), () => new Error('Unexpected None')))
|
|
449
672
|
* ```
|
|
450
673
|
*
|
|
451
|
-
* @category
|
|
674
|
+
* @category Conversions
|
|
452
675
|
* @since 2.0.0
|
|
453
676
|
*/
|
|
454
677
|
exports.liftThrowable = liftThrowable;
|
|
@@ -459,12 +682,17 @@ const getOrThrowWith = exports.getOrThrowWith = /*#__PURE__*/(0, _Function.dual)
|
|
|
459
682
|
throw onNone();
|
|
460
683
|
});
|
|
461
684
|
/**
|
|
462
|
-
* Extracts the value of an `Option` or throws if the `Option`
|
|
685
|
+
* Extracts the value of an `Option` or throws a default error if the `Option`
|
|
686
|
+
* is `None`.
|
|
463
687
|
*
|
|
464
|
-
*
|
|
688
|
+
* **Details**
|
|
465
689
|
*
|
|
466
|
-
*
|
|
467
|
-
*
|
|
690
|
+
* This function extracts the value from an `Option` if it is `Some`. If the
|
|
691
|
+
* `Option` is `None`, it throws a default error. It is useful for fail-fast
|
|
692
|
+
* scenarios where the absence of a value is treated as an exceptional case and
|
|
693
|
+
* a default error is sufficient.
|
|
694
|
+
*
|
|
695
|
+
* @see {@link getOrThrowWith} for a version that allows you to provide a custom error.
|
|
468
696
|
*
|
|
469
697
|
* @example
|
|
470
698
|
* ```ts
|
|
@@ -474,48 +702,168 @@ const getOrThrowWith = exports.getOrThrowWith = /*#__PURE__*/(0, _Function.dual)
|
|
|
474
702
|
* assert.throws(() => Option.getOrThrow(Option.none()))
|
|
475
703
|
* ```
|
|
476
704
|
*
|
|
477
|
-
* @category
|
|
705
|
+
* @category Conversions
|
|
478
706
|
* @since 2.0.0
|
|
479
707
|
*/
|
|
480
708
|
const getOrThrow = exports.getOrThrow = /*#__PURE__*/getOrThrowWith(() => new Error("getOrThrow called on a None"));
|
|
481
709
|
/**
|
|
482
|
-
*
|
|
710
|
+
* Transforms the value inside a `Some` to a new value using the provided
|
|
711
|
+
* function, while leaving `None` unchanged.
|
|
712
|
+
*
|
|
713
|
+
* **Details**
|
|
714
|
+
*
|
|
715
|
+
* This function applies a mapping function `f` to the value inside an `Option`
|
|
716
|
+
* if it is a `Some`. If the `Option` is `None`, it remains unchanged. The
|
|
717
|
+
* result is a new `Option` with the transformed value (if it was a `Some`) or
|
|
718
|
+
* still `None`.
|
|
719
|
+
*
|
|
720
|
+
* This utility is particularly useful for chaining transformations in a
|
|
721
|
+
* functional way without needing to manually handle `None` cases.
|
|
722
|
+
*
|
|
723
|
+
* @example
|
|
724
|
+
* ```ts
|
|
725
|
+
* import { Option } from "effect"
|
|
483
726
|
*
|
|
484
|
-
*
|
|
485
|
-
*
|
|
727
|
+
* // Mapping over a `Some`
|
|
728
|
+
* const someValue = Option.some(2)
|
|
486
729
|
*
|
|
487
|
-
*
|
|
730
|
+
* console.log(Option.map(someValue, (n) => n * 2))
|
|
731
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 4 }
|
|
732
|
+
*
|
|
733
|
+
* // Mapping over a `None`
|
|
734
|
+
* const noneValue = Option.none<number>()
|
|
735
|
+
*
|
|
736
|
+
* console.log(Option.map(noneValue, (n) => n * 2))
|
|
737
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
738
|
+
* ```
|
|
739
|
+
*
|
|
740
|
+
* @category Mapping
|
|
488
741
|
* @since 2.0.0
|
|
489
742
|
*/
|
|
490
743
|
const map = exports.map = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => isNone(self) ? none() : some(f(self.value)));
|
|
491
744
|
/**
|
|
492
|
-
*
|
|
745
|
+
* Replaces the value inside a `Some` with the specified constant value, leaving
|
|
746
|
+
* `None` unchanged.
|
|
747
|
+
*
|
|
748
|
+
* **Details**
|
|
493
749
|
*
|
|
494
|
-
*
|
|
750
|
+
* This function transforms an `Option` by replacing the value inside a `Some`
|
|
751
|
+
* with the given constant value `b`. If the `Option` is `None`, it remains
|
|
752
|
+
* unchanged.
|
|
753
|
+
*
|
|
754
|
+
* This is useful when you want to preserve the presence of a value (`Some`) but
|
|
755
|
+
* replace its content with a fixed value.
|
|
756
|
+
*
|
|
757
|
+
* @example
|
|
758
|
+
* ```ts
|
|
759
|
+
* import { Option } from "effect"
|
|
760
|
+
*
|
|
761
|
+
* // Replacing the value of a `Some`
|
|
762
|
+
* const someValue = Option.some(42)
|
|
763
|
+
*
|
|
764
|
+
* console.log(Option.as(someValue, "new value"))
|
|
765
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'new value' }
|
|
766
|
+
*
|
|
767
|
+
* // Replacing a `None` (no effect)
|
|
768
|
+
* const noneValue = Option.none<number>()
|
|
769
|
+
*
|
|
770
|
+
* console.log(Option.as(noneValue, "new value"))
|
|
771
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
772
|
+
* ```
|
|
773
|
+
*
|
|
774
|
+
* @category Mapping
|
|
495
775
|
* @since 2.0.0
|
|
496
776
|
*/
|
|
497
777
|
const as = exports.as = /*#__PURE__*/(0, _Function.dual)(2, (self, b) => map(self, () => b));
|
|
498
778
|
/**
|
|
499
|
-
*
|
|
779
|
+
* Replaces the value inside a `Some` with the constant value `void`, leaving
|
|
780
|
+
* `None` unchanged.
|
|
781
|
+
*
|
|
782
|
+
* **Details**
|
|
500
783
|
*
|
|
501
|
-
* This
|
|
784
|
+
* This function transforms an `Option` by replacing the value inside a `Some`
|
|
785
|
+
* with `void`. If the `Option` is `None`, it remains unchanged.
|
|
502
786
|
*
|
|
503
|
-
*
|
|
787
|
+
* This is particularly useful in scenarios where the presence or absence of a
|
|
788
|
+
* value is significant, but the actual content of the value is irrelevant.
|
|
789
|
+
*
|
|
790
|
+
* @category Mapping
|
|
504
791
|
* @since 2.0.0
|
|
505
792
|
*/
|
|
506
793
|
const asVoid = exports.asVoid = /*#__PURE__*/as(undefined);
|
|
507
794
|
const void_ = exports.void = /*#__PURE__*/some(undefined);
|
|
508
795
|
/**
|
|
509
|
-
* Applies a function to the value of
|
|
796
|
+
* Applies a function to the value of a `Some` and flattens the resulting
|
|
797
|
+
* `Option`. If the input is `None`, it remains `None`.
|
|
798
|
+
*
|
|
799
|
+
* **Details**
|
|
800
|
+
*
|
|
801
|
+
* This function allows you to chain computations that return `Option` values.
|
|
802
|
+
* If the input `Option` is `Some`, the provided function `f` is applied to the
|
|
803
|
+
* contained value, and the resulting `Option` is returned. If the input is
|
|
804
|
+
* `None`, the function is not applied, and the result remains `None`.
|
|
805
|
+
*
|
|
806
|
+
* This utility is particularly useful for sequencing operations that may fail
|
|
807
|
+
* or produce optional results, enabling clean and concise workflows for
|
|
808
|
+
* handling such cases.
|
|
809
|
+
*
|
|
810
|
+
* @example
|
|
811
|
+
* ```ts
|
|
812
|
+
* import { Option } from "effect"
|
|
813
|
+
*
|
|
814
|
+
* interface Address {
|
|
815
|
+
* readonly city: string
|
|
816
|
+
* readonly street: Option.Option<string>
|
|
817
|
+
* }
|
|
818
|
+
*
|
|
819
|
+
* interface User {
|
|
820
|
+
* readonly id: number
|
|
821
|
+
* readonly username: string
|
|
822
|
+
* readonly email: Option.Option<string>
|
|
823
|
+
* readonly address: Option.Option<Address>
|
|
824
|
+
* }
|
|
825
|
+
*
|
|
826
|
+
* const user: User = {
|
|
827
|
+
* id: 1,
|
|
828
|
+
* username: "john_doe",
|
|
829
|
+
* email: Option.some("john.doe@example.com"),
|
|
830
|
+
* address: Option.some({
|
|
831
|
+
* city: "New York",
|
|
832
|
+
* street: Option.some("123 Main St")
|
|
833
|
+
* })
|
|
834
|
+
* }
|
|
835
|
+
*
|
|
836
|
+
* // Use flatMap to extract the street value
|
|
837
|
+
* const street = user.address.pipe(
|
|
838
|
+
* Option.flatMap((address) => address.street)
|
|
839
|
+
* )
|
|
840
|
+
*
|
|
841
|
+
* console.log(street)
|
|
842
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: '123 Main St' }
|
|
843
|
+
* ```
|
|
510
844
|
*
|
|
511
|
-
* @category
|
|
845
|
+
* @category Sequencing
|
|
512
846
|
* @since 2.0.0
|
|
513
847
|
*/
|
|
514
848
|
const flatMap = exports.flatMap = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => isNone(self) ? none() : f(self.value));
|
|
515
849
|
/**
|
|
516
|
-
*
|
|
850
|
+
* Chains two `Option`s together. The second `Option` can either be a static
|
|
851
|
+
* value or depend on the result of the first `Option`.
|
|
852
|
+
*
|
|
853
|
+
* **Details**
|
|
854
|
+
*
|
|
855
|
+
* This function enables sequencing of two `Option` computations. If the first
|
|
856
|
+
* `Option` is `Some`, the second `Option` is evaluated. The second `Option` can
|
|
857
|
+
* either:
|
|
858
|
+
*
|
|
859
|
+
* - Be a static `Option` value.
|
|
860
|
+
* - Be a function that produces an `Option`, optionally based on the value of
|
|
861
|
+
* the first `Option`.
|
|
862
|
+
*
|
|
863
|
+
* If the first `Option` is `None`, the function skips the evaluation of the
|
|
864
|
+
* second `Option` and directly returns `None`.
|
|
517
865
|
*
|
|
518
|
-
* @category
|
|
866
|
+
* @category Sequencing
|
|
519
867
|
* @since 2.0.0
|
|
520
868
|
*/
|
|
521
869
|
const andThen = exports.andThen = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => flatMap(self, a => {
|
|
@@ -523,11 +871,24 @@ const andThen = exports.andThen = /*#__PURE__*/(0, _Function.dual)(2, (self, f)
|
|
|
523
871
|
return isOption(b) ? b : some(b);
|
|
524
872
|
}));
|
|
525
873
|
/**
|
|
526
|
-
*
|
|
874
|
+
* Combines `flatMap` and `fromNullable`, transforming the value inside a `Some`
|
|
875
|
+
* using a function that may return `null` or `undefined`.
|
|
876
|
+
*
|
|
877
|
+
* **Details**
|
|
878
|
+
*
|
|
879
|
+
* This function applies a transformation function `f` to the value inside a
|
|
880
|
+
* `Some`. The function `f` may return a value, `null`, or `undefined`. If `f`
|
|
881
|
+
* returns a value, it is wrapped in a `Some`. If `f` returns `null` or
|
|
882
|
+
* `undefined`, the result is `None`. If the input `Option` is `None`, the
|
|
883
|
+
* function is not applied, and `None` is returned.
|
|
884
|
+
*
|
|
885
|
+
* This utility is particularly useful when working with deeply nested optional
|
|
886
|
+
* values or chaining computations that may result in `null` or `undefined` at
|
|
887
|
+
* some point.
|
|
527
888
|
*
|
|
528
889
|
* @example
|
|
529
890
|
* ```ts
|
|
530
|
-
* import {
|
|
891
|
+
* import { Option } from "effect"
|
|
531
892
|
*
|
|
532
893
|
* interface Employee {
|
|
533
894
|
* company?: {
|
|
@@ -539,66 +900,133 @@ const andThen = exports.andThen = /*#__PURE__*/(0, _Function.dual)(2, (self, f)
|
|
|
539
900
|
* }
|
|
540
901
|
* }
|
|
541
902
|
*
|
|
542
|
-
* const employee1: Employee = { company: { address: { street: { name:
|
|
903
|
+
* const employee1: Employee = { company: { address: { street: { name: "high street" } } } }
|
|
543
904
|
*
|
|
544
|
-
*
|
|
545
|
-
*
|
|
546
|
-
*
|
|
547
|
-
* Option.flatMapNullable(employee => employee.company?.address?.street?.name)
|
|
548
|
-
* ),
|
|
549
|
-
* Option.some('high street')
|
|
905
|
+
* // Extracting a deeply nested property
|
|
906
|
+
* console.log(
|
|
907
|
+
* Option.some(employee1)
|
|
908
|
+
* .pipe(Option.flatMapNullable((employee) => employee.company?.address?.street?.name))
|
|
550
909
|
* )
|
|
910
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'high street' }
|
|
551
911
|
*
|
|
552
912
|
* const employee2: Employee = { company: { address: { street: {} } } }
|
|
553
913
|
*
|
|
554
|
-
*
|
|
555
|
-
*
|
|
556
|
-
*
|
|
557
|
-
* Option.flatMapNullable(employee => employee.company?.address?.street?.name)
|
|
558
|
-
* ),
|
|
559
|
-
* Option.none()
|
|
914
|
+
* // Property does not exist
|
|
915
|
+
* console.log(
|
|
916
|
+
* Option.some(employee2)
|
|
917
|
+
* .pipe(Option.flatMapNullable((employee) => employee.company?.address?.street?.name))
|
|
560
918
|
* )
|
|
919
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
561
920
|
* ```
|
|
562
921
|
*
|
|
563
|
-
* @category
|
|
922
|
+
* @category Sequencing
|
|
564
923
|
* @since 2.0.0
|
|
565
924
|
*/
|
|
566
925
|
const flatMapNullable = exports.flatMapNullable = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => isNone(self) ? none() : fromNullable(f(self.value)));
|
|
567
926
|
/**
|
|
568
|
-
*
|
|
927
|
+
* Flattens an `Option` of `Option` into a single `Option`.
|
|
928
|
+
*
|
|
929
|
+
* **Details**
|
|
930
|
+
*
|
|
931
|
+
* This function takes an `Option` that wraps another `Option` and flattens it
|
|
932
|
+
* into a single `Option`. If the outer `Option` is `Some`, the function
|
|
933
|
+
* extracts the inner `Option`. If the outer `Option` is `None`, the result
|
|
934
|
+
* remains `None`.
|
|
935
|
+
*
|
|
936
|
+
* This is useful for simplifying nested `Option` structures that may arise
|
|
937
|
+
* during functional operations.
|
|
938
|
+
*
|
|
939
|
+
* @category Sequencing
|
|
569
940
|
* @since 2.0.0
|
|
570
941
|
*/
|
|
571
942
|
const flatten = exports.flatten = /*#__PURE__*/flatMap(_Function.identity);
|
|
572
943
|
/**
|
|
573
|
-
*
|
|
944
|
+
* Combines two `Option`s, keeping the value from the second `Option` if both
|
|
945
|
+
* are `Some`.
|
|
946
|
+
*
|
|
947
|
+
* **Details**
|
|
948
|
+
*
|
|
949
|
+
* This function takes two `Option`s and returns the second one if the first is
|
|
950
|
+
* `Some`. If the first `Option` is `None`, the result will also be `None`,
|
|
951
|
+
* regardless of the second `Option`. It effectively "zips" the two `Option`s
|
|
952
|
+
* while discarding the value from the first `Option`.
|
|
953
|
+
*
|
|
954
|
+
* This is particularly useful when sequencing computations where the result of
|
|
955
|
+
* the first computation is not needed, and you only care about the result of
|
|
956
|
+
* the second computation.
|
|
957
|
+
*
|
|
958
|
+
* @category Zipping
|
|
574
959
|
* @since 2.0.0
|
|
575
960
|
*/
|
|
576
961
|
const zipRight = exports.zipRight = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => flatMap(self, () => that));
|
|
577
962
|
/**
|
|
578
|
-
*
|
|
963
|
+
* Combines two `Option`s, keeping the value from the first `Option` if both are
|
|
964
|
+
* `Some`.
|
|
965
|
+
*
|
|
966
|
+
* **Details**
|
|
967
|
+
*
|
|
968
|
+
* This function takes two `Option`s and returns the first one if it is `Some`.
|
|
969
|
+
* If either the first `Option` or the second `Option` is `None`, the result
|
|
970
|
+
* will be `None`. This operation "zips" the two `Option`s while discarding the
|
|
971
|
+
* value from the second `Option`.
|
|
972
|
+
*
|
|
973
|
+
* This is useful when sequencing computations where the second `Option`
|
|
974
|
+
* represents a dependency or condition that must hold, but its value is
|
|
975
|
+
* irrelevant.
|
|
976
|
+
*
|
|
977
|
+
* @category Zipping
|
|
579
978
|
* @since 2.0.0
|
|
580
979
|
*/
|
|
581
|
-
const
|
|
980
|
+
const zipLeft = exports.zipLeft = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => tap(self, () => that));
|
|
582
981
|
/**
|
|
583
|
-
*
|
|
982
|
+
* Composes two functions that return `Option` values, creating a new function
|
|
983
|
+
* that chains them together.
|
|
584
984
|
*
|
|
585
|
-
*
|
|
985
|
+
* **Details**
|
|
586
986
|
*
|
|
587
|
-
*
|
|
588
|
-
*
|
|
987
|
+
* This function allows you to compose two computations, each represented by a
|
|
988
|
+
* function that returns an `Option`. The result of the first function is passed
|
|
989
|
+
* to the second function if it is `Some`. If the first function returns `None`,
|
|
990
|
+
* the composed function short-circuits and returns `None` without invoking the
|
|
991
|
+
* second function.
|
|
992
|
+
*
|
|
993
|
+
* @example
|
|
994
|
+
* ```ts
|
|
995
|
+
* import { Option } from "effect"
|
|
996
|
+
*
|
|
997
|
+
* const parse = (s: string): Option.Option<number> => isNaN(Number(s)) ? Option.none() : Option.some(Number(s))
|
|
998
|
+
*
|
|
999
|
+
* const double = (n: number): Option.Option<number> => n > 0 ? Option.some(n * 2) : Option.none()
|
|
1000
|
+
*
|
|
1001
|
+
* const parseAndDouble = Option.composeK(parse, double)
|
|
1002
|
+
*
|
|
1003
|
+
* console.log(parseAndDouble("42"))
|
|
1004
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 84 }
|
|
1005
|
+
*
|
|
1006
|
+
* console.log(parseAndDouble("not a number"))
|
|
1007
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1008
|
+
* ```
|
|
589
1009
|
*
|
|
590
|
-
* @category
|
|
1010
|
+
* @category Sequencing
|
|
591
1011
|
* @since 2.0.0
|
|
592
1012
|
*/
|
|
593
|
-
const
|
|
1013
|
+
const composeK = exports.composeK = /*#__PURE__*/(0, _Function.dual)(2, (afb, bfc) => a => flatMap(afb(a), bfc));
|
|
594
1014
|
/**
|
|
595
|
-
* Applies the provided function `f` to the value of the `Option` if it is
|
|
596
|
-
* unless `f` returns `None`, in which
|
|
1015
|
+
* Applies the provided function `f` to the value of the `Option` if it is
|
|
1016
|
+
* `Some` and returns the original `Option`, unless `f` returns `None`, in which
|
|
1017
|
+
* case it returns `None`.
|
|
1018
|
+
*
|
|
1019
|
+
* **Details**
|
|
597
1020
|
*
|
|
598
|
-
* This function
|
|
1021
|
+
* This function allows you to perform additional computations on the value of
|
|
1022
|
+
* an `Option` without modifying its original value. If the `Option` is `Some`,
|
|
1023
|
+
* the provided function `f` is executed with the value, and its result
|
|
1024
|
+
* determines whether the original `Option` is returned (`Some`) or the result
|
|
1025
|
+
* is `None` if `f` returns `None`. If the input `Option` is `None`, the
|
|
1026
|
+
* function is not executed, and `None` is returned.
|
|
599
1027
|
*
|
|
600
|
-
*
|
|
601
|
-
*
|
|
1028
|
+
* This is particularly useful for applying side conditions or performing
|
|
1029
|
+
* validation checks while retaining the original `Option`'s value.
|
|
602
1030
|
*
|
|
603
1031
|
* @example
|
|
604
1032
|
* ```ts
|
|
@@ -606,22 +1034,47 @@ const zipLeft = exports.zipLeft = /*#__PURE__*/(0, _Function.dual)(2, (self, tha
|
|
|
606
1034
|
*
|
|
607
1035
|
* const getInteger = (n: number) => Number.isInteger(n) ? Option.some(n) : Option.none()
|
|
608
1036
|
*
|
|
609
|
-
*
|
|
610
|
-
*
|
|
611
|
-
*
|
|
1037
|
+
* console.log(Option.tap(Option.none(), getInteger))
|
|
1038
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1039
|
+
*
|
|
1040
|
+
* console.log(Option.tap(Option.some(1), getInteger))
|
|
1041
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
1042
|
+
*
|
|
1043
|
+
* console.log(Option.tap(Option.some(1.14), getInteger))
|
|
1044
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
612
1045
|
* ```
|
|
613
1046
|
*
|
|
614
|
-
* @category
|
|
1047
|
+
* @category Sequencing
|
|
615
1048
|
* @since 2.0.0
|
|
616
1049
|
*/
|
|
617
1050
|
const tap = exports.tap = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => flatMap(self, a => map(f(a), () => a)));
|
|
618
1051
|
/**
|
|
619
|
-
*
|
|
1052
|
+
* Combines two `Option` values into a single `Option` containing a tuple of
|
|
1053
|
+
* their values if both are `Some`.
|
|
1054
|
+
*
|
|
1055
|
+
* **Details**
|
|
1056
|
+
*
|
|
1057
|
+
* This function takes two `Option`s and combines their values into a tuple `[A,
|
|
1058
|
+
* B]` if both are `Some`. If either of the `Option`s is `None`, the result is
|
|
1059
|
+
* `None`. This is particularly useful for combining multiple `Option` values
|
|
1060
|
+
* into a single one, ensuring both contain valid values.
|
|
1061
|
+
*
|
|
1062
|
+
* @category Combining
|
|
620
1063
|
* @since 2.0.0
|
|
621
1064
|
*/
|
|
622
1065
|
const product = (self, that) => isSome(self) && isSome(that) ? some([self.value, that.value]) : none();
|
|
623
1066
|
/**
|
|
624
|
-
*
|
|
1067
|
+
* Combines an `Option` with a collection of `Option`s into a single `Option`
|
|
1068
|
+
* containing a tuple of their values if all are `Some`.
|
|
1069
|
+
*
|
|
1070
|
+
* **Details**
|
|
1071
|
+
*
|
|
1072
|
+
* This function takes a primary `Option` and a collection of `Option`s and
|
|
1073
|
+
* combines their values into a tuple `[A, ...Array<A>]` if all are `Some`. If
|
|
1074
|
+
* the primary `Option` or any `Option` in the collection is `None`, the result
|
|
1075
|
+
* is `None`.
|
|
1076
|
+
*
|
|
1077
|
+
* @category Combining
|
|
625
1078
|
* @since 2.0.0
|
|
626
1079
|
*/
|
|
627
1080
|
exports.product = product;
|
|
@@ -639,24 +1092,47 @@ const productMany = (self, collection) => {
|
|
|
639
1092
|
return some(out);
|
|
640
1093
|
};
|
|
641
1094
|
/**
|
|
642
|
-
*
|
|
1095
|
+
* Combines a structure of `Option`s into a single `Option` containing the
|
|
1096
|
+
* values with the same structure.
|
|
1097
|
+
*
|
|
1098
|
+
* **Details**
|
|
643
1099
|
*
|
|
644
|
-
*
|
|
645
|
-
*
|
|
646
|
-
*
|
|
1100
|
+
* This function takes a structure of `Option`s (a tuple, struct, or iterable)
|
|
1101
|
+
* and produces a single `Option` that contains the values from the input
|
|
1102
|
+
* structure if all `Option`s are `Some`. If any `Option` in the input is
|
|
1103
|
+
* `None`, the result is `None`. The structure of the input is preserved in the
|
|
1104
|
+
* output.
|
|
647
1105
|
*
|
|
648
|
-
*
|
|
1106
|
+
* - If the input is a tuple (e.g., an array), the result will be an `Option`
|
|
1107
|
+
* containing a tuple with the same length.
|
|
1108
|
+
* - If the input is a struct (e.g., an object), the result will be an `Option`
|
|
1109
|
+
* containing a struct with the same keys.
|
|
1110
|
+
* - If the input is an iterable, the result will be an `Option` containing an
|
|
1111
|
+
* array.
|
|
649
1112
|
*
|
|
650
1113
|
* @example
|
|
651
1114
|
* ```ts
|
|
652
1115
|
* import { Option } from "effect"
|
|
653
1116
|
*
|
|
654
|
-
*
|
|
655
|
-
*
|
|
656
|
-
*
|
|
1117
|
+
* const maybeName: Option.Option<string> = Option.some("John")
|
|
1118
|
+
* const maybeAge: Option.Option<number> = Option.some(25)
|
|
1119
|
+
*
|
|
1120
|
+
* // ┌─── Option<[string, number]>
|
|
1121
|
+
* // ▼
|
|
1122
|
+
* const tuple = Option.all([maybeName, maybeAge])
|
|
1123
|
+
* console.log(tuple)
|
|
1124
|
+
* // Output:
|
|
1125
|
+
* // { _id: 'Option', _tag: 'Some', value: [ 'John', 25 ] }
|
|
1126
|
+
*
|
|
1127
|
+
* // ┌─── Option<{ name: string; age: number; }>
|
|
1128
|
+
* // ▼
|
|
1129
|
+
* const struct = Option.all({ name: maybeName, age: maybeAge })
|
|
1130
|
+
* console.log(struct)
|
|
1131
|
+
* // Output:
|
|
1132
|
+
* // { _id: 'Option', _tag: 'Some', value: { name: 'John', age: 25 } }
|
|
657
1133
|
* ```
|
|
658
1134
|
*
|
|
659
|
-
* @category
|
|
1135
|
+
* @category Combining
|
|
660
1136
|
* @since 2.0.0
|
|
661
1137
|
*/
|
|
662
1138
|
// @ts-expect-error
|
|
@@ -683,54 +1159,82 @@ const all = input => {
|
|
|
683
1159
|
return some(out);
|
|
684
1160
|
};
|
|
685
1161
|
/**
|
|
686
|
-
*
|
|
1162
|
+
* Combines two `Option` values into a new `Option` by applying a provided
|
|
1163
|
+
* function to their values.
|
|
1164
|
+
*
|
|
1165
|
+
* **Details**
|
|
1166
|
+
*
|
|
1167
|
+
* This function takes two `Option` values (`self` and `that`) and a combining
|
|
1168
|
+
* function `f`. If both `Option` values are `Some`, the function `f` is applied
|
|
1169
|
+
* to their values, and the result is wrapped in a new `Some`. If either
|
|
1170
|
+
* `Option` is `None`, the result is `None`.
|
|
687
1171
|
*
|
|
688
|
-
*
|
|
689
|
-
*
|
|
690
|
-
* @param f - The function used to combine the values of the two `Option`s
|
|
1172
|
+
* This utility is useful for combining two optional computations into a single
|
|
1173
|
+
* result while maintaining type safety and avoiding explicit checks for `None`.
|
|
691
1174
|
*
|
|
692
1175
|
* @example
|
|
693
1176
|
* ```ts
|
|
694
1177
|
* import { Option } from "effect"
|
|
695
1178
|
*
|
|
696
|
-
*
|
|
1179
|
+
* const maybeName: Option.Option<string> = Option.some("John")
|
|
1180
|
+
* const maybeAge: Option.Option<number> = Option.some(25)
|
|
697
1181
|
*
|
|
698
|
-
*
|
|
1182
|
+
* // Combine the name and age into a person object
|
|
1183
|
+
* const person = Option.zipWith(maybeName, maybeAge, (name, age) => ({
|
|
1184
|
+
* name: name.toUpperCase(),
|
|
1185
|
+
* age
|
|
1186
|
+
* }))
|
|
699
1187
|
*
|
|
700
|
-
*
|
|
701
|
-
*
|
|
702
|
-
*
|
|
703
|
-
* assert.deepStrictEqual(Option.zipWith(Option.some(1), Option.some(2), complex), Option.some([1, 2]))
|
|
704
|
-
*
|
|
705
|
-
* assert.deepStrictEqual(Option.zipWith(Option.some(1), complex)(Option.some(2)), Option.some([2, 1]))
|
|
1188
|
+
* console.log(person)
|
|
1189
|
+
* // Output:
|
|
1190
|
+
* // { _id: 'Option', _tag: 'Some', value: { name: 'JOHN', age: 25 } }
|
|
706
1191
|
* ```
|
|
707
1192
|
*
|
|
708
|
-
* @category
|
|
1193
|
+
* @category Zipping
|
|
709
1194
|
* @since 2.0.0
|
|
710
1195
|
*/
|
|
711
1196
|
exports.all = all;
|
|
712
1197
|
const zipWith = exports.zipWith = /*#__PURE__*/(0, _Function.dual)(3, (self, that, f) => map(product(self, that), ([a, b]) => f(a, b)));
|
|
713
1198
|
/**
|
|
714
|
-
*
|
|
1199
|
+
* Applies a function inside a `Some` to a value inside another `Some`,
|
|
1200
|
+
* combining them into a new `Option`.
|
|
1201
|
+
*
|
|
1202
|
+
* **Details**
|
|
1203
|
+
*
|
|
1204
|
+
* This function allows you to apply a function wrapped in an `Option` (`self`)
|
|
1205
|
+
* to a value wrapped in another `Option` (`that`). If both `Option`s are
|
|
1206
|
+
* `Some`, the function is applied to the value, and the result is wrapped in a
|
|
1207
|
+
* new `Some`. If either `Option` is `None`, the result is `None`.
|
|
1208
|
+
*
|
|
1209
|
+
* @category Combining
|
|
715
1210
|
* @since 2.0.0
|
|
716
1211
|
*/
|
|
717
1212
|
const ap = exports.ap = /*#__PURE__*/(0, _Function.dual)(2, (self, that) => zipWith(self, that, (f, a) => f(a)));
|
|
718
1213
|
/**
|
|
719
|
-
* Reduces an `Iterable` of `Option<A>` to a single value of type `B`,
|
|
1214
|
+
* Reduces an `Iterable` of `Option<A>` to a single value of type `B`, ignoring
|
|
1215
|
+
* elements that are `None`.
|
|
720
1216
|
*
|
|
721
|
-
*
|
|
722
|
-
*
|
|
723
|
-
*
|
|
1217
|
+
* **Details**
|
|
1218
|
+
*
|
|
1219
|
+
* This function takes an initial value of type `B` and a reducing function `f`
|
|
1220
|
+
* that combines the accumulator with values of type `A`. It processes an
|
|
1221
|
+
* iterable of `Option<A>`, applying `f` only to the `Some` values while
|
|
1222
|
+
* ignoring the `None` values. The result is a single value of type `B`.
|
|
1223
|
+
*
|
|
1224
|
+
* This utility is particularly useful for aggregating values from an iterable
|
|
1225
|
+
* of `Option`s while skipping the absent (`None`) values.
|
|
724
1226
|
*
|
|
725
1227
|
* @example
|
|
726
1228
|
* ```ts
|
|
727
|
-
* import {
|
|
1229
|
+
* import { Option, pipe } from "effect"
|
|
728
1230
|
*
|
|
729
1231
|
* const iterable = [Option.some(1), Option.none(), Option.some(2), Option.none()]
|
|
730
|
-
*
|
|
1232
|
+
*
|
|
1233
|
+
* console.log(pipe(iterable, Option.reduceCompact(0, (b, a) => b + a)))
|
|
1234
|
+
* // Output: 3
|
|
731
1235
|
* ```
|
|
732
1236
|
*
|
|
733
|
-
* @category
|
|
1237
|
+
* @category Reducing
|
|
734
1238
|
* @since 2.0.0
|
|
735
1239
|
*/
|
|
736
1240
|
const reduceCompact = exports.reduceCompact = /*#__PURE__*/(0, _Function.dual)(3, (self, b, f) => {
|
|
@@ -743,26 +1247,45 @@ const reduceCompact = exports.reduceCompact = /*#__PURE__*/(0, _Function.dual)(3
|
|
|
743
1247
|
return out;
|
|
744
1248
|
});
|
|
745
1249
|
/**
|
|
746
|
-
*
|
|
1250
|
+
* Converts an `Option` into an `Array`.
|
|
747
1251
|
* If the input is `None`, an empty array is returned.
|
|
748
|
-
* If the input is `Some`,
|
|
749
|
-
*
|
|
750
|
-
* @param self - The `Option` to convert to an array.
|
|
1252
|
+
* If the input is `Some`, its value is wrapped in a single-element array.
|
|
751
1253
|
*
|
|
752
1254
|
* @example
|
|
753
1255
|
* ```ts
|
|
754
1256
|
* import { Option } from "effect"
|
|
755
1257
|
*
|
|
756
|
-
*
|
|
757
|
-
*
|
|
1258
|
+
* console.log(Option.toArray(Option.some(1)))
|
|
1259
|
+
* // Output: [1]
|
|
1260
|
+
*
|
|
1261
|
+
* console.log(Option.toArray(Option.none()))
|
|
1262
|
+
* // Output: []
|
|
758
1263
|
* ```
|
|
759
1264
|
*
|
|
760
|
-
* @category
|
|
1265
|
+
* @category Conversions
|
|
761
1266
|
* @since 2.0.0
|
|
762
1267
|
*/
|
|
763
1268
|
const toArray = self => isNone(self) ? [] : [self.value];
|
|
764
1269
|
/**
|
|
765
|
-
*
|
|
1270
|
+
* Splits an `Option` into two `Option`s based on the result of a mapping
|
|
1271
|
+
* function that produces an `Either`.
|
|
1272
|
+
*
|
|
1273
|
+
* **Details**
|
|
1274
|
+
*
|
|
1275
|
+
* This function takes an `Option` and a mapping function `f` that converts its
|
|
1276
|
+
* value into an `Either`. It returns a tuple of two `Option`s:
|
|
1277
|
+
*
|
|
1278
|
+
* - The first `Option` (`left`) contains the value from the `Left` side of the
|
|
1279
|
+
* `Either` if it exists, otherwise `None`.
|
|
1280
|
+
* - The second `Option` (`right`) contains the value from the `Right` side of
|
|
1281
|
+
* the `Either` if it exists, otherwise `None`.
|
|
1282
|
+
*
|
|
1283
|
+
* If the input `Option` is `None`, both returned `Option`s are `None`.
|
|
1284
|
+
*
|
|
1285
|
+
* This utility is useful for filtering and categorizing the contents of an
|
|
1286
|
+
* `Option` based on a bifurcating computation.
|
|
1287
|
+
*
|
|
1288
|
+
* @category Filtering
|
|
766
1289
|
* @since 2.0.0
|
|
767
1290
|
*/
|
|
768
1291
|
exports.toArray = toArray;
|
|
@@ -774,25 +1297,40 @@ const partitionMap = exports.partitionMap = /*#__PURE__*/(0, _Function.dual)(2,
|
|
|
774
1297
|
return either.isLeft(e) ? [some(e.left), none()] : [none(), some(e.right)];
|
|
775
1298
|
});
|
|
776
1299
|
/**
|
|
777
|
-
* Maps over the value of an `Option`
|
|
1300
|
+
* Maps over the value of an `Option` with a function that may return `None`,
|
|
1301
|
+
* effectively filtering and transforming the value.
|
|
1302
|
+
*
|
|
1303
|
+
* **Details**
|
|
778
1304
|
*
|
|
779
|
-
*
|
|
1305
|
+
* This function allows you to both transform the value of a `Some` and filter
|
|
1306
|
+
* it at the same time. The mapping function `f` can either return a new
|
|
1307
|
+
* `Option` (to transform the value) or return `None` to filter it out. If the
|
|
1308
|
+
* input `Option` is `None`, the function is not applied, and the result remains
|
|
1309
|
+
* `None`.
|
|
780
1310
|
*
|
|
781
|
-
*
|
|
782
|
-
*
|
|
1311
|
+
* This utility is particularly useful when you want to apply a transformation
|
|
1312
|
+
* to the value of an `Option` while conditionally removing invalid or unwanted
|
|
1313
|
+
* results.
|
|
783
1314
|
*
|
|
784
1315
|
* @example
|
|
785
1316
|
* ```ts
|
|
786
1317
|
* import { Option } from "effect"
|
|
787
1318
|
*
|
|
788
|
-
*
|
|
1319
|
+
* // Transform and filter numbers
|
|
1320
|
+
* const transformEven = (n: Option.Option<number>): Option.Option<string> =>
|
|
1321
|
+
* Option.flatMap(n, (n) => (n % 2 === 0 ? Option.some(`Even: ${n}`) : Option.none()))
|
|
789
1322
|
*
|
|
790
|
-
*
|
|
791
|
-
*
|
|
792
|
-
*
|
|
1323
|
+
* console.log(transformEven(Option.none()))
|
|
1324
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1325
|
+
*
|
|
1326
|
+
* console.log(transformEven(Option.some(1)))
|
|
1327
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1328
|
+
*
|
|
1329
|
+
* console.log(transformEven(Option.some(2)))
|
|
1330
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'Even: 2' }
|
|
793
1331
|
* ```
|
|
794
1332
|
*
|
|
795
|
-
* @category
|
|
1333
|
+
* @category Filtering
|
|
796
1334
|
* @since 2.0.0
|
|
797
1335
|
*/
|
|
798
1336
|
const filterMap = exports.filterMap = /*#__PURE__*/(0, _Function.dual)(2, (self, f) => isNone(self) ? none() : f(self.value));
|
|
@@ -801,150 +1339,277 @@ const filterMap = exports.filterMap = /*#__PURE__*/(0, _Function.dual)(2, (self,
|
|
|
801
1339
|
*
|
|
802
1340
|
* If you need to change the type of the `Option` in addition to filtering, see `filterMap`.
|
|
803
1341
|
*
|
|
804
|
-
* @param predicate - A predicate function to apply to the `Option` value.
|
|
805
|
-
* @param fb - The `Option` to filter.
|
|
806
|
-
*
|
|
807
1342
|
* @example
|
|
808
1343
|
* ```ts
|
|
809
1344
|
* import { Option } from "effect"
|
|
810
1345
|
*
|
|
811
|
-
*
|
|
812
|
-
*
|
|
1346
|
+
* const removeEmptyString = (input: Option.Option<string>) =>
|
|
1347
|
+
* Option.filter(input, (value) => value !== "")
|
|
813
1348
|
*
|
|
814
|
-
*
|
|
815
|
-
*
|
|
816
|
-
* assert.deepStrictEqual(Option.filter(Option.some(2), isEven), Option.some(2))
|
|
1349
|
+
* console.log(removeEmptyString(Option.none()))
|
|
1350
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
817
1351
|
*
|
|
818
|
-
*
|
|
819
|
-
*
|
|
1352
|
+
* console.log(removeEmptyString(Option.some("")))
|
|
1353
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
820
1354
|
*
|
|
821
|
-
*
|
|
822
|
-
*
|
|
823
|
-
* assert.deepStrictEqual(Option.filter(Option.some(2), isNumber), Option.some(2))
|
|
1355
|
+
* console.log(removeEmptyString(Option.some("a")))
|
|
1356
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 'a' }
|
|
824
1357
|
* ```
|
|
825
1358
|
*
|
|
826
|
-
* @category
|
|
1359
|
+
* @category Filtering
|
|
827
1360
|
* @since 2.0.0
|
|
828
1361
|
*/
|
|
829
1362
|
const filter = exports.filter = /*#__PURE__*/(0, _Function.dual)(2, (self, predicate) => filterMap(self, b => predicate(b) ? option.some(b) : option.none));
|
|
830
1363
|
/**
|
|
1364
|
+
* Creates an `Equivalence` instance for comparing `Option` values, using a
|
|
1365
|
+
* provided `Equivalence` for the inner type.
|
|
1366
|
+
*
|
|
1367
|
+
* **Details**
|
|
1368
|
+
*
|
|
1369
|
+
* This function takes an `Equivalence` instance for a specific type `A` and
|
|
1370
|
+
* produces an `Equivalence` instance for `Option<A>`. The resulting
|
|
1371
|
+
* `Equivalence` determines whether two `Option` values are equivalent:
|
|
1372
|
+
*
|
|
1373
|
+
* - Two `None`s are considered equivalent.
|
|
1374
|
+
* - A `Some` and a `None` are not equivalent.
|
|
1375
|
+
* - Two `Some` values are equivalent if their inner values are equivalent
|
|
1376
|
+
* according to the provided `Equivalence`.
|
|
1377
|
+
*
|
|
831
1378
|
* @example
|
|
832
1379
|
* ```ts
|
|
833
|
-
*
|
|
1380
|
+
* // Title: Comparing Optional Numbers for Equivalence
|
|
1381
|
+
* import { Number, Option } from "effect"
|
|
834
1382
|
*
|
|
835
1383
|
* const isEquivalent = Option.getEquivalence(Number.Equivalence)
|
|
836
|
-
*
|
|
837
|
-
*
|
|
838
|
-
*
|
|
839
|
-
*
|
|
840
|
-
*
|
|
1384
|
+
*
|
|
1385
|
+
* console.log(isEquivalent(Option.none(), Option.none()))
|
|
1386
|
+
* // Output: true
|
|
1387
|
+
*
|
|
1388
|
+
* console.log(isEquivalent(Option.none(), Option.some(1)))
|
|
1389
|
+
* // Output: false
|
|
1390
|
+
*
|
|
1391
|
+
* console.log(isEquivalent(Option.some(1), Option.none()))
|
|
1392
|
+
* // Output: false
|
|
1393
|
+
*
|
|
1394
|
+
* console.log(isEquivalent(Option.some(1), Option.some(2)))
|
|
1395
|
+
* // Output: false
|
|
1396
|
+
*
|
|
1397
|
+
* console.log(isEquivalent(Option.some(1), Option.some(1)))
|
|
1398
|
+
* // Output: true
|
|
841
1399
|
* ```
|
|
842
1400
|
*
|
|
843
|
-
* @category
|
|
1401
|
+
* @category Equivalence
|
|
844
1402
|
* @since 2.0.0
|
|
845
1403
|
*/
|
|
846
1404
|
const getEquivalence = isEquivalent => Equivalence.make((x, y) => isNone(x) ? isNone(y) : isNone(y) ? false : isEquivalent(x.value, y.value));
|
|
847
1405
|
/**
|
|
848
|
-
*
|
|
849
|
-
* `
|
|
850
|
-
* the type the `Option` contains.
|
|
1406
|
+
* Creates an `Order` instance for comparing `Option` values, using a provided
|
|
1407
|
+
* `Order` for the inner type.
|
|
851
1408
|
*
|
|
852
|
-
*
|
|
1409
|
+
* **Details**
|
|
1410
|
+
*
|
|
1411
|
+
* This function produces an `Order` instance for `Option<A>`, allowing `Option`
|
|
1412
|
+
* values to be compared:
|
|
1413
|
+
*
|
|
1414
|
+
* - `None` is always considered less than any `Some` value.
|
|
1415
|
+
* - If both are `Some`, their inner values are compared using the provided
|
|
1416
|
+
* `Order` instance.
|
|
853
1417
|
*
|
|
854
1418
|
* @example
|
|
855
1419
|
* ```ts
|
|
856
|
-
* import {
|
|
857
|
-
*
|
|
858
|
-
* const
|
|
859
|
-
*
|
|
860
|
-
*
|
|
861
|
-
*
|
|
862
|
-
*
|
|
863
|
-
*
|
|
1420
|
+
* import { Number, Option } from "effect"
|
|
1421
|
+
*
|
|
1422
|
+
* const order = Option.getOrder(Number.Order)
|
|
1423
|
+
*
|
|
1424
|
+
* console.log(order(Option.none(), Option.none()))
|
|
1425
|
+
* // Output: 0
|
|
1426
|
+
*
|
|
1427
|
+
* console.log(order(Option.none(), Option.some(1)))
|
|
1428
|
+
* // Output: -1
|
|
1429
|
+
*
|
|
1430
|
+
* console.log(order(Option.some(1), Option.none()))
|
|
1431
|
+
* // Output: 1
|
|
1432
|
+
*
|
|
1433
|
+
* console.log(order(Option.some(1), Option.some(2)))
|
|
1434
|
+
* // Output: -1
|
|
1435
|
+
*
|
|
1436
|
+
* console.log(order(Option.some(1), Option.some(1)))
|
|
1437
|
+
* // Output: 0
|
|
864
1438
|
* ```
|
|
865
1439
|
*
|
|
866
|
-
* @category
|
|
1440
|
+
* @category Sorting
|
|
867
1441
|
* @since 2.0.0
|
|
868
1442
|
*/
|
|
869
1443
|
exports.getEquivalence = getEquivalence;
|
|
870
1444
|
const getOrder = O => order.make((self, that) => isSome(self) ? isSome(that) ? O(self.value, that.value) : 1 : -1);
|
|
871
1445
|
/**
|
|
872
|
-
* Lifts a binary function
|
|
1446
|
+
* Lifts a binary function to work with `Option` values, allowing the function
|
|
1447
|
+
* to operate on two `Option`s.
|
|
1448
|
+
*
|
|
1449
|
+
* **Details**
|
|
873
1450
|
*
|
|
874
|
-
*
|
|
1451
|
+
* This function takes a binary function `f` and returns a new function that
|
|
1452
|
+
* applies `f` to the values of two `Option`s (`self` and `that`). If both
|
|
1453
|
+
* `Option`s are `Some`, the binary function `f` is applied to their values, and
|
|
1454
|
+
* the result is wrapped in a new `Some`. If either `Option` is `None`, the
|
|
1455
|
+
* result is `None`.
|
|
875
1456
|
*
|
|
876
|
-
* @
|
|
1457
|
+
* @example
|
|
1458
|
+
* ```ts
|
|
1459
|
+
* import { Option } from "effect"
|
|
1460
|
+
*
|
|
1461
|
+
* // A binary function to add two numbers
|
|
1462
|
+
* const add = (a: number, b: number): number => a + b
|
|
1463
|
+
*
|
|
1464
|
+
* // Lift the `add` function to work with `Option` values
|
|
1465
|
+
* const addOptions = Option.lift2(add)
|
|
1466
|
+
*
|
|
1467
|
+
* // Both `Option`s are `Some`
|
|
1468
|
+
* console.log(addOptions(Option.some(2), Option.some(3)))
|
|
1469
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 5 }
|
|
1470
|
+
*
|
|
1471
|
+
* // One `Option` is `None`
|
|
1472
|
+
* console.log(addOptions(Option.some(2), Option.none()))
|
|
1473
|
+
* // Output: { _id: 'Option', _tag: 'None' }
|
|
1474
|
+
* ```
|
|
1475
|
+
*
|
|
1476
|
+
* @category Lifting
|
|
877
1477
|
* @since 2.0.0
|
|
878
1478
|
*/
|
|
879
1479
|
exports.getOrder = getOrder;
|
|
880
1480
|
const lift2 = f => (0, _Function.dual)(2, (self, that) => zipWith(self, that, f));
|
|
881
1481
|
/**
|
|
882
|
-
*
|
|
883
|
-
* if the predicate
|
|
1482
|
+
* Lifts a `Predicate` or `Refinement` into the `Option` context, returning a
|
|
1483
|
+
* `Some` of the input value if the predicate is satisfied, or `None` otherwise.
|
|
884
1484
|
*
|
|
885
|
-
*
|
|
1485
|
+
* **Details**
|
|
1486
|
+
*
|
|
1487
|
+
* This function transforms a `Predicate` (or a more specific `Refinement`) into
|
|
1488
|
+
* a function that produces an `Option`. If the predicate evaluates to `true`,
|
|
1489
|
+
* the input value is wrapped in a `Some`. If the predicate evaluates to
|
|
1490
|
+
* `false`, the result is `None`.
|
|
886
1491
|
*
|
|
887
1492
|
* @example
|
|
888
1493
|
* ```ts
|
|
889
1494
|
* import { Option } from "effect"
|
|
890
1495
|
*
|
|
891
|
-
*
|
|
1496
|
+
* // Check if a number is positive
|
|
1497
|
+
* const isPositive = (n: number) => n > 0
|
|
1498
|
+
*
|
|
1499
|
+
* // ┌─── (b: number) => Option<number>
|
|
1500
|
+
* // ▼
|
|
1501
|
+
* const parsePositive = Option.liftPredicate(isPositive)
|
|
892
1502
|
*
|
|
893
|
-
*
|
|
894
|
-
*
|
|
1503
|
+
* console.log(parsePositive(1))
|
|
1504
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 1 }
|
|
1505
|
+
*
|
|
1506
|
+
* console.log(parsePositive(-1))
|
|
1507
|
+
* // OUtput: { _id: 'Option', _tag: 'None' }
|
|
895
1508
|
* ```
|
|
896
1509
|
*
|
|
897
|
-
* @category
|
|
1510
|
+
* @category Lifting
|
|
898
1511
|
* @since 2.0.0
|
|
899
1512
|
*/
|
|
900
1513
|
exports.lift2 = lift2;
|
|
901
1514
|
const liftPredicate = exports.liftPredicate = /*#__PURE__*/(0, _Function.dual)(2, (b, predicate) => predicate(b) ? some(b) : none());
|
|
902
1515
|
/**
|
|
903
|
-
* Returns a function that checks if
|
|
1516
|
+
* Returns a function that checks if an `Option` contains a specified value,
|
|
1517
|
+
* using a provided equivalence function.
|
|
1518
|
+
*
|
|
1519
|
+
* **Details**
|
|
904
1520
|
*
|
|
905
|
-
*
|
|
906
|
-
*
|
|
907
|
-
*
|
|
1521
|
+
* This function allows you to check whether an `Option` contains a specific
|
|
1522
|
+
* value. It uses an equivalence function `isEquivalent` to compare the value
|
|
1523
|
+
* inside the `Option` to the provided value. If the `Option` is `Some` and the
|
|
1524
|
+
* equivalence function returns `true`, the result is `true`. If the `Option` is
|
|
1525
|
+
* `None` or the values are not equivalent, the result is `false`.
|
|
1526
|
+
*
|
|
1527
|
+
* @see {@link contains} for a version that uses the default `Equivalence`.
|
|
908
1528
|
*
|
|
909
1529
|
* @example
|
|
910
1530
|
* ```ts
|
|
911
|
-
* import {
|
|
1531
|
+
* import { Number, Option } from "effect"
|
|
1532
|
+
*
|
|
1533
|
+
* const contains = Option.containsWith(Number.Equivalence)
|
|
1534
|
+
*
|
|
1535
|
+
* console.log(Option.some(2).pipe(contains(2)))
|
|
1536
|
+
* // Output: true
|
|
1537
|
+
*
|
|
1538
|
+
* console.log(Option.some(1).pipe(contains(2)))
|
|
1539
|
+
* // Output: false
|
|
912
1540
|
*
|
|
913
|
-
*
|
|
914
|
-
*
|
|
915
|
-
* assert.deepStrictEqual(pipe(Option.none(), Option.containsWith(Number.Equivalence)(2)), false)
|
|
1541
|
+
* console.log(Option.none().pipe(contains(2)))
|
|
1542
|
+
* // Output: false
|
|
916
1543
|
* ```
|
|
917
1544
|
*
|
|
918
|
-
* @category
|
|
1545
|
+
* @category Elements
|
|
919
1546
|
* @since 2.0.0
|
|
920
1547
|
*/
|
|
921
1548
|
const containsWith = isEquivalent => (0, _Function.dual)(2, (self, a) => isNone(self) ? false : isEquivalent(self.value, a));
|
|
922
1549
|
exports.containsWith = containsWith;
|
|
923
1550
|
const _equivalence = /*#__PURE__*/Equal.equivalence();
|
|
924
1551
|
/**
|
|
925
|
-
* Returns a function that checks if an `Option` contains a
|
|
1552
|
+
* Returns a function that checks if an `Option` contains a specified value
|
|
1553
|
+
* using the default `Equivalence`.
|
|
926
1554
|
*
|
|
927
|
-
*
|
|
1555
|
+
* **Details**
|
|
1556
|
+
*
|
|
1557
|
+
* This function allows you to check whether an `Option` contains a specific
|
|
1558
|
+
* value. It uses the default `Equivalence` for equality comparison. If the
|
|
1559
|
+
* `Option` is `Some` and its value is equivalent to the provided value, the
|
|
1560
|
+
* result is `true`. If the `Option` is `None` or the values are not equivalent,
|
|
1561
|
+
* the result is `false`.
|
|
1562
|
+
*
|
|
1563
|
+
* @see {@link containsWith} for a version that allows you to specify a custom equivalence function.
|
|
1564
|
+
*
|
|
1565
|
+
* @example
|
|
1566
|
+
* ```ts
|
|
1567
|
+
* import { Option } from "effect"
|
|
1568
|
+
*
|
|
1569
|
+
* console.log(Option.some(2).pipe(Option.contains(2)))
|
|
1570
|
+
* // Output: true
|
|
1571
|
+
*
|
|
1572
|
+
* console.log(Option.some(1).pipe(Option.contains(2)))
|
|
1573
|
+
* // Output: false
|
|
1574
|
+
*
|
|
1575
|
+
* console.log(Option.none().pipe(Option.contains(2)))
|
|
1576
|
+
* // Output: false
|
|
1577
|
+
* ```
|
|
1578
|
+
*
|
|
1579
|
+
* @category Elements
|
|
928
1580
|
* @since 2.0.0
|
|
929
1581
|
*/
|
|
930
1582
|
const contains = exports.contains = /*#__PURE__*/containsWith(_equivalence);
|
|
931
1583
|
/**
|
|
932
|
-
*
|
|
1584
|
+
* Checks if a value in an `Option` satisfies a given predicate or refinement.
|
|
1585
|
+
*
|
|
1586
|
+
* **Details**
|
|
1587
|
+
*
|
|
1588
|
+
* This function allows you to check if a value inside a `Some` meets a
|
|
1589
|
+
* specified condition. If the `Option` is `None`, the result is `false`. If the
|
|
1590
|
+
* `Option` is `Some`, the provided predicate or refinement is applied to the
|
|
1591
|
+
* value:
|
|
933
1592
|
*
|
|
934
|
-
*
|
|
935
|
-
*
|
|
1593
|
+
* - If the condition is met, the result is `true`.
|
|
1594
|
+
* - If the condition is not met, the result is `false`.
|
|
936
1595
|
*
|
|
937
1596
|
* @example
|
|
938
1597
|
* ```ts
|
|
939
|
-
* import {
|
|
1598
|
+
* import { Option } from "effect"
|
|
940
1599
|
*
|
|
941
1600
|
* const isEven = (n: number) => n % 2 === 0
|
|
942
1601
|
*
|
|
943
|
-
*
|
|
944
|
-
*
|
|
945
|
-
*
|
|
1602
|
+
* console.log(Option.some(2).pipe(Option.exists(isEven)))
|
|
1603
|
+
* // Output: true
|
|
1604
|
+
*
|
|
1605
|
+
* console.log(Option.some(1).pipe(Option.exists(isEven)))
|
|
1606
|
+
* // Output: false
|
|
1607
|
+
*
|
|
1608
|
+
* console.log(Option.none().pipe(Option.exists(isEven)))
|
|
1609
|
+
* // Output: false
|
|
946
1610
|
* ```
|
|
947
1611
|
*
|
|
1612
|
+
* @category Elements
|
|
948
1613
|
* @since 2.0.0
|
|
949
1614
|
*/
|
|
950
1615
|
const exists = exports.exists = /*#__PURE__*/(0, _Function.dual)(2, (self, refinement) => isNone(self) ? false : refinement(self.value));
|
|
@@ -980,7 +1645,7 @@ const exists = exports.exists = /*#__PURE__*/(0, _Function.dual)(2, (self, refin
|
|
|
980
1645
|
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
981
1646
|
* ```
|
|
982
1647
|
*
|
|
983
|
-
* @category
|
|
1648
|
+
* @category Do notation
|
|
984
1649
|
* @since 2.0.0
|
|
985
1650
|
*/
|
|
986
1651
|
const bindTo = exports.bindTo = /*#__PURE__*/doNotation.bindTo(map);
|
|
@@ -1014,7 +1679,7 @@ const let_ = exports.let = /*#__PURE__*/doNotation.let_(map);
|
|
|
1014
1679
|
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
1015
1680
|
* ```
|
|
1016
1681
|
*
|
|
1017
|
-
* @category
|
|
1682
|
+
* @category Do notation
|
|
1018
1683
|
* @since 2.0.0
|
|
1019
1684
|
*/
|
|
1020
1685
|
const bind = exports.bind = /*#__PURE__*/doNotation.bind(map, flatMap);
|
|
@@ -1047,52 +1712,66 @@ const bind = exports.bind = /*#__PURE__*/doNotation.bind(map, flatMap);
|
|
|
1047
1712
|
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
1048
1713
|
* ```
|
|
1049
1714
|
*
|
|
1050
|
-
* @category
|
|
1715
|
+
* @category Do notation
|
|
1051
1716
|
* @since 2.0.0
|
|
1052
1717
|
*/
|
|
1053
1718
|
const Do = exports.Do = /*#__PURE__*/some({});
|
|
1054
1719
|
const adapter = /*#__PURE__*/Gen.adapter();
|
|
1055
1720
|
/**
|
|
1056
|
-
*
|
|
1721
|
+
* Similar to `Effect.gen`, `Option.gen` provides a more readable,
|
|
1722
|
+
* generator-based syntax for working with `Option` values, making code that
|
|
1723
|
+
* involves `Option` easier to write and understand. This approach is similar to
|
|
1724
|
+
* using `async/await` but tailored for `Option`.
|
|
1725
|
+
*
|
|
1726
|
+
* @example
|
|
1727
|
+
* ```ts
|
|
1728
|
+
* // Title: Using Option.gen to Create a Combined Value
|
|
1729
|
+
* import { Option } from "effect"
|
|
1730
|
+
*
|
|
1731
|
+
* const maybeName: Option.Option<string> = Option.some("John")
|
|
1732
|
+
* const maybeAge: Option.Option<number> = Option.some(25)
|
|
1733
|
+
*
|
|
1734
|
+
* const person = Option.gen(function* () {
|
|
1735
|
+
* const name = (yield* maybeName).toUpperCase()
|
|
1736
|
+
* const age = yield* maybeAge
|
|
1737
|
+
* return { name, age }
|
|
1738
|
+
* })
|
|
1739
|
+
*
|
|
1740
|
+
* console.log(person)
|
|
1741
|
+
* // Output:
|
|
1742
|
+
* // { _id: 'Option', _tag: 'Some', value: { name: 'JOHN', age: 25 } }
|
|
1743
|
+
* ```
|
|
1744
|
+
*
|
|
1745
|
+
* @category Generators
|
|
1057
1746
|
* @since 2.0.0
|
|
1058
1747
|
*/
|
|
1059
1748
|
const gen = (...args) => {
|
|
1060
|
-
|
|
1061
|
-
if (args.length === 1) {
|
|
1062
|
-
f = args[0];
|
|
1063
|
-
} else {
|
|
1064
|
-
f = args[1].bind(args[0]);
|
|
1065
|
-
}
|
|
1749
|
+
const f = args.length === 1 ? args[0] : args[1].bind(args[0]);
|
|
1066
1750
|
const iterator = f(adapter);
|
|
1067
1751
|
let state = iterator.next();
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
} else {
|
|
1071
|
-
let current = state.value;
|
|
1072
|
-
if (Gen.isGenKind(current)) {
|
|
1073
|
-
current = current.value;
|
|
1074
|
-
} else {
|
|
1075
|
-
current = Gen.yieldWrapGet(current);
|
|
1076
|
-
}
|
|
1752
|
+
while (!state.done) {
|
|
1753
|
+
const current = Gen.isGenKind(state.value) ? state.value.value : Gen.yieldWrapGet(state.value);
|
|
1077
1754
|
if (isNone(current)) {
|
|
1078
1755
|
return current;
|
|
1079
1756
|
}
|
|
1080
|
-
|
|
1081
|
-
state = iterator.next(current.value);
|
|
1082
|
-
if (!state.done) {
|
|
1083
|
-
current = state.value;
|
|
1084
|
-
if (Gen.isGenKind(current)) {
|
|
1085
|
-
current = current.value;
|
|
1086
|
-
} else {
|
|
1087
|
-
current = Gen.yieldWrapGet(current);
|
|
1088
|
-
}
|
|
1089
|
-
if (isNone(current)) {
|
|
1090
|
-
return current;
|
|
1091
|
-
}
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
return some(state.value);
|
|
1757
|
+
state = iterator.next(current.value);
|
|
1095
1758
|
}
|
|
1759
|
+
return some(state.value);
|
|
1096
1760
|
};
|
|
1761
|
+
/**
|
|
1762
|
+
* Merges two optional values, applying a function if both exist.
|
|
1763
|
+
* Unlike {@link zipWith}, this function returns `None` only if both inputs are `None`.
|
|
1764
|
+
*
|
|
1765
|
+
* @internal
|
|
1766
|
+
*/
|
|
1097
1767
|
exports.gen = gen;
|
|
1768
|
+
const mergeWith = f => (o1, o2) => {
|
|
1769
|
+
if (isNone(o1)) {
|
|
1770
|
+
return o2;
|
|
1771
|
+
} else if (isNone(o2)) {
|
|
1772
|
+
return o1;
|
|
1773
|
+
}
|
|
1774
|
+
return some(f(o1.value, o2.value));
|
|
1775
|
+
};
|
|
1776
|
+
exports.mergeWith = mergeWith;
|
|
1098
1777
|
//# sourceMappingURL=Option.js.map
|