effect 3.1.1 → 3.1.3
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/Array.js +10 -13
- package/dist/cjs/Array.js.map +1 -1
- package/dist/cjs/BigDecimal.js +14 -17
- package/dist/cjs/BigDecimal.js.map +1 -1
- package/dist/cjs/BigInt.js +41 -46
- package/dist/cjs/BigInt.js.map +1 -1
- package/dist/cjs/Boolean.js +13 -13
- package/dist/cjs/Brand.js +2 -2
- package/dist/cjs/Chunk.js +3 -3
- package/dist/cjs/Context.js +15 -20
- package/dist/cjs/Context.js.map +1 -1
- package/dist/cjs/Cron.js +3 -6
- package/dist/cjs/Cron.js.map +1 -1
- package/dist/cjs/Data.js +8 -13
- package/dist/cjs/Data.js.map +1 -1
- package/dist/cjs/Duration.js +1 -1
- package/dist/cjs/Effect.js +77 -9
- package/dist/cjs/Effect.js.map +1 -1
- package/dist/cjs/Either.js +117 -59
- package/dist/cjs/Either.js.map +1 -1
- package/dist/cjs/FiberHandle.js.map +1 -1
- package/dist/cjs/FiberMap.js.map +1 -1
- package/dist/cjs/FiberSet.js.map +1 -1
- package/dist/cjs/Function.js +2 -2
- package/dist/cjs/Function.js.map +1 -1
- package/dist/cjs/Iterable.js +10 -13
- package/dist/cjs/Iterable.js.map +1 -1
- package/dist/cjs/List.js +2 -2
- package/dist/cjs/Metric.js +5 -7
- package/dist/cjs/Metric.js.map +1 -1
- package/dist/cjs/Number.js +20 -21
- package/dist/cjs/Number.js.map +1 -1
- package/dist/cjs/Option.js +223 -164
- package/dist/cjs/Option.js.map +1 -1
- package/dist/cjs/Order.js +1 -2
- package/dist/cjs/Order.js.map +1 -1
- package/dist/cjs/Ordering.js +2 -2
- package/dist/cjs/Predicate.js +9 -12
- package/dist/cjs/Predicate.js.map +1 -1
- package/dist/cjs/Readable.js.map +1 -1
- package/dist/cjs/Record.js +35 -47
- package/dist/cjs/Record.js.map +1 -1
- package/dist/cjs/RegExp.js +1 -1
- package/dist/cjs/Request.js +2 -2
- package/dist/cjs/Request.js.map +1 -1
- package/dist/cjs/Stream.js +82 -4
- package/dist/cjs/Stream.js.map +1 -1
- package/dist/cjs/String.js +81 -111
- package/dist/cjs/String.js.map +1 -1
- package/dist/cjs/Struct.js +13 -19
- package/dist/cjs/Struct.js.map +1 -1
- package/dist/cjs/Subscribable.js.map +1 -1
- package/dist/cjs/Symbol.js +3 -3
- package/dist/cjs/TestAnnotation.js.map +1 -1
- package/dist/cjs/TestAnnotationMap.js.map +1 -1
- package/dist/cjs/TestAnnotations.js.map +1 -1
- package/dist/cjs/TestClock.js.map +1 -1
- package/dist/cjs/TestLive.js.map +1 -1
- package/dist/cjs/TestSized.js.map +1 -1
- package/dist/cjs/Trie.js +28 -44
- package/dist/cjs/Trie.js.map +1 -1
- package/dist/cjs/Utils.js.map +1 -1
- package/dist/cjs/internal/core-effect.js +4 -11
- package/dist/cjs/internal/core-effect.js.map +1 -1
- package/dist/cjs/internal/doNotation.js +23 -0
- package/dist/cjs/internal/doNotation.js.map +1 -0
- package/dist/cjs/internal/groupBy.js.map +1 -1
- package/dist/cjs/internal/stream.js +3 -7
- package/dist/cjs/internal/stream.js.map +1 -1
- package/dist/cjs/internal/version.js +1 -1
- package/dist/dts/Array.d.ts +10 -13
- package/dist/dts/Array.d.ts.map +1 -1
- package/dist/dts/BigDecimal.d.ts +14 -17
- package/dist/dts/BigDecimal.d.ts.map +1 -1
- package/dist/dts/BigInt.d.ts +41 -46
- package/dist/dts/BigInt.d.ts.map +1 -1
- package/dist/dts/Boolean.d.ts +13 -13
- package/dist/dts/Brand.d.ts +3 -3
- package/dist/dts/Chunk.d.ts +3 -3
- package/dist/dts/Context.d.ts +15 -20
- package/dist/dts/Context.d.ts.map +1 -1
- package/dist/dts/Cron.d.ts +3 -6
- package/dist/dts/Cron.d.ts.map +1 -1
- package/dist/dts/Data.d.ts +10 -16
- package/dist/dts/Data.d.ts.map +1 -1
- package/dist/dts/Duration.d.ts +1 -1
- package/dist/dts/Effect.d.ts +120 -26
- package/dist/dts/Effect.d.ts.map +1 -1
- package/dist/dts/Either.d.ts +155 -64
- package/dist/dts/Either.d.ts.map +1 -1
- package/dist/dts/FiberHandle.d.ts.map +1 -1
- package/dist/dts/FiberMap.d.ts.map +1 -1
- package/dist/dts/FiberSet.d.ts.map +1 -1
- package/dist/dts/Function.d.ts +4 -2
- package/dist/dts/Function.d.ts.map +1 -1
- package/dist/dts/HashMap.d.ts +2 -2
- package/dist/dts/Iterable.d.ts +10 -13
- package/dist/dts/Iterable.d.ts.map +1 -1
- package/dist/dts/List.d.ts +2 -2
- package/dist/dts/Metric.d.ts +5 -7
- package/dist/dts/Metric.d.ts.map +1 -1
- package/dist/dts/Number.d.ts +20 -21
- package/dist/dts/Number.d.ts.map +1 -1
- package/dist/dts/Option.d.ts +245 -155
- package/dist/dts/Option.d.ts.map +1 -1
- package/dist/dts/Order.d.ts +1 -2
- package/dist/dts/Order.d.ts.map +1 -1
- package/dist/dts/Ordering.d.ts +2 -2
- package/dist/dts/Predicate.d.ts +9 -12
- package/dist/dts/Predicate.d.ts.map +1 -1
- package/dist/dts/Readable.d.ts.map +1 -1
- package/dist/dts/Record.d.ts +35 -47
- package/dist/dts/Record.d.ts.map +1 -1
- package/dist/dts/RegExp.d.ts +1 -1
- package/dist/dts/Request.d.ts +2 -2
- package/dist/dts/Request.d.ts.map +1 -1
- package/dist/dts/Stream.d.ts +136 -31
- package/dist/dts/Stream.d.ts.map +1 -1
- package/dist/dts/String.d.ts +81 -111
- package/dist/dts/String.d.ts.map +1 -1
- package/dist/dts/Struct.d.ts +13 -19
- package/dist/dts/Struct.d.ts.map +1 -1
- package/dist/dts/Subscribable.d.ts.map +1 -1
- package/dist/dts/Symbol.d.ts +3 -3
- package/dist/dts/TestAnnotation.d.ts.map +1 -1
- package/dist/dts/TestAnnotationMap.d.ts.map +1 -1
- package/dist/dts/TestAnnotations.d.ts.map +1 -1
- package/dist/dts/TestClock.d.ts +1 -5
- package/dist/dts/TestClock.d.ts.map +1 -1
- package/dist/dts/TestLive.d.ts.map +1 -1
- package/dist/dts/TestSized.d.ts.map +1 -1
- package/dist/dts/Trie.d.ts +28 -44
- package/dist/dts/Trie.d.ts.map +1 -1
- package/dist/dts/Types.d.ts +10 -10
- package/dist/dts/Utils.d.ts.map +1 -1
- package/dist/dts/internal/doNotation.d.ts +2 -0
- package/dist/dts/internal/doNotation.d.ts.map +1 -0
- package/dist/dts/internal/stream.d.ts.map +1 -1
- package/dist/esm/Array.js +10 -13
- package/dist/esm/Array.js.map +1 -1
- package/dist/esm/BigDecimal.js +14 -17
- package/dist/esm/BigDecimal.js.map +1 -1
- package/dist/esm/BigInt.js +41 -46
- package/dist/esm/BigInt.js.map +1 -1
- package/dist/esm/Boolean.js +13 -13
- package/dist/esm/Brand.js +2 -2
- package/dist/esm/Chunk.js +3 -3
- package/dist/esm/Context.js +15 -20
- package/dist/esm/Context.js.map +1 -1
- package/dist/esm/Cron.js +3 -6
- package/dist/esm/Cron.js.map +1 -1
- package/dist/esm/Data.js +9 -15
- package/dist/esm/Data.js.map +1 -1
- package/dist/esm/Duration.js +1 -1
- package/dist/esm/Effect.js +101 -11
- package/dist/esm/Effect.js.map +1 -1
- package/dist/esm/Either.js +141 -61
- package/dist/esm/Either.js.map +1 -1
- package/dist/esm/FiberHandle.js.map +1 -1
- package/dist/esm/FiberMap.js.map +1 -1
- package/dist/esm/FiberSet.js.map +1 -1
- package/dist/esm/Function.js +2 -2
- package/dist/esm/Function.js.map +1 -1
- package/dist/esm/Iterable.js +10 -13
- package/dist/esm/Iterable.js.map +1 -1
- package/dist/esm/List.js +2 -2
- package/dist/esm/Metric.js +5 -7
- package/dist/esm/Metric.js.map +1 -1
- package/dist/esm/Number.js +20 -21
- package/dist/esm/Number.js.map +1 -1
- package/dist/esm/Option.js +249 -164
- package/dist/esm/Option.js.map +1 -1
- package/dist/esm/Order.js +1 -2
- package/dist/esm/Order.js.map +1 -1
- package/dist/esm/Ordering.js +2 -2
- package/dist/esm/Predicate.js +9 -12
- package/dist/esm/Predicate.js.map +1 -1
- package/dist/esm/Readable.js.map +1 -1
- package/dist/esm/Record.js +35 -47
- package/dist/esm/Record.js.map +1 -1
- package/dist/esm/RegExp.js +1 -1
- package/dist/esm/Request.js +2 -2
- package/dist/esm/Request.js.map +1 -1
- package/dist/esm/Stream.js +107 -6
- package/dist/esm/Stream.js.map +1 -1
- package/dist/esm/String.js +81 -111
- package/dist/esm/String.js.map +1 -1
- package/dist/esm/Struct.js +13 -19
- package/dist/esm/Struct.js.map +1 -1
- package/dist/esm/Subscribable.js.map +1 -1
- package/dist/esm/Symbol.js +3 -3
- package/dist/esm/TestAnnotation.js.map +1 -1
- package/dist/esm/TestAnnotationMap.js.map +1 -1
- package/dist/esm/TestAnnotations.js.map +1 -1
- package/dist/esm/TestClock.js.map +1 -1
- package/dist/esm/TestLive.js.map +1 -1
- package/dist/esm/TestSized.js.map +1 -1
- package/dist/esm/Trie.js +28 -44
- package/dist/esm/Trie.js.map +1 -1
- package/dist/esm/Utils.js.map +1 -1
- package/dist/esm/internal/core-effect.js +4 -11
- package/dist/esm/internal/core-effect.js.map +1 -1
- package/dist/esm/internal/doNotation.js +14 -0
- package/dist/esm/internal/doNotation.js.map +1 -0
- package/dist/esm/internal/groupBy.js.map +1 -1
- package/dist/esm/internal/stream.js +3 -7
- package/dist/esm/internal/stream.js.map +1 -1
- package/dist/esm/internal/version.js +1 -1
- package/package.json +1 -1
- package/src/Array.ts +10 -13
- package/src/BigDecimal.ts +14 -17
- package/src/BigInt.ts +41 -46
- package/src/Boolean.ts +13 -13
- package/src/Brand.ts +3 -3
- package/src/Chunk.ts +3 -3
- package/src/Context.ts +15 -20
- package/src/Cron.ts +3 -6
- package/src/Data.ts +10 -16
- package/src/Duration.ts +1 -1
- package/src/Effect.ts +122 -32
- package/src/Either.ts +161 -94
- package/src/FiberHandle.ts +1 -1
- package/src/FiberMap.ts +1 -1
- package/src/FiberSet.ts +1 -1
- package/src/Function.ts +4 -2
- package/src/HashMap.ts +2 -2
- package/src/Iterable.ts +10 -13
- package/src/List.ts +2 -2
- package/src/Metric.ts +5 -7
- package/src/Number.ts +20 -21
- package/src/Option.ts +250 -172
- package/src/Order.ts +1 -2
- package/src/Ordering.ts +2 -2
- package/src/Predicate.ts +9 -12
- package/src/Readable.ts +1 -1
- package/src/Record.ts +35 -47
- package/src/RegExp.ts +1 -1
- package/src/Request.ts +3 -3
- package/src/Stream.ts +137 -36
- package/src/String.ts +81 -111
- package/src/Struct.ts +13 -19
- package/src/Subscribable.ts +1 -1
- package/src/Symbol.ts +3 -3
- package/src/TestAnnotation.ts +1 -1
- package/src/TestAnnotationMap.ts +1 -1
- package/src/TestAnnotations.ts +1 -1
- package/src/TestClock.ts +1 -5
- package/src/TestLive.ts +1 -1
- package/src/TestSized.ts +1 -1
- package/src/Trie.ts +28 -44
- package/src/Types.ts +10 -10
- package/src/Utils.ts +2 -2
- package/src/internal/core-effect.ts +29 -45
- package/src/internal/doNotation.ts +77 -0
- package/src/internal/groupBy.ts +16 -16
- package/src/internal/stream.ts +34 -58
- package/src/internal/version.ts +1 -1
package/src/Option.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type { LazyArg } from "./Function.js"
|
|
|
8
8
|
import { constNull, constUndefined, dual, identity, isFunction } from "./Function.js"
|
|
9
9
|
import type { TypeLambda } from "./HKT.js"
|
|
10
10
|
import type { Inspectable } from "./Inspectable.js"
|
|
11
|
+
import * as doNotation from "./internal/doNotation.js"
|
|
11
12
|
import * as either from "./internal/either.js"
|
|
12
13
|
import * as option from "./internal/option.js"
|
|
13
14
|
import type { Order } from "./Order.js"
|
|
@@ -28,7 +29,7 @@ export type Option<A> = None<A> | Some<A>
|
|
|
28
29
|
* @category symbols
|
|
29
30
|
* @since 2.0.0
|
|
30
31
|
*/
|
|
31
|
-
export const TypeId = Symbol.for("effect/Option")
|
|
32
|
+
export const TypeId: unique symbol = Symbol.for("effect/Option")
|
|
32
33
|
|
|
33
34
|
/**
|
|
34
35
|
* @category symbols
|
|
@@ -124,11 +125,11 @@ export const some: <A>(value: A) => Option<A> = option.some
|
|
|
124
125
|
* @param input - The value to check.
|
|
125
126
|
*
|
|
126
127
|
* @example
|
|
127
|
-
* import {
|
|
128
|
+
* import { Option } from "effect"
|
|
128
129
|
*
|
|
129
|
-
* assert.deepStrictEqual(isOption(some(1)), true)
|
|
130
|
-
* assert.deepStrictEqual(isOption(none()), true)
|
|
131
|
-
* assert.deepStrictEqual(isOption({}), false)
|
|
130
|
+
* assert.deepStrictEqual(Option.isOption(Option.some(1)), true)
|
|
131
|
+
* assert.deepStrictEqual(Option.isOption(Option.none()), true)
|
|
132
|
+
* assert.deepStrictEqual(Option.isOption({}), false)
|
|
132
133
|
*
|
|
133
134
|
* @category guards
|
|
134
135
|
* @since 2.0.0
|
|
@@ -141,10 +142,10 @@ export const isOption: (input: unknown) => input is Option<unknown> = option.isO
|
|
|
141
142
|
* @param self - The `Option` to check.
|
|
142
143
|
*
|
|
143
144
|
* @example
|
|
144
|
-
* import {
|
|
145
|
+
* import { Option } from "effect"
|
|
145
146
|
*
|
|
146
|
-
* assert.deepStrictEqual(isNone(some(1)), false)
|
|
147
|
-
* assert.deepStrictEqual(isNone(none()), true)
|
|
147
|
+
* assert.deepStrictEqual(Option.isNone(Option.some(1)), false)
|
|
148
|
+
* assert.deepStrictEqual(Option.isNone(Option.none()), true)
|
|
148
149
|
*
|
|
149
150
|
* @category guards
|
|
150
151
|
* @since 2.0.0
|
|
@@ -157,10 +158,10 @@ export const isNone: <A>(self: Option<A>) => self is None<A> = option.isNone
|
|
|
157
158
|
* @param self - The `Option` to check.
|
|
158
159
|
*
|
|
159
160
|
* @example
|
|
160
|
-
* import {
|
|
161
|
+
* import { Option } from "effect"
|
|
161
162
|
*
|
|
162
|
-
* assert.deepStrictEqual(isSome(some(1)), true)
|
|
163
|
-
* assert.deepStrictEqual(isSome(none()), false)
|
|
163
|
+
* assert.deepStrictEqual(Option.isSome(Option.some(1)), true)
|
|
164
|
+
* assert.deepStrictEqual(Option.isSome(Option.none()), false)
|
|
164
165
|
*
|
|
165
166
|
* @category guards
|
|
166
167
|
* @since 2.0.0
|
|
@@ -176,16 +177,15 @@ export const isSome: <A>(self: Option<A>) => self is Some<A> = option.isSome
|
|
|
176
177
|
* @param onSome - The function to be called if the `Option` is `Some`, it will be passed the `Option`'s value and its result will be returned
|
|
177
178
|
*
|
|
178
179
|
* @example
|
|
179
|
-
* import {
|
|
180
|
-
* import { pipe } from "effect/Function"
|
|
180
|
+
* import { pipe, Option } from "effect"
|
|
181
181
|
*
|
|
182
182
|
* assert.deepStrictEqual(
|
|
183
|
-
* pipe(some(1), match({ onNone: () => 'a none', onSome: (a) => `a some containing ${a}` })),
|
|
183
|
+
* pipe(Option.some(1), Option.match({ onNone: () => 'a none', onSome: (a) => `a some containing ${a}` })),
|
|
184
184
|
* 'a some containing 1'
|
|
185
185
|
* )
|
|
186
186
|
*
|
|
187
187
|
* assert.deepStrictEqual(
|
|
188
|
-
* pipe(none(), match({ onNone: () => 'a none', onSome: (a) => `a some containing ${a}` })),
|
|
188
|
+
* pipe(Option.none(), Option.match({ onNone: () => 'a none', onSome: (a) => `a some containing ${a}` })),
|
|
189
189
|
* 'a none'
|
|
190
190
|
* )
|
|
191
191
|
*
|
|
@@ -214,12 +214,12 @@ export const match: {
|
|
|
214
214
|
* This function ensures that a type guard definition is type-safe.
|
|
215
215
|
*
|
|
216
216
|
* @example
|
|
217
|
-
* import
|
|
217
|
+
* import { Option } from "effect"
|
|
218
218
|
*
|
|
219
|
-
* const parsePositive = (n: number):
|
|
220
|
-
* n > 0 ?
|
|
219
|
+
* const parsePositive = (n: number): Option.Option<number> =>
|
|
220
|
+
* n > 0 ? Option.some(n) : Option.none()
|
|
221
221
|
*
|
|
222
|
-
* const isPositive =
|
|
222
|
+
* const isPositive = Option.toRefinement(parsePositive)
|
|
223
223
|
*
|
|
224
224
|
* assert.deepStrictEqual(isPositive(1), true)
|
|
225
225
|
* assert.deepStrictEqual(isPositive(-1), false)
|
|
@@ -236,10 +236,10 @@ export const toRefinement = <A, B extends A>(f: (a: A) => Option<B>): (a: A) =>
|
|
|
236
236
|
* @param collection - The `Iterable` to be converted to an `Option`.
|
|
237
237
|
*
|
|
238
238
|
* @example
|
|
239
|
-
* import {
|
|
239
|
+
* import { Option } from "effect"
|
|
240
240
|
*
|
|
241
|
-
* assert.deepStrictEqual(fromIterable([1, 2, 3]), some(1))
|
|
242
|
-
* assert.deepStrictEqual(fromIterable([]), none())
|
|
241
|
+
* assert.deepStrictEqual(Option.fromIterable([1, 2, 3]), Option.some(1))
|
|
242
|
+
* assert.deepStrictEqual(Option.fromIterable([]), Option.none())
|
|
243
243
|
*
|
|
244
244
|
* @category constructors
|
|
245
245
|
* @since 2.0.0
|
|
@@ -257,11 +257,10 @@ export const fromIterable = <A>(collection: Iterable<A>): Option<A> => {
|
|
|
257
257
|
* Alias of {@link fromEither}.
|
|
258
258
|
*
|
|
259
259
|
* @example
|
|
260
|
-
* import
|
|
261
|
-
* import * as E from "effect/Either"
|
|
260
|
+
* import { Option, Either } from "effect"
|
|
262
261
|
*
|
|
263
|
-
* assert.deepStrictEqual(
|
|
264
|
-
* assert.deepStrictEqual(
|
|
262
|
+
* assert.deepStrictEqual(Option.getRight(Either.right('ok')), Option.some('ok'))
|
|
263
|
+
* assert.deepStrictEqual(Option.getRight(Either.left('err')), Option.none())
|
|
265
264
|
*
|
|
266
265
|
* @category conversions
|
|
267
266
|
* @since 2.0.0
|
|
@@ -272,11 +271,10 @@ export const getRight: <R, L>(self: Either<R, L>) => Option<R> = either.getRight
|
|
|
272
271
|
* Converts a `Either` to an `Option` discarding the value.
|
|
273
272
|
*
|
|
274
273
|
* @example
|
|
275
|
-
* import
|
|
276
|
-
* import * as E from "effect/Either"
|
|
274
|
+
* import { Option, Either } from "effect"
|
|
277
275
|
*
|
|
278
|
-
* assert.deepStrictEqual(
|
|
279
|
-
* assert.deepStrictEqual(
|
|
276
|
+
* assert.deepStrictEqual(Option.getLeft(Either.right("ok")), Option.none())
|
|
277
|
+
* assert.deepStrictEqual(Option.getLeft(Either.left("a")), Option.some("a"))
|
|
280
278
|
*
|
|
281
279
|
* @category conversions
|
|
282
280
|
* @since 2.0.0
|
|
@@ -290,11 +288,10 @@ export const getLeft: <R, L>(self: Either<R, L>) => Option<L> = either.getLeft
|
|
|
290
288
|
* @param onNone - Function that returns the default value to return if the `Option` is `None`.
|
|
291
289
|
*
|
|
292
290
|
* @example
|
|
293
|
-
* import {
|
|
294
|
-
* import { pipe } from "effect/Function"
|
|
291
|
+
* import { pipe, Option } from "effect"
|
|
295
292
|
*
|
|
296
|
-
* assert.deepStrictEqual(pipe(some(1), getOrElse(() => 0)), 1)
|
|
297
|
-
* assert.deepStrictEqual(pipe(none(), getOrElse(() => 0)), 0)
|
|
293
|
+
* assert.deepStrictEqual(pipe(Option.some(1), Option.getOrElse(() => 0)), 1)
|
|
294
|
+
* assert.deepStrictEqual(pipe(Option.none(), Option.getOrElse(() => 0)), 0)
|
|
298
295
|
*
|
|
299
296
|
* @category getters
|
|
300
297
|
* @since 2.0.0
|
|
@@ -314,36 +311,35 @@ export const getOrElse: {
|
|
|
314
311
|
* @param that - The `Option` to return if `self` is `None`.
|
|
315
312
|
*
|
|
316
313
|
* @example
|
|
317
|
-
* import
|
|
318
|
-
* import { pipe } from "effect/Function"
|
|
314
|
+
* import { pipe, Option } from "effect"
|
|
319
315
|
*
|
|
320
316
|
* assert.deepStrictEqual(
|
|
321
317
|
* pipe(
|
|
322
|
-
*
|
|
323
|
-
*
|
|
318
|
+
* Option.none(),
|
|
319
|
+
* Option.orElse(() => Option.none())
|
|
324
320
|
* ),
|
|
325
|
-
*
|
|
321
|
+
* Option.none()
|
|
326
322
|
* )
|
|
327
323
|
* assert.deepStrictEqual(
|
|
328
324
|
* pipe(
|
|
329
|
-
*
|
|
330
|
-
*
|
|
325
|
+
* Option.some('a'),
|
|
326
|
+
* Option.orElse(() => Option.none())
|
|
331
327
|
* ),
|
|
332
|
-
*
|
|
328
|
+
* Option.some('a')
|
|
333
329
|
* )
|
|
334
330
|
* assert.deepStrictEqual(
|
|
335
331
|
* pipe(
|
|
336
|
-
*
|
|
337
|
-
*
|
|
332
|
+
* Option.none(),
|
|
333
|
+
* Option.orElse(() => Option.some('b'))
|
|
338
334
|
* ),
|
|
339
|
-
*
|
|
335
|
+
* Option.some('b')
|
|
340
336
|
* )
|
|
341
337
|
* assert.deepStrictEqual(
|
|
342
338
|
* pipe(
|
|
343
|
-
*
|
|
344
|
-
*
|
|
339
|
+
* Option.some('a'),
|
|
340
|
+
* Option.orElse(() => Option.some('b'))
|
|
345
341
|
* ),
|
|
346
|
-
*
|
|
342
|
+
* Option.some('a')
|
|
347
343
|
* )
|
|
348
344
|
*
|
|
349
345
|
* @category error handling
|
|
@@ -364,22 +360,21 @@ export const orElse: {
|
|
|
364
360
|
* @param onNone - Function that returns the default value to return if the `Option` is `None`.
|
|
365
361
|
*
|
|
366
362
|
* @example
|
|
367
|
-
* import
|
|
368
|
-
* import { pipe } from "effect/Function"
|
|
363
|
+
* import { pipe, Option } from "effect"
|
|
369
364
|
*
|
|
370
365
|
* assert.deepStrictEqual(
|
|
371
366
|
* pipe(
|
|
372
|
-
*
|
|
373
|
-
*
|
|
367
|
+
* Option.none(),
|
|
368
|
+
* Option.orElseSome(() => 'b')
|
|
374
369
|
* ),
|
|
375
|
-
*
|
|
370
|
+
* Option.some('b')
|
|
376
371
|
* )
|
|
377
372
|
* assert.deepStrictEqual(
|
|
378
373
|
* pipe(
|
|
379
|
-
*
|
|
380
|
-
*
|
|
374
|
+
* Option.some('a'),
|
|
375
|
+
* Option.orElseSome(() => 'b')
|
|
381
376
|
* ),
|
|
382
|
-
*
|
|
377
|
+
* Option.some('a')
|
|
383
378
|
* )
|
|
384
379
|
*
|
|
385
380
|
* @category error handling
|
|
@@ -420,9 +415,9 @@ export const orElseEither: {
|
|
|
420
415
|
* @param collection - An iterable collection of `Option` to be searched.
|
|
421
416
|
*
|
|
422
417
|
* @example
|
|
423
|
-
* import
|
|
418
|
+
* import { Option } from "effect"
|
|
424
419
|
*
|
|
425
|
-
* assert.deepStrictEqual(
|
|
420
|
+
* assert.deepStrictEqual(Option.firstSomeOf([Option.none(), Option.some(1), Option.some(2)]), Option.some(1))
|
|
426
421
|
*
|
|
427
422
|
* @category error handling
|
|
428
423
|
* @since 2.0.0
|
|
@@ -444,11 +439,11 @@ export const firstSomeOf = <A>(collection: Iterable<Option<A>>): Option<A> => {
|
|
|
444
439
|
* @param nullableValue - The nullable value to be converted to an `Option`.
|
|
445
440
|
*
|
|
446
441
|
* @example
|
|
447
|
-
* import
|
|
442
|
+
* import { Option } from "effect"
|
|
448
443
|
*
|
|
449
|
-
* assert.deepStrictEqual(
|
|
450
|
-
* assert.deepStrictEqual(
|
|
451
|
-
* assert.deepStrictEqual(
|
|
444
|
+
* assert.deepStrictEqual(Option.fromNullable(undefined), Option.none())
|
|
445
|
+
* assert.deepStrictEqual(Option.fromNullable(null), Option.none())
|
|
446
|
+
* assert.deepStrictEqual(Option.fromNullable(1), Option.some(1))
|
|
452
447
|
*
|
|
453
448
|
* @category conversions
|
|
454
449
|
* @since 2.0.0
|
|
@@ -463,17 +458,17 @@ export const fromNullable = <A>(
|
|
|
463
458
|
* This API is useful for lifting a function that returns `null` or `undefined` into the `Option` context.
|
|
464
459
|
*
|
|
465
460
|
* @example
|
|
466
|
-
* import
|
|
461
|
+
* import { Option } from "effect"
|
|
467
462
|
*
|
|
468
463
|
* const parse = (s: string): number | undefined => {
|
|
469
464
|
* const n = parseFloat(s)
|
|
470
465
|
* return isNaN(n) ? undefined : n
|
|
471
466
|
* }
|
|
472
467
|
*
|
|
473
|
-
* const parseOption =
|
|
468
|
+
* const parseOption = Option.liftNullable(parse)
|
|
474
469
|
*
|
|
475
|
-
* assert.deepStrictEqual(parseOption('1'),
|
|
476
|
-
* assert.deepStrictEqual(parseOption('not a number'),
|
|
470
|
+
* assert.deepStrictEqual(parseOption('1'), Option.some(1))
|
|
471
|
+
* assert.deepStrictEqual(parseOption('not a number'), Option.none())
|
|
477
472
|
*
|
|
478
473
|
* @category conversions
|
|
479
474
|
* @since 2.0.0
|
|
@@ -489,10 +484,10 @@ export const liftNullable = <A extends ReadonlyArray<unknown>, B>(
|
|
|
489
484
|
* @param self - The `Option` to extract the value from.
|
|
490
485
|
*
|
|
491
486
|
* @example
|
|
492
|
-
* import
|
|
487
|
+
* import { Option } from "effect"
|
|
493
488
|
*
|
|
494
|
-
* assert.deepStrictEqual(
|
|
495
|
-
* assert.deepStrictEqual(
|
|
489
|
+
* assert.deepStrictEqual(Option.getOrNull(Option.some(1)), 1)
|
|
490
|
+
* assert.deepStrictEqual(Option.getOrNull(Option.none()), null)
|
|
496
491
|
*
|
|
497
492
|
* @category getters
|
|
498
493
|
* @since 2.0.0
|
|
@@ -505,10 +500,10 @@ export const getOrNull: <A>(self: Option<A>) => A | null = getOrElse(constNull)
|
|
|
505
500
|
* @param self - The `Option` to extract the value from.
|
|
506
501
|
*
|
|
507
502
|
* @example
|
|
508
|
-
* import
|
|
503
|
+
* import { Option } from "effect"
|
|
509
504
|
*
|
|
510
|
-
* assert.deepStrictEqual(
|
|
511
|
-
* assert.deepStrictEqual(
|
|
505
|
+
* assert.deepStrictEqual(Option.getOrUndefined(Option.some(1)), 1)
|
|
506
|
+
* assert.deepStrictEqual(Option.getOrUndefined(Option.none()), undefined)
|
|
512
507
|
*
|
|
513
508
|
* @category getters
|
|
514
509
|
* @since 2.0.0
|
|
@@ -524,12 +519,12 @@ export const getOrUndefined: <A>(self: Option<A>) => A | undefined = getOrElse(c
|
|
|
524
519
|
* @param f - the function that can throw exceptions.
|
|
525
520
|
*
|
|
526
521
|
* @example
|
|
527
|
-
* import
|
|
522
|
+
* import { Option } from "effect"
|
|
528
523
|
*
|
|
529
|
-
* const parse =
|
|
524
|
+
* const parse = Option.liftThrowable(JSON.parse)
|
|
530
525
|
*
|
|
531
|
-
* assert.deepStrictEqual(parse("1"),
|
|
532
|
-
* assert.deepStrictEqual(parse(""),
|
|
526
|
+
* assert.deepStrictEqual(parse("1"), Option.some(1))
|
|
527
|
+
* assert.deepStrictEqual(parse(""), Option.none())
|
|
533
528
|
*
|
|
534
529
|
* @category conversions
|
|
535
530
|
* @since 2.0.0
|
|
@@ -554,13 +549,13 @@ export const liftThrowable = <A extends ReadonlyArray<unknown>, B>(
|
|
|
554
549
|
* @param onNone - A function that will be called if the `Option` is `None`. It returns the error to be thrown.
|
|
555
550
|
*
|
|
556
551
|
* @example
|
|
557
|
-
* import
|
|
552
|
+
* import { Option } from "effect"
|
|
558
553
|
*
|
|
559
554
|
* assert.deepStrictEqual(
|
|
560
|
-
*
|
|
555
|
+
* Option.getOrThrowWith(Option.some(1), () => new Error('Unexpected None')),
|
|
561
556
|
* 1
|
|
562
557
|
* )
|
|
563
|
-
* assert.throws(() =>
|
|
558
|
+
* assert.throws(() => Option.getOrThrowWith(Option.none(), () => new Error('Unexpected None')))
|
|
564
559
|
*
|
|
565
560
|
* @category conversions
|
|
566
561
|
* @since 2.0.0
|
|
@@ -584,10 +579,10 @@ export const getOrThrowWith: {
|
|
|
584
579
|
* @throws `Error("getOrThrow called on a None")`
|
|
585
580
|
*
|
|
586
581
|
* @example
|
|
587
|
-
* import
|
|
582
|
+
* import { Option } from "effect"
|
|
588
583
|
*
|
|
589
|
-
* assert.deepStrictEqual(
|
|
590
|
-
* assert.throws(() =>
|
|
584
|
+
* assert.deepStrictEqual(Option.getOrThrow(Option.some(1)), 1)
|
|
585
|
+
* assert.throws(() => Option.getOrThrow(Option.none()))
|
|
591
586
|
*
|
|
592
587
|
* @category conversions
|
|
593
588
|
* @since 2.0.0
|
|
@@ -681,8 +676,7 @@ export const andThen: {
|
|
|
681
676
|
* This is `flatMap` + `fromNullable`, useful when working with optional values.
|
|
682
677
|
*
|
|
683
678
|
* @example
|
|
684
|
-
* import {
|
|
685
|
-
* import { pipe } from "effect/Function"
|
|
679
|
+
* import { pipe, Option } from "effect"
|
|
686
680
|
*
|
|
687
681
|
* interface Employee {
|
|
688
682
|
* company?: {
|
|
@@ -698,20 +692,20 @@ export const andThen: {
|
|
|
698
692
|
*
|
|
699
693
|
* assert.deepStrictEqual(
|
|
700
694
|
* pipe(
|
|
701
|
-
* some(employee1),
|
|
702
|
-
* flatMapNullable(employee => employee.company?.address?.street?.name),
|
|
695
|
+
* Option.some(employee1),
|
|
696
|
+
* Option.flatMapNullable(employee => employee.company?.address?.street?.name),
|
|
703
697
|
* ),
|
|
704
|
-
* some('high street')
|
|
698
|
+
* Option.some('high street')
|
|
705
699
|
* )
|
|
706
700
|
*
|
|
707
701
|
* const employee2: Employee = { company: { address: { street: {} } } }
|
|
708
702
|
*
|
|
709
703
|
* assert.deepStrictEqual(
|
|
710
704
|
* pipe(
|
|
711
|
-
* some(employee2),
|
|
712
|
-
* flatMapNullable(employee => employee.company?.address?.street?.name),
|
|
705
|
+
* Option.some(employee2),
|
|
706
|
+
* Option.flatMapNullable(employee => employee.company?.address?.street?.name),
|
|
713
707
|
* ),
|
|
714
|
-
* none()
|
|
708
|
+
* Option.none()
|
|
715
709
|
* )
|
|
716
710
|
*
|
|
717
711
|
* @category sequencing
|
|
@@ -776,13 +770,13 @@ export const zipLeft: {
|
|
|
776
770
|
* @param self - The `Option` to apply the function to
|
|
777
771
|
*
|
|
778
772
|
* @example
|
|
779
|
-
* import
|
|
773
|
+
* import { Option } from "effect"
|
|
780
774
|
*
|
|
781
|
-
* const getInteger = (n: number) => Number.isInteger(n) ?
|
|
775
|
+
* const getInteger = (n: number) => Number.isInteger(n) ? Option.some(n) : Option.none()
|
|
782
776
|
*
|
|
783
|
-
* assert.deepStrictEqual(
|
|
784
|
-
* assert.deepStrictEqual(
|
|
785
|
-
* assert.deepStrictEqual(
|
|
777
|
+
* assert.deepStrictEqual(Option.tap(Option.none(), getInteger), Option.none())
|
|
778
|
+
* assert.deepStrictEqual(Option.tap(Option.some(1), getInteger), Option.some(1))
|
|
779
|
+
* assert.deepStrictEqual(Option.tap(Option.some(1.14), getInteger), Option.none())
|
|
786
780
|
*
|
|
787
781
|
* @category sequencing
|
|
788
782
|
* @since 2.0.0
|
|
@@ -830,11 +824,11 @@ export const productMany = <A>(
|
|
|
830
824
|
* @param fields - the struct of `Option`s to be sequenced.
|
|
831
825
|
*
|
|
832
826
|
* @example
|
|
833
|
-
* import
|
|
827
|
+
* import { Option } from "effect"
|
|
834
828
|
*
|
|
835
|
-
* assert.deepStrictEqual(
|
|
836
|
-
* assert.deepStrictEqual(
|
|
837
|
-
* assert.deepStrictEqual(
|
|
829
|
+
* assert.deepStrictEqual(Option.all([Option.some(1), Option.some(2)]), Option.some([1, 2]))
|
|
830
|
+
* assert.deepStrictEqual(Option.all({ a: Option.some(1), b: Option.some("hello") }), Option.some({ a: 1, b: "hello" }))
|
|
831
|
+
* assert.deepStrictEqual(Option.all({ a: Option.some(1), b: Option.none() }), Option.none())
|
|
838
832
|
*
|
|
839
833
|
* @category combining
|
|
840
834
|
* @since 2.0.0
|
|
@@ -879,18 +873,18 @@ export const all: <const I extends Iterable<Option<any>> | Record<string, Option
|
|
|
879
873
|
* @param f - The function used to combine the values of the two `Option`s
|
|
880
874
|
*
|
|
881
875
|
* @example
|
|
882
|
-
* import
|
|
876
|
+
* import { Option } from "effect"
|
|
883
877
|
*
|
|
884
878
|
* type Complex = [real: number, imaginary: number]
|
|
885
879
|
*
|
|
886
880
|
* const complex = (real: number, imaginary: number): Complex => [real, imaginary]
|
|
887
881
|
*
|
|
888
|
-
* assert.deepStrictEqual(
|
|
889
|
-
* assert.deepStrictEqual(
|
|
890
|
-
* assert.deepStrictEqual(
|
|
891
|
-
* assert.deepStrictEqual(
|
|
882
|
+
* assert.deepStrictEqual(Option.zipWith(Option.none(), Option.none(), complex), Option.none())
|
|
883
|
+
* assert.deepStrictEqual(Option.zipWith(Option.some(1), Option.none(), complex), Option.none())
|
|
884
|
+
* assert.deepStrictEqual(Option.zipWith(Option.none(), Option.some(1), complex), Option.none())
|
|
885
|
+
* assert.deepStrictEqual(Option.zipWith(Option.some(1), Option.some(2), complex), Option.some([1, 2]))
|
|
892
886
|
*
|
|
893
|
-
* assert.deepStrictEqual(
|
|
887
|
+
* assert.deepStrictEqual(Option.zipWith(Option.some(1), complex)(Option.some(2)), Option.some([2, 1]))
|
|
894
888
|
*
|
|
895
889
|
* @category zipping
|
|
896
890
|
* @since 2.0.0
|
|
@@ -921,11 +915,10 @@ export const ap: {
|
|
|
921
915
|
* @param f - The reducing function that takes the current accumulator value and the unwrapped value of an `Option<A>`.
|
|
922
916
|
*
|
|
923
917
|
* @example
|
|
924
|
-
* import {
|
|
925
|
-
* import { pipe } from "effect/Function"
|
|
918
|
+
* import { pipe, Option } from "effect"
|
|
926
919
|
*
|
|
927
|
-
* const iterable = [some(1), none(), some(2), none()]
|
|
928
|
-
* assert.deepStrictEqual(pipe(iterable, reduceCompact(0, (b, a) => b + a)), 3)
|
|
920
|
+
* const iterable = [Option.some(1), Option.none(), Option.some(2), Option.none()]
|
|
921
|
+
* assert.deepStrictEqual(pipe(iterable, Option.reduceCompact(0, (b, a) => b + a)), 3)
|
|
929
922
|
*
|
|
930
923
|
* @category folding
|
|
931
924
|
* @since 2.0.0
|
|
@@ -954,10 +947,10 @@ export const reduceCompact: {
|
|
|
954
947
|
* @param self - The `Option` to convert to an array.
|
|
955
948
|
*
|
|
956
949
|
* @example
|
|
957
|
-
* import
|
|
950
|
+
* import { Option } from "effect"
|
|
958
951
|
*
|
|
959
|
-
* assert.deepStrictEqual(
|
|
960
|
-
* assert.deepStrictEqual(
|
|
952
|
+
* assert.deepStrictEqual(Option.toArray(Option.some(1)), [1])
|
|
953
|
+
* assert.deepStrictEqual(Option.toArray(Option.none()), [])
|
|
961
954
|
*
|
|
962
955
|
* @category conversions
|
|
963
956
|
* @since 2.0.0
|
|
@@ -991,13 +984,13 @@ export const partitionMap: {
|
|
|
991
984
|
* @param f - A function to apply to the value of the `Option`.
|
|
992
985
|
*
|
|
993
986
|
* @example
|
|
994
|
-
* import
|
|
987
|
+
* import { Option } from "effect"
|
|
995
988
|
*
|
|
996
|
-
* const evenNumber = (n: number) => n % 2 === 0 ?
|
|
989
|
+
* const evenNumber = (n: number) => n % 2 === 0 ? Option.some(n) : Option.none()
|
|
997
990
|
*
|
|
998
|
-
* assert.deepStrictEqual(
|
|
999
|
-
* assert.deepStrictEqual(
|
|
1000
|
-
* assert.deepStrictEqual(
|
|
991
|
+
* assert.deepStrictEqual(Option.filterMap(Option.none(), evenNumber), Option.none())
|
|
992
|
+
* assert.deepStrictEqual(Option.filterMap(Option.some(3), evenNumber), Option.none())
|
|
993
|
+
* assert.deepStrictEqual(Option.filterMap(Option.some(2), evenNumber), Option.some(2))
|
|
1001
994
|
*
|
|
1002
995
|
* @category filtering
|
|
1003
996
|
* @since 2.0.0
|
|
@@ -1019,21 +1012,21 @@ export const filterMap: {
|
|
|
1019
1012
|
* @param fb - The `Option` to filter.
|
|
1020
1013
|
*
|
|
1021
1014
|
* @example
|
|
1022
|
-
* import
|
|
1015
|
+
* import { Option } from "effect"
|
|
1023
1016
|
*
|
|
1024
1017
|
* // predicate
|
|
1025
1018
|
* const isEven = (n: number) => n % 2 === 0
|
|
1026
1019
|
*
|
|
1027
|
-
* assert.deepStrictEqual(
|
|
1028
|
-
* assert.deepStrictEqual(
|
|
1029
|
-
* assert.deepStrictEqual(
|
|
1020
|
+
* assert.deepStrictEqual(Option.filter(Option.none(), isEven), Option.none())
|
|
1021
|
+
* assert.deepStrictEqual(Option.filter(Option.some(3), isEven), Option.none())
|
|
1022
|
+
* assert.deepStrictEqual(Option.filter(Option.some(2), isEven), Option.some(2))
|
|
1030
1023
|
*
|
|
1031
1024
|
* // refinement
|
|
1032
1025
|
* const isNumber = (v: unknown): v is number => typeof v === "number"
|
|
1033
1026
|
*
|
|
1034
|
-
* assert.deepStrictEqual(
|
|
1035
|
-
* assert.deepStrictEqual(
|
|
1036
|
-
* assert.deepStrictEqual(
|
|
1027
|
+
* assert.deepStrictEqual(Option.filter(Option.none(), isNumber), Option.none())
|
|
1028
|
+
* assert.deepStrictEqual(Option.filter(Option.some('hello'), isNumber), Option.none())
|
|
1029
|
+
* assert.deepStrictEqual(Option.filter(Option.some(2), isNumber), Option.some(2))
|
|
1037
1030
|
*
|
|
1038
1031
|
* @category filtering
|
|
1039
1032
|
* @since 2.0.0
|
|
@@ -1051,15 +1044,14 @@ export const filter: {
|
|
|
1051
1044
|
|
|
1052
1045
|
/**
|
|
1053
1046
|
* @example
|
|
1054
|
-
* import {
|
|
1055
|
-
* import * as N from 'effect/Number'
|
|
1047
|
+
* import { Option, Number } from "effect"
|
|
1056
1048
|
*
|
|
1057
|
-
* const isEquivalent = getEquivalence(
|
|
1058
|
-
* assert.deepStrictEqual(isEquivalent(none(), none()), true)
|
|
1059
|
-
* assert.deepStrictEqual(isEquivalent(none(), some(1)), false)
|
|
1060
|
-
* assert.deepStrictEqual(isEquivalent(some(1), none()), false)
|
|
1061
|
-
* assert.deepStrictEqual(isEquivalent(some(1), some(2)), false)
|
|
1062
|
-
* assert.deepStrictEqual(isEquivalent(some(1), some(1)), true)
|
|
1049
|
+
* const isEquivalent = Option.getEquivalence(Number.Equivalence)
|
|
1050
|
+
* assert.deepStrictEqual(isEquivalent(Option.none(), Option.none()), true)
|
|
1051
|
+
* assert.deepStrictEqual(isEquivalent(Option.none(), Option.some(1)), false)
|
|
1052
|
+
* assert.deepStrictEqual(isEquivalent(Option.some(1), Option.none()), false)
|
|
1053
|
+
* assert.deepStrictEqual(isEquivalent(Option.some(1), Option.some(2)), false)
|
|
1054
|
+
* assert.deepStrictEqual(isEquivalent(Option.some(1), Option.some(1)), true)
|
|
1063
1055
|
*
|
|
1064
1056
|
* @category equivalence
|
|
1065
1057
|
* @since 2.0.0
|
|
@@ -1075,16 +1067,14 @@ export const getEquivalence = <A>(isEquivalent: Equivalence.Equivalence<A>): Equ
|
|
|
1075
1067
|
* `None` is considered to be less than any `Some` value.
|
|
1076
1068
|
*
|
|
1077
1069
|
* @example
|
|
1078
|
-
* import {
|
|
1079
|
-
* import * as N from 'effect/Number'
|
|
1080
|
-
* import { pipe } from "effect/Function"
|
|
1070
|
+
* import { pipe, Option, Number } from "effect"
|
|
1081
1071
|
*
|
|
1082
|
-
* const O = getOrder(
|
|
1083
|
-
* assert.deepStrictEqual(O(none(), none()), 0)
|
|
1084
|
-
* assert.deepStrictEqual(O(none(), some(1)), -1)
|
|
1085
|
-
* assert.deepStrictEqual(O(some(1), none()), 1)
|
|
1086
|
-
* assert.deepStrictEqual(O(some(1), some(2)), -1)
|
|
1087
|
-
* assert.deepStrictEqual(O(some(1), some(1)), 0)
|
|
1072
|
+
* const O = Option.getOrder(Number.Order)
|
|
1073
|
+
* assert.deepStrictEqual(O(Option.none(), Option.none()), 0)
|
|
1074
|
+
* assert.deepStrictEqual(O(Option.none(), Option.some(1)), -1)
|
|
1075
|
+
* assert.deepStrictEqual(O(Option.some(1), Option.none()), 1)
|
|
1076
|
+
* assert.deepStrictEqual(O(Option.some(1), Option.some(2)), -1)
|
|
1077
|
+
* assert.deepStrictEqual(O(Option.some(1), Option.some(1)), 0)
|
|
1088
1078
|
*
|
|
1089
1079
|
* @category sorting
|
|
1090
1080
|
* @since 2.0.0
|
|
@@ -1112,12 +1102,12 @@ export const lift2 = <A, B, C>(f: (a: A, b: B) => C): {
|
|
|
1112
1102
|
* @param predicate - A `Predicate` function that takes in a value of type `A` and returns a boolean.
|
|
1113
1103
|
*
|
|
1114
1104
|
* @example
|
|
1115
|
-
* import
|
|
1105
|
+
* import { Option } from "effect"
|
|
1116
1106
|
*
|
|
1117
|
-
* const getOption =
|
|
1107
|
+
* const getOption = Option.liftPredicate((n: number) => n >= 0)
|
|
1118
1108
|
*
|
|
1119
|
-
* assert.deepStrictEqual(getOption(-1),
|
|
1120
|
-
* assert.deepStrictEqual(getOption(1),
|
|
1109
|
+
* assert.deepStrictEqual(getOption(-1), Option.none())
|
|
1110
|
+
* assert.deepStrictEqual(getOption(1), Option.some(1))
|
|
1121
1111
|
*
|
|
1122
1112
|
* @category lifting
|
|
1123
1113
|
* @since 2.0.0
|
|
@@ -1135,13 +1125,11 @@ export const liftPredicate: { // Note: I intentionally avoid using the NoInfer p
|
|
|
1135
1125
|
* @param a - The value to compare against the `Option`.
|
|
1136
1126
|
*
|
|
1137
1127
|
* @example
|
|
1138
|
-
* import {
|
|
1139
|
-
* import { Equivalence } from 'effect/Number'
|
|
1140
|
-
* import { pipe } from "effect/Function"
|
|
1128
|
+
* import { pipe, Option, Number } from "effect"
|
|
1141
1129
|
*
|
|
1142
|
-
* assert.deepStrictEqual(pipe(some(2), containsWith(Equivalence)(2)), true)
|
|
1143
|
-
* assert.deepStrictEqual(pipe(some(1), containsWith(Equivalence)(2)), false)
|
|
1144
|
-
* assert.deepStrictEqual(pipe(none(), containsWith(Equivalence)(2)), false)
|
|
1130
|
+
* assert.deepStrictEqual(pipe(Option.some(2), Option.containsWith(Number.Equivalence)(2)), true)
|
|
1131
|
+
* assert.deepStrictEqual(pipe(Option.some(1), Option.containsWith(Number.Equivalence)(2)), false)
|
|
1132
|
+
* assert.deepStrictEqual(pipe(Option.none(), Option.containsWith(Number.Equivalence)(2)), false)
|
|
1145
1133
|
*
|
|
1146
1134
|
* @category elements
|
|
1147
1135
|
* @since 2.0.0
|
|
@@ -1171,14 +1159,13 @@ export const contains: {
|
|
|
1171
1159
|
* @param predicate - The condition to check.
|
|
1172
1160
|
*
|
|
1173
1161
|
* @example
|
|
1174
|
-
* import {
|
|
1175
|
-
* import { pipe } from "effect/Function"
|
|
1162
|
+
* import { pipe, Option } from "effect"
|
|
1176
1163
|
*
|
|
1177
1164
|
* const isEven = (n: number) => n % 2 === 0
|
|
1178
1165
|
*
|
|
1179
|
-
* assert.deepStrictEqual(pipe(some(2), exists(isEven)), true)
|
|
1180
|
-
* assert.deepStrictEqual(pipe(some(1), exists(isEven)), false)
|
|
1181
|
-
* assert.deepStrictEqual(pipe(none(), exists(isEven)), false)
|
|
1166
|
+
* assert.deepStrictEqual(pipe(Option.some(2), Option.exists(isEven)), true)
|
|
1167
|
+
* assert.deepStrictEqual(pipe(Option.some(1), Option.exists(isEven)), false)
|
|
1168
|
+
* assert.deepStrictEqual(pipe(Option.none(), Option.exists(isEven)), false)
|
|
1182
1169
|
*
|
|
1183
1170
|
* @since 2.0.0
|
|
1184
1171
|
*/
|
|
@@ -1198,16 +1185,39 @@ export const exists: {
|
|
|
1198
1185
|
// -------------------------------------------------------------------------------------
|
|
1199
1186
|
|
|
1200
1187
|
/**
|
|
1188
|
+
* The "do simulation" in allows you to write code in a more declarative style, similar to the "do notation" in other programming languages. It provides a way to define variables and perform operations on them using functions like `bind` and `let`.
|
|
1189
|
+
*
|
|
1190
|
+
* Here's how the do simulation works:
|
|
1191
|
+
*
|
|
1192
|
+
* 1. Start the do simulation using the `Do` value
|
|
1193
|
+
* 2. Within the do simulation scope, you can use the `bind` function to define variables and bind them to `Option` values
|
|
1194
|
+
* 3. You can accumulate multiple `bind` statements to define multiple variables within the scope
|
|
1195
|
+
* 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values
|
|
1196
|
+
* 5. Regular `Option` functions like `map` and `filter` can still be used within the do simulation. These functions will receive the accumulated variables as arguments within the scope
|
|
1197
|
+
*
|
|
1198
|
+
* @see {@link Do}
|
|
1199
|
+
* @see {@link bind}
|
|
1200
|
+
* @see {@link let_ let}
|
|
1201
|
+
*
|
|
1202
|
+
* @example
|
|
1203
|
+
* import { Option, pipe } from "effect"
|
|
1204
|
+
*
|
|
1205
|
+
* const result = pipe(
|
|
1206
|
+
* Option.Do,
|
|
1207
|
+
* Option.bind("x", () => Option.some(2)),
|
|
1208
|
+
* Option.bind("y", () => Option.some(3)),
|
|
1209
|
+
* Option.let("sum", ({ x, y }) => x + y),
|
|
1210
|
+
* Option.filter(({ x, y }) => x * y > 5)
|
|
1211
|
+
* )
|
|
1212
|
+
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
1213
|
+
*
|
|
1201
1214
|
* @category do notation
|
|
1202
1215
|
* @since 2.0.0
|
|
1203
1216
|
*/
|
|
1204
1217
|
export const bindTo: {
|
|
1205
1218
|
<N extends string>(name: N): <A>(self: Option<A>) => Option<{ [K in N]: A }>
|
|
1206
1219
|
<A, N extends string>(self: Option<A>, name: N): Option<{ [K in N]: A }>
|
|
1207
|
-
} =
|
|
1208
|
-
2,
|
|
1209
|
-
<A, N extends string>(self: Option<A>, name: N): Option<{ [K in N]: A }> => map(self, (a) => ({ [name]: a } as any))
|
|
1210
|
-
)
|
|
1220
|
+
} = doNotation.bindTo<OptionTypeLambda>(map)
|
|
1211
1221
|
|
|
1212
1222
|
const let_: {
|
|
1213
1223
|
<N extends string, A extends object, B>(
|
|
@@ -1219,15 +1229,36 @@ const let_: {
|
|
|
1219
1229
|
name: Exclude<N, keyof A>,
|
|
1220
1230
|
f: (a: A) => B
|
|
1221
1231
|
): Option<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }>
|
|
1222
|
-
} =
|
|
1223
|
-
self: Option<A>,
|
|
1224
|
-
name: Exclude<N, keyof A>,
|
|
1225
|
-
f: (a: A) => B
|
|
1226
|
-
): Option<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }> =>
|
|
1227
|
-
map(self, (a) => Object.assign({}, a, { [name]: f(a) }) as any))
|
|
1232
|
+
} = doNotation.let_<OptionTypeLambda>(map)
|
|
1228
1233
|
|
|
1229
1234
|
export {
|
|
1230
1235
|
/**
|
|
1236
|
+
* The "do simulation" in allows you to write code in a more declarative style, similar to the "do notation" in other programming languages. It provides a way to define variables and perform operations on them using functions like `bind` and `let`.
|
|
1237
|
+
*
|
|
1238
|
+
* Here's how the do simulation works:
|
|
1239
|
+
*
|
|
1240
|
+
* 1. Start the do simulation using the `Do` value
|
|
1241
|
+
* 2. Within the do simulation scope, you can use the `bind` function to define variables and bind them to `Option` values
|
|
1242
|
+
* 3. You can accumulate multiple `bind` statements to define multiple variables within the scope
|
|
1243
|
+
* 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values
|
|
1244
|
+
* 5. Regular `Option` functions like `map` and `filter` can still be used within the do simulation. These functions will receive the accumulated variables as arguments within the scope
|
|
1245
|
+
*
|
|
1246
|
+
* @see {@link Do}
|
|
1247
|
+
* @see {@link bind}
|
|
1248
|
+
* @see {@link bindTo}
|
|
1249
|
+
*
|
|
1250
|
+
* @example
|
|
1251
|
+
* import { Option, pipe } from "effect"
|
|
1252
|
+
*
|
|
1253
|
+
* const result = pipe(
|
|
1254
|
+
* Option.Do,
|
|
1255
|
+
* Option.bind("x", () => Option.some(2)),
|
|
1256
|
+
* Option.bind("y", () => Option.some(3)),
|
|
1257
|
+
* Option.let("sum", ({ x, y }) => x + y),
|
|
1258
|
+
* Option.filter(({ x, y }) => x * y > 5)
|
|
1259
|
+
* )
|
|
1260
|
+
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
1261
|
+
*
|
|
1231
1262
|
* @category do notation
|
|
1232
1263
|
* @since 2.0.0
|
|
1233
1264
|
*/
|
|
@@ -1235,6 +1266,32 @@ export {
|
|
|
1235
1266
|
}
|
|
1236
1267
|
|
|
1237
1268
|
/**
|
|
1269
|
+
* The "do simulation" in allows you to write code in a more declarative style, similar to the "do notation" in other programming languages. It provides a way to define variables and perform operations on them using functions like `bind` and `let`.
|
|
1270
|
+
*
|
|
1271
|
+
* Here's how the do simulation works:
|
|
1272
|
+
*
|
|
1273
|
+
* 1. Start the do simulation using the `Do` value
|
|
1274
|
+
* 2. Within the do simulation scope, you can use the `bind` function to define variables and bind them to `Option` values
|
|
1275
|
+
* 3. You can accumulate multiple `bind` statements to define multiple variables within the scope
|
|
1276
|
+
* 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values
|
|
1277
|
+
* 5. Regular `Option` functions like `map` and `filter` can still be used within the do simulation. These functions will receive the accumulated variables as arguments within the scope
|
|
1278
|
+
*
|
|
1279
|
+
* @see {@link Do}
|
|
1280
|
+
* @see {@link bindTo}
|
|
1281
|
+
* @see {@link let_ let}
|
|
1282
|
+
*
|
|
1283
|
+
* @example
|
|
1284
|
+
* import { Option, pipe } from "effect"
|
|
1285
|
+
*
|
|
1286
|
+
* const result = pipe(
|
|
1287
|
+
* Option.Do,
|
|
1288
|
+
* Option.bind("x", () => Option.some(2)),
|
|
1289
|
+
* Option.bind("y", () => Option.some(3)),
|
|
1290
|
+
* Option.let("sum", ({ x, y }) => x + y),
|
|
1291
|
+
* Option.filter(({ x, y }) => x * y > 5)
|
|
1292
|
+
* )
|
|
1293
|
+
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
1294
|
+
*
|
|
1238
1295
|
* @category do notation
|
|
1239
1296
|
* @since 2.0.0
|
|
1240
1297
|
*/
|
|
@@ -1248,14 +1305,35 @@ export const bind: {
|
|
|
1248
1305
|
name: Exclude<N, keyof A>,
|
|
1249
1306
|
f: (a: A) => Option<B>
|
|
1250
1307
|
): Option<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }>
|
|
1251
|
-
} =
|
|
1252
|
-
self: Option<A>,
|
|
1253
|
-
name: Exclude<N, keyof A>,
|
|
1254
|
-
f: (a: A) => Option<B>
|
|
1255
|
-
): Option<{ [K in keyof A | N]: K extends keyof A ? A[K] : B }> =>
|
|
1256
|
-
flatMap(self, (a) => map(f(a), (b) => Object.assign({}, a, { [name]: b }) as any)))
|
|
1308
|
+
} = doNotation.bind<OptionTypeLambda>(map, flatMap)
|
|
1257
1309
|
|
|
1258
1310
|
/**
|
|
1311
|
+
* The "do simulation" in allows you to write code in a more declarative style, similar to the "do notation" in other programming languages. It provides a way to define variables and perform operations on them using functions like `bind` and `let`.
|
|
1312
|
+
*
|
|
1313
|
+
* Here's how the do simulation works:
|
|
1314
|
+
*
|
|
1315
|
+
* 1. Start the do simulation using the `Do` value
|
|
1316
|
+
* 2. Within the do simulation scope, you can use the `bind` function to define variables and bind them to `Option` values
|
|
1317
|
+
* 3. You can accumulate multiple `bind` statements to define multiple variables within the scope
|
|
1318
|
+
* 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values
|
|
1319
|
+
* 5. Regular `Option` functions like `map` and `filter` can still be used within the do simulation. These functions will receive the accumulated variables as arguments within the scope
|
|
1320
|
+
*
|
|
1321
|
+
* @see {@link bindTo}
|
|
1322
|
+
* @see {@link bind}
|
|
1323
|
+
* @see {@link let_ let}
|
|
1324
|
+
*
|
|
1325
|
+
* @example
|
|
1326
|
+
* import { Option, pipe } from "effect"
|
|
1327
|
+
*
|
|
1328
|
+
* const result = pipe(
|
|
1329
|
+
* Option.Do,
|
|
1330
|
+
* Option.bind("x", () => Option.some(2)),
|
|
1331
|
+
* Option.bind("y", () => Option.some(3)),
|
|
1332
|
+
* Option.let("sum", ({ x, y }) => x + y),
|
|
1333
|
+
* Option.filter(({ x, y }) => x * y > 5)
|
|
1334
|
+
* )
|
|
1335
|
+
* assert.deepStrictEqual(result, Option.some({ x: 2, y: 3, sum: 5 }))
|
|
1336
|
+
*
|
|
1259
1337
|
* @category do notation
|
|
1260
1338
|
* @since 2.0.0
|
|
1261
1339
|
*/
|