@clayroach/effect 3.19.14-source-capture.8 → 3.19.14-source-trace.2
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/SourceLocation/package.json +6 -0
- package/dist/cjs/Effect.js +2 -28
- package/dist/cjs/Effect.js.map +1 -1
- package/dist/cjs/FiberRef.js +12 -1
- package/dist/cjs/FiberRef.js.map +1 -1
- package/dist/cjs/Layer.js +2 -24
- package/dist/cjs/Layer.js.map +1 -1
- package/dist/cjs/RuntimeFlags.js +1 -29
- package/dist/cjs/RuntimeFlags.js.map +1 -1
- package/dist/cjs/SourceLocation.js +60 -0
- package/dist/cjs/SourceLocation.js.map +1 -0
- package/dist/cjs/Tracer.js +1 -15
- package/dist/cjs/Tracer.js.map +1 -1
- package/dist/cjs/Utils.js +1 -1
- package/dist/cjs/Utils.js.map +1 -1
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/internal/clock.js +1 -1
- package/dist/cjs/internal/clock.js.map +1 -1
- package/dist/cjs/internal/core.js +17 -50
- package/dist/cjs/internal/core.js.map +1 -1
- package/dist/cjs/internal/effect/circular.js +18 -30
- package/dist/cjs/internal/effect/circular.js.map +1 -1
- package/dist/cjs/internal/fiberRuntime.js +16 -65
- package/dist/cjs/internal/fiberRuntime.js.map +1 -1
- package/dist/cjs/internal/layer/circular.js +1 -5
- package/dist/cjs/internal/layer/circular.js.map +1 -1
- package/dist/cjs/internal/layer.js +1 -3
- package/dist/cjs/internal/layer.js.map +1 -1
- package/dist/cjs/internal/logger.js +25 -2
- package/dist/cjs/internal/logger.js.map +1 -1
- package/dist/cjs/internal/runtimeFlags.js +2 -11
- package/dist/cjs/internal/runtimeFlags.js.map +1 -1
- package/dist/cjs/internal/tracer.js +1 -114
- package/dist/cjs/internal/tracer.js.map +1 -1
- package/dist/dts/Config.d.ts +2 -2
- package/dist/dts/Config.d.ts.map +1 -1
- package/dist/dts/Effect.d.ts +8 -29
- package/dist/dts/Effect.d.ts.map +1 -1
- package/dist/dts/FiberRef.d.ts +12 -0
- package/dist/dts/FiberRef.d.ts.map +1 -1
- package/dist/dts/Layer.d.ts +0 -22
- package/dist/dts/Layer.d.ts.map +1 -1
- package/dist/dts/RuntimeFlags.d.ts +0 -28
- package/dist/dts/RuntimeFlags.d.ts.map +1 -1
- package/dist/dts/SourceLocation.d.ts +88 -0
- package/dist/dts/SourceLocation.d.ts.map +1 -0
- package/dist/dts/Tracer.d.ts +0 -15
- package/dist/dts/Tracer.d.ts.map +1 -1
- package/dist/dts/index.d.ts +6 -0
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/dts/internal/core.d.ts.map +1 -1
- package/dist/dts/internal/layer.d.ts.map +1 -1
- package/dist/dts/internal/runtimeFlags.d.ts.map +1 -1
- package/dist/esm/Effect.js +0 -26
- package/dist/esm/Effect.js.map +1 -1
- package/dist/esm/FiberRef.js +11 -0
- package/dist/esm/FiberRef.js.map +1 -1
- package/dist/esm/Layer.js +0 -22
- package/dist/esm/Layer.js.map +1 -1
- package/dist/esm/RuntimeFlags.js +0 -28
- package/dist/esm/RuntimeFlags.js.map +1 -1
- package/dist/esm/SourceLocation.js +51 -0
- package/dist/esm/SourceLocation.js.map +1 -0
- package/dist/esm/Tracer.js +0 -14
- package/dist/esm/Tracer.js.map +1 -1
- package/dist/esm/Utils.js +1 -1
- package/dist/esm/Utils.js.map +1 -1
- package/dist/esm/index.js +6 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/internal/clock.js +1 -1
- package/dist/esm/internal/clock.js.map +1 -1
- package/dist/esm/internal/core.js +12 -45
- package/dist/esm/internal/core.js.map +1 -1
- package/dist/esm/internal/effect/circular.js +18 -30
- package/dist/esm/internal/effect/circular.js.map +1 -1
- package/dist/esm/internal/fiberRuntime.js +13 -60
- package/dist/esm/internal/fiberRuntime.js.map +1 -1
- package/dist/esm/internal/layer/circular.js +0 -4
- package/dist/esm/internal/layer/circular.js.map +1 -1
- package/dist/esm/internal/layer.js +0 -2
- package/dist/esm/internal/layer.js.map +1 -1
- package/dist/esm/internal/logger.js +25 -2
- package/dist/esm/internal/logger.js.map +1 -1
- package/dist/esm/internal/runtimeFlags.js +1 -9
- package/dist/esm/internal/runtimeFlags.js.map +1 -1
- package/dist/esm/internal/tracer.js +0 -111
- package/dist/esm/internal/tracer.js.map +1 -1
- package/package.json +12 -1
- package/src/Arbitrary.ts +1101 -0
- package/src/Array.ts +3589 -0
- package/src/BigDecimal.ts +1349 -0
- package/src/BigInt.ts +643 -0
- package/src/Boolean.ts +287 -0
- package/src/Brand.ts +360 -0
- package/src/Cache.ts +281 -0
- package/src/Cause.ts +1555 -0
- package/src/Channel.ts +2355 -0
- package/src/ChildExecutorDecision.ts +146 -0
- package/src/Chunk.ts +1495 -0
- package/src/Clock.ts +111 -0
- package/src/Config.ts +542 -0
- package/src/ConfigError.ts +270 -0
- package/src/ConfigProvider.ts +333 -0
- package/src/ConfigProviderPathPatch.ts +100 -0
- package/src/Console.ts +226 -0
- package/src/Context.ts +585 -0
- package/src/Cron.ts +706 -0
- package/src/Data.ts +596 -0
- package/src/DateTime.ts +1686 -0
- package/src/DefaultServices.ts +34 -0
- package/src/Deferred.ts +301 -0
- package/src/Differ.ts +450 -0
- package/src/Duration.ts +1000 -0
- package/src/Effect.ts +14817 -0
- package/src/Effectable.ts +107 -0
- package/src/Either.ts +1040 -0
- package/src/Encoding.ts +195 -0
- package/src/Equal.ts +98 -0
- package/src/Equivalence.ts +235 -0
- package/src/ExecutionPlan.ts +308 -0
- package/src/ExecutionStrategy.ts +119 -0
- package/src/Exit.ts +467 -0
- package/src/FastCheck.ts +9 -0
- package/src/Fiber.ts +744 -0
- package/src/FiberHandle.ts +540 -0
- package/src/FiberId.ts +195 -0
- package/src/FiberMap.ts +656 -0
- package/src/FiberRef.ts +444 -0
- package/src/FiberRefs.ts +204 -0
- package/src/FiberRefsPatch.ts +105 -0
- package/src/FiberSet.ts +491 -0
- package/src/FiberStatus.ts +108 -0
- package/src/Function.ts +1222 -0
- package/src/GlobalValue.ts +53 -0
- package/src/Graph.ts +3732 -0
- package/src/GroupBy.ts +103 -0
- package/src/HKT.ts +45 -0
- package/src/Hash.ts +195 -0
- package/src/HashMap.ts +519 -0
- package/src/HashRing.ts +317 -0
- package/src/HashSet.ts +2346 -0
- package/src/Inspectable.ts +287 -0
- package/src/Iterable.ts +1119 -0
- package/src/JSONSchema.ts +1044 -0
- package/src/KeyedPool.ts +167 -0
- package/src/Layer.ts +1228 -0
- package/src/LayerMap.ts +436 -0
- package/src/List.ts +977 -0
- package/src/LogLevel.ts +285 -0
- package/src/LogSpan.ts +25 -0
- package/src/Logger.ts +702 -0
- package/src/Mailbox.ts +268 -0
- package/src/ManagedRuntime.ts +180 -0
- package/src/Match.ts +1477 -0
- package/src/MergeDecision.ts +95 -0
- package/src/MergeState.ts +172 -0
- package/src/MergeStrategy.ts +107 -0
- package/src/Metric.ts +780 -0
- package/src/MetricBoundaries.ts +69 -0
- package/src/MetricHook.ts +151 -0
- package/src/MetricKey.ts +224 -0
- package/src/MetricKeyType.ts +262 -0
- package/src/MetricLabel.ts +47 -0
- package/src/MetricPair.ts +71 -0
- package/src/MetricPolling.ts +148 -0
- package/src/MetricRegistry.ts +48 -0
- package/src/MetricState.ts +257 -0
- package/src/Micro.ts +4405 -0
- package/src/ModuleVersion.ts +18 -0
- package/src/MutableHashMap.ts +411 -0
- package/src/MutableHashSet.ts +706 -0
- package/src/MutableList.ts +297 -0
- package/src/MutableQueue.ts +227 -0
- package/src/MutableRef.ts +202 -0
- package/src/NonEmptyIterable.ts +32 -0
- package/src/Number.ts +1071 -0
- package/src/Option.ts +2170 -0
- package/src/Order.ts +373 -0
- package/src/Ordering.ts +111 -0
- package/src/ParseResult.ts +2031 -0
- package/src/PartitionedSemaphore.ts +200 -0
- package/src/Pipeable.ts +566 -0
- package/src/Pool.ts +204 -0
- package/src/Predicate.ts +1405 -0
- package/src/Pretty.ts +205 -0
- package/src/PrimaryKey.ts +23 -0
- package/src/PubSub.ts +182 -0
- package/src/Queue.ts +644 -0
- package/src/Random.ts +204 -0
- package/src/RateLimiter.ts +138 -0
- package/src/RcMap.ts +141 -0
- package/src/RcRef.ts +122 -0
- package/src/Readable.ts +93 -0
- package/src/Record.ts +1274 -0
- package/src/RedBlackTree.ts +421 -0
- package/src/Redacted.ts +144 -0
- package/src/Ref.ts +180 -0
- package/src/RegExp.ts +38 -0
- package/src/Reloadable.ts +127 -0
- package/src/Request.ts +347 -0
- package/src/RequestBlock.ts +118 -0
- package/src/RequestResolver.ts +366 -0
- package/src/Resource.ts +119 -0
- package/src/Runtime.ts +383 -0
- package/src/RuntimeFlags.ts +336 -0
- package/src/RuntimeFlagsPatch.ts +183 -0
- package/src/STM.ts +2045 -0
- package/src/Schedule.ts +2219 -0
- package/src/ScheduleDecision.ts +62 -0
- package/src/ScheduleInterval.ts +151 -0
- package/src/ScheduleIntervals.ts +122 -0
- package/src/Scheduler.ts +353 -0
- package/src/Schema.ts +10914 -0
- package/src/SchemaAST.ts +3043 -0
- package/src/Scope.ts +204 -0
- package/src/ScopedCache.ts +151 -0
- package/src/ScopedRef.ts +117 -0
- package/src/Secret.ts +88 -0
- package/src/SingleProducerAsyncInput.ts +67 -0
- package/src/Sink.ts +1461 -0
- package/src/SortedMap.ts +287 -0
- package/src/SortedSet.ts +390 -0
- package/src/SourceLocation.ts +108 -0
- package/src/Stream.ts +6468 -0
- package/src/StreamEmit.ts +136 -0
- package/src/StreamHaltStrategy.ts +123 -0
- package/src/Streamable.ts +45 -0
- package/src/String.ts +778 -0
- package/src/Struct.ts +243 -0
- package/src/Subscribable.ts +100 -0
- package/src/SubscriptionRef.ts +298 -0
- package/src/Supervisor.ts +240 -0
- package/src/Symbol.ts +29 -0
- package/src/SynchronizedRef.ts +270 -0
- package/src/TArray.ts +495 -0
- package/src/TDeferred.ts +100 -0
- package/src/TMap.ts +515 -0
- package/src/TPriorityQueue.ts +223 -0
- package/src/TPubSub.ts +200 -0
- package/src/TQueue.ts +432 -0
- package/src/TRandom.ts +129 -0
- package/src/TReentrantLock.ts +224 -0
- package/src/TRef.ts +178 -0
- package/src/TSemaphore.ts +129 -0
- package/src/TSet.ts +365 -0
- package/src/TSubscriptionRef.ts +192 -0
- package/src/Take.ts +258 -0
- package/src/TestAnnotation.ts +158 -0
- package/src/TestAnnotationMap.ts +119 -0
- package/src/TestAnnotations.ts +117 -0
- package/src/TestClock.ts +556 -0
- package/src/TestConfig.ts +47 -0
- package/src/TestContext.ts +36 -0
- package/src/TestLive.ts +53 -0
- package/src/TestServices.ts +390 -0
- package/src/TestSized.ts +55 -0
- package/src/Tracer.ts +182 -0
- package/src/Trie.ts +840 -0
- package/src/Tuple.ts +305 -0
- package/src/Types.ts +353 -0
- package/src/Unify.ts +113 -0
- package/src/UpstreamPullRequest.ts +117 -0
- package/src/UpstreamPullStrategy.ts +121 -0
- package/src/Utils.ts +809 -0
- package/src/index.ts +1568 -0
- package/src/internal/array.ts +8 -0
- package/src/internal/blockedRequests.ts +520 -0
- package/src/internal/cache.ts +733 -0
- package/src/internal/cause.ts +1050 -0
- package/src/internal/channel/channelExecutor.ts +1200 -0
- package/src/internal/channel/channelState.ts +134 -0
- package/src/internal/channel/childExecutorDecision.ts +96 -0
- package/src/internal/channel/continuation.ts +200 -0
- package/src/internal/channel/mergeDecision.ts +113 -0
- package/src/internal/channel/mergeState.ts +120 -0
- package/src/internal/channel/mergeStrategy.ts +72 -0
- package/src/internal/channel/singleProducerAsyncInput.ts +259 -0
- package/src/internal/channel/subexecutor.ts +229 -0
- package/src/internal/channel/upstreamPullRequest.ts +84 -0
- package/src/internal/channel/upstreamPullStrategy.ts +87 -0
- package/src/internal/channel.ts +2603 -0
- package/src/internal/clock.ts +95 -0
- package/src/internal/completedRequestMap.ts +9 -0
- package/src/internal/concurrency.ts +54 -0
- package/src/internal/config.ts +716 -0
- package/src/internal/configError.ts +304 -0
- package/src/internal/configProvider/pathPatch.ts +97 -0
- package/src/internal/configProvider.ts +799 -0
- package/src/internal/console.ts +153 -0
- package/src/internal/context.ts +337 -0
- package/src/internal/core-effect.ts +2293 -0
- package/src/internal/core-stream.ts +998 -0
- package/src/internal/core.ts +3189 -0
- package/src/internal/data.ts +36 -0
- package/src/internal/dataSource.ts +327 -0
- package/src/internal/dateTime.ts +1277 -0
- package/src/internal/defaultServices/console.ts +100 -0
- package/src/internal/defaultServices.ts +163 -0
- package/src/internal/deferred.ts +46 -0
- package/src/internal/differ/chunkPatch.ts +211 -0
- package/src/internal/differ/contextPatch.ts +232 -0
- package/src/internal/differ/hashMapPatch.ts +220 -0
- package/src/internal/differ/hashSetPatch.ts +176 -0
- package/src/internal/differ/orPatch.ts +311 -0
- package/src/internal/differ/readonlyArrayPatch.ts +210 -0
- package/src/internal/differ.ts +200 -0
- package/src/internal/doNotation.ts +80 -0
- package/src/internal/effect/circular.ts +895 -0
- package/src/internal/effectable.ts +131 -0
- package/src/internal/either.ts +110 -0
- package/src/internal/encoding/base64.ts +286 -0
- package/src/internal/encoding/base64Url.ts +29 -0
- package/src/internal/encoding/common.ts +51 -0
- package/src/internal/encoding/hex.ts +315 -0
- package/src/internal/errors.ts +7 -0
- package/src/internal/executionPlan.ts +114 -0
- package/src/internal/executionStrategy.ts +74 -0
- package/src/internal/fiber.ts +388 -0
- package/src/internal/fiberId.ts +267 -0
- package/src/internal/fiberMessage.ts +82 -0
- package/src/internal/fiberRefs/patch.ts +144 -0
- package/src/internal/fiberRefs.ts +297 -0
- package/src/internal/fiberRuntime.ts +3842 -0
- package/src/internal/fiberScope.ts +71 -0
- package/src/internal/fiberStatus.ts +119 -0
- package/src/internal/groupBy.ts +530 -0
- package/src/internal/hashMap/array.ts +49 -0
- package/src/internal/hashMap/bitwise.ts +32 -0
- package/src/internal/hashMap/config.ts +14 -0
- package/src/internal/hashMap/keySet.ts +8 -0
- package/src/internal/hashMap/node.ts +391 -0
- package/src/internal/hashMap.ts +586 -0
- package/src/internal/hashSet.ts +323 -0
- package/src/internal/keyedPool.ts +244 -0
- package/src/internal/layer/circular.ts +214 -0
- package/src/internal/layer.ts +1483 -0
- package/src/internal/logSpan.ts +20 -0
- package/src/internal/logger-circular.ts +24 -0
- package/src/internal/logger.ts +522 -0
- package/src/internal/mailbox.ts +561 -0
- package/src/internal/managedRuntime/circular.ts +6 -0
- package/src/internal/managedRuntime.ts +134 -0
- package/src/internal/matcher.ts +652 -0
- package/src/internal/metric/boundaries.ts +75 -0
- package/src/internal/metric/hook.ts +483 -0
- package/src/internal/metric/key.ts +167 -0
- package/src/internal/metric/keyType.ts +238 -0
- package/src/internal/metric/label.ts +41 -0
- package/src/internal/metric/pair.ts +48 -0
- package/src/internal/metric/polling.ts +149 -0
- package/src/internal/metric/registry.ts +187 -0
- package/src/internal/metric/state.ts +290 -0
- package/src/internal/metric.ts +577 -0
- package/src/internal/opCodes/cause.ts +35 -0
- package/src/internal/opCodes/channel.ts +83 -0
- package/src/internal/opCodes/channelChildExecutorDecision.ts +17 -0
- package/src/internal/opCodes/channelMergeDecision.ts +11 -0
- package/src/internal/opCodes/channelMergeState.ts +17 -0
- package/src/internal/opCodes/channelMergeStrategy.ts +11 -0
- package/src/internal/opCodes/channelState.ts +23 -0
- package/src/internal/opCodes/channelUpstreamPullRequest.ts +11 -0
- package/src/internal/opCodes/channelUpstreamPullStrategy.ts +11 -0
- package/src/internal/opCodes/config.ts +65 -0
- package/src/internal/opCodes/configError.ts +35 -0
- package/src/internal/opCodes/continuation.ts +11 -0
- package/src/internal/opCodes/deferred.ts +11 -0
- package/src/internal/opCodes/effect.ts +89 -0
- package/src/internal/opCodes/layer.ts +59 -0
- package/src/internal/opCodes/streamHaltStrategy.ts +23 -0
- package/src/internal/option.ts +80 -0
- package/src/internal/pool.ts +432 -0
- package/src/internal/pubsub.ts +1762 -0
- package/src/internal/query.ts +204 -0
- package/src/internal/queue.ts +766 -0
- package/src/internal/random.ts +161 -0
- package/src/internal/rateLimiter.ts +93 -0
- package/src/internal/rcMap.ts +285 -0
- package/src/internal/rcRef.ts +192 -0
- package/src/internal/redBlackTree/iterator.ts +200 -0
- package/src/internal/redBlackTree/node.ts +68 -0
- package/src/internal/redBlackTree.ts +1245 -0
- package/src/internal/redacted.ts +73 -0
- package/src/internal/ref.ts +171 -0
- package/src/internal/reloadable.ts +140 -0
- package/src/internal/request.ts +177 -0
- package/src/internal/resource.ts +76 -0
- package/src/internal/ringBuffer.ts +68 -0
- package/src/internal/runtime.ts +558 -0
- package/src/internal/runtimeFlags.ts +178 -0
- package/src/internal/runtimeFlagsPatch.ts +103 -0
- package/src/internal/schedule/decision.ts +47 -0
- package/src/internal/schedule/interval.ts +101 -0
- package/src/internal/schedule/intervals.ts +180 -0
- package/src/internal/schedule.ts +2199 -0
- package/src/internal/schema/errors.ts +191 -0
- package/src/internal/schema/schemaId.ts +106 -0
- package/src/internal/schema/util.ts +50 -0
- package/src/internal/scopedCache.ts +644 -0
- package/src/internal/scopedRef.ts +118 -0
- package/src/internal/secret.ts +89 -0
- package/src/internal/singleShotGen.ts +35 -0
- package/src/internal/sink.ts +2120 -0
- package/src/internal/stack.ts +10 -0
- package/src/internal/stm/core.ts +817 -0
- package/src/internal/stm/entry.ts +59 -0
- package/src/internal/stm/journal.ts +123 -0
- package/src/internal/stm/opCodes/stm.ts +71 -0
- package/src/internal/stm/opCodes/stmState.ts +17 -0
- package/src/internal/stm/opCodes/strategy.ts +17 -0
- package/src/internal/stm/opCodes/tExit.ts +29 -0
- package/src/internal/stm/opCodes/tryCommit.ts +11 -0
- package/src/internal/stm/stm.ts +1453 -0
- package/src/internal/stm/stmState.ts +136 -0
- package/src/internal/stm/tArray.ts +550 -0
- package/src/internal/stm/tDeferred.ts +81 -0
- package/src/internal/stm/tExit.ts +190 -0
- package/src/internal/stm/tMap.ts +824 -0
- package/src/internal/stm/tPriorityQueue.ts +267 -0
- package/src/internal/stm/tPubSub.ts +551 -0
- package/src/internal/stm/tQueue.ts +393 -0
- package/src/internal/stm/tRandom.ts +140 -0
- package/src/internal/stm/tReentrantLock.ts +352 -0
- package/src/internal/stm/tRef.ts +195 -0
- package/src/internal/stm/tSemaphore.ts +113 -0
- package/src/internal/stm/tSet.ts +259 -0
- package/src/internal/stm/tSubscriptionRef.ts +286 -0
- package/src/internal/stm/tryCommit.ts +34 -0
- package/src/internal/stm/txnId.ts +14 -0
- package/src/internal/stm/versioned.ts +4 -0
- package/src/internal/stream/debounceState.ts +57 -0
- package/src/internal/stream/emit.ts +123 -0
- package/src/internal/stream/haltStrategy.ts +94 -0
- package/src/internal/stream/handoff.ts +187 -0
- package/src/internal/stream/handoffSignal.ts +59 -0
- package/src/internal/stream/pull.ts +34 -0
- package/src/internal/stream/sinkEndReason.ts +30 -0
- package/src/internal/stream/zipAllState.ts +88 -0
- package/src/internal/stream/zipChunksState.ts +56 -0
- package/src/internal/stream.ts +8801 -0
- package/src/internal/string-utils.ts +107 -0
- package/src/internal/subscriptionRef.ts +138 -0
- package/src/internal/supervisor/patch.ts +190 -0
- package/src/internal/supervisor.ts +303 -0
- package/src/internal/synchronizedRef.ts +114 -0
- package/src/internal/take.ts +199 -0
- package/src/internal/testing/sleep.ts +27 -0
- package/src/internal/testing/suspendedWarningData.ts +85 -0
- package/src/internal/testing/warningData.ts +94 -0
- package/src/internal/tracer.ts +150 -0
- package/src/internal/trie.ts +722 -0
- package/src/internal/version.ts +7 -0
package/src/Either.ts
ADDED
|
@@ -0,0 +1,1040 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 2.0.0
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import * as Equivalence from "./Equivalence.js"
|
|
6
|
+
import type { LazyArg } from "./Function.js"
|
|
7
|
+
import { constNull, constUndefined, dual, identity } from "./Function.js"
|
|
8
|
+
import type { TypeLambda } from "./HKT.js"
|
|
9
|
+
import type { Inspectable } from "./Inspectable.js"
|
|
10
|
+
import * as doNotation from "./internal/doNotation.js"
|
|
11
|
+
import * as either from "./internal/either.js"
|
|
12
|
+
import * as option_ from "./internal/option.js"
|
|
13
|
+
import type { Option } from "./Option.js"
|
|
14
|
+
import type { Pipeable } from "./Pipeable.js"
|
|
15
|
+
import type { Predicate, Refinement } from "./Predicate.js"
|
|
16
|
+
import { isFunction } from "./Predicate.js"
|
|
17
|
+
import type { Covariant, NoInfer, NotFunction } from "./Types.js"
|
|
18
|
+
import type * as Unify from "./Unify.js"
|
|
19
|
+
import * as Gen from "./Utils.js"
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @category models
|
|
23
|
+
* @since 2.0.0
|
|
24
|
+
*/
|
|
25
|
+
export type Either<A, E = never> = Left<E, A> | Right<E, A>
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @category symbols
|
|
29
|
+
* @since 2.0.0
|
|
30
|
+
*/
|
|
31
|
+
export const TypeId: unique symbol = either.TypeId
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @category symbols
|
|
35
|
+
* @since 2.0.0
|
|
36
|
+
*/
|
|
37
|
+
export type TypeId = typeof TypeId
|
|
38
|
+
|
|
39
|
+
// TODO(4.0): flip the order of the type parameters
|
|
40
|
+
/**
|
|
41
|
+
* @category models
|
|
42
|
+
* @since 2.0.0
|
|
43
|
+
*/
|
|
44
|
+
export interface Left<out E, out A> extends Pipeable, Inspectable {
|
|
45
|
+
readonly _tag: "Left"
|
|
46
|
+
readonly _op: "Left"
|
|
47
|
+
readonly left: E
|
|
48
|
+
readonly [TypeId]: {
|
|
49
|
+
readonly _R: Covariant<A>
|
|
50
|
+
readonly _L: Covariant<E>
|
|
51
|
+
}
|
|
52
|
+
[Unify.typeSymbol]?: unknown
|
|
53
|
+
[Unify.unifySymbol]?: EitherUnify<this>
|
|
54
|
+
[Unify.ignoreSymbol]?: EitherUnifyIgnore
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// TODO(4.0): flip the order of the type parameters
|
|
58
|
+
/**
|
|
59
|
+
* @category models
|
|
60
|
+
* @since 2.0.0
|
|
61
|
+
*/
|
|
62
|
+
export interface Right<out E, out A> extends Pipeable, Inspectable {
|
|
63
|
+
readonly _tag: "Right"
|
|
64
|
+
readonly _op: "Right"
|
|
65
|
+
readonly right: A
|
|
66
|
+
readonly [TypeId]: {
|
|
67
|
+
readonly _R: Covariant<A>
|
|
68
|
+
readonly _L: Covariant<E>
|
|
69
|
+
}
|
|
70
|
+
[Unify.typeSymbol]?: unknown
|
|
71
|
+
[Unify.unifySymbol]?: EitherUnify<this>
|
|
72
|
+
[Unify.ignoreSymbol]?: EitherUnifyIgnore
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @category models
|
|
77
|
+
* @since 2.0.0
|
|
78
|
+
*/
|
|
79
|
+
export interface EitherUnify<A extends { [Unify.typeSymbol]?: any }> {
|
|
80
|
+
Either?: () => A[Unify.typeSymbol] extends Either<infer R0, infer L0> | infer _ ? Either<R0, L0> : never
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @category models
|
|
85
|
+
* @since 2.0.0
|
|
86
|
+
*/
|
|
87
|
+
export interface EitherUnifyIgnore {}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @category type lambdas
|
|
91
|
+
* @since 2.0.0
|
|
92
|
+
*/
|
|
93
|
+
export interface EitherTypeLambda extends TypeLambda {
|
|
94
|
+
readonly type: Either<this["Target"], this["Out1"]>
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* @since 2.0.0
|
|
99
|
+
*/
|
|
100
|
+
export declare namespace Either {
|
|
101
|
+
/**
|
|
102
|
+
* @since 2.0.0
|
|
103
|
+
* @category type-level
|
|
104
|
+
*/
|
|
105
|
+
export type Left<T extends Either<any, any>> = [T] extends [Either<infer _A, infer _E>] ? _E : never
|
|
106
|
+
/**
|
|
107
|
+
* @since 2.0.0
|
|
108
|
+
* @category type-level
|
|
109
|
+
*/
|
|
110
|
+
export type Right<T extends Either<any, any>> = [T] extends [Either<infer _A, infer _E>] ? _A : never
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Constructs a new `Either` holding a `Right` value. This usually represents a successful value due to the right bias
|
|
115
|
+
* of this structure.
|
|
116
|
+
*
|
|
117
|
+
* @category constructors
|
|
118
|
+
* @since 2.0.0
|
|
119
|
+
*/
|
|
120
|
+
export const right: <A>(a: A) => Either<A> = either.right
|
|
121
|
+
|
|
122
|
+
const void_: Either<void> = right(void 0)
|
|
123
|
+
export {
|
|
124
|
+
/**
|
|
125
|
+
* @category constructors
|
|
126
|
+
* @since 3.13.0
|
|
127
|
+
*/
|
|
128
|
+
void_ as void
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Constructs a new `Either` holding a `Left` value. This usually represents a failure, due to the right-bias of this
|
|
133
|
+
* structure.
|
|
134
|
+
*
|
|
135
|
+
* @category constructors
|
|
136
|
+
* @since 2.0.0
|
|
137
|
+
*/
|
|
138
|
+
export const left: <E>(e: E) => Either<never, E> = either.left
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Takes a lazy default and a nullable value, if the value is not nully (`null` or `undefined`), turn it into a `Right`, if the value is nully use
|
|
142
|
+
* the provided default as a `Left`.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* import * as assert from "node:assert"
|
|
147
|
+
* import { Either } from "effect"
|
|
148
|
+
*
|
|
149
|
+
* assert.deepStrictEqual(Either.fromNullable(1, () => 'fallback'), Either.right(1))
|
|
150
|
+
* assert.deepStrictEqual(Either.fromNullable(null, () => 'fallback'), Either.left('fallback'))
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* @category constructors
|
|
154
|
+
* @since 2.0.0
|
|
155
|
+
*/
|
|
156
|
+
export const fromNullable: {
|
|
157
|
+
<A, E>(onNullable: (right: A) => E): (self: A) => Either<NonNullable<A>, E>
|
|
158
|
+
<A, E>(self: A, onNullable: (right: A) => E): Either<NonNullable<A>, E>
|
|
159
|
+
} = dual(
|
|
160
|
+
2,
|
|
161
|
+
<A, E>(self: A, onNullable: (right: A) => E): Either<NonNullable<A>, E> =>
|
|
162
|
+
self == null ? left(onNullable(self)) : right(self)
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* import * as assert from "node:assert"
|
|
169
|
+
* import { Either, Option } from "effect"
|
|
170
|
+
*
|
|
171
|
+
* assert.deepStrictEqual(Either.fromOption(Option.some(1), () => 'error'), Either.right(1))
|
|
172
|
+
* assert.deepStrictEqual(Either.fromOption(Option.none(), () => 'error'), Either.left('error'))
|
|
173
|
+
* ```
|
|
174
|
+
*
|
|
175
|
+
* @category constructors
|
|
176
|
+
* @since 2.0.0
|
|
177
|
+
*/
|
|
178
|
+
export const fromOption: {
|
|
179
|
+
<E>(onNone: () => E): <A>(self: Option<A>) => Either<A, E>
|
|
180
|
+
<A, E>(self: Option<A>, onNone: () => E): Either<A, E>
|
|
181
|
+
} = either.fromOption
|
|
182
|
+
|
|
183
|
+
const try_: {
|
|
184
|
+
<A, E>(
|
|
185
|
+
options: {
|
|
186
|
+
readonly try: LazyArg<A>
|
|
187
|
+
readonly catch: (error: unknown) => E
|
|
188
|
+
}
|
|
189
|
+
): Either<A, E>
|
|
190
|
+
<A>(evaluate: LazyArg<A>): Either<A, unknown>
|
|
191
|
+
} = (<A, E>(
|
|
192
|
+
evaluate: LazyArg<A> | {
|
|
193
|
+
readonly try: LazyArg<A>
|
|
194
|
+
readonly catch: (error: unknown) => E
|
|
195
|
+
}
|
|
196
|
+
) => {
|
|
197
|
+
if (isFunction(evaluate)) {
|
|
198
|
+
try {
|
|
199
|
+
return right(evaluate())
|
|
200
|
+
} catch (e) {
|
|
201
|
+
return left(e)
|
|
202
|
+
}
|
|
203
|
+
} else {
|
|
204
|
+
try {
|
|
205
|
+
return right(evaluate.try())
|
|
206
|
+
} catch (e) {
|
|
207
|
+
return left(evaluate.catch(e))
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}) as any
|
|
211
|
+
|
|
212
|
+
export {
|
|
213
|
+
/**
|
|
214
|
+
* Imports a synchronous side-effect into a pure `Either` value, translating any
|
|
215
|
+
* thrown exceptions into typed failed eithers creating with `Either.left`.
|
|
216
|
+
*
|
|
217
|
+
* @category constructors
|
|
218
|
+
* @since 2.0.0
|
|
219
|
+
*/
|
|
220
|
+
try_ as try
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Tests if a value is a `Either`.
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* import * as assert from "node:assert"
|
|
229
|
+
* import { Either } from "effect"
|
|
230
|
+
*
|
|
231
|
+
* assert.deepStrictEqual(Either.isEither(Either.right(1)), true)
|
|
232
|
+
* assert.deepStrictEqual(Either.isEither(Either.left("a")), true)
|
|
233
|
+
* assert.deepStrictEqual(Either.isEither({ right: 1 }), false)
|
|
234
|
+
* ```
|
|
235
|
+
*
|
|
236
|
+
* @category guards
|
|
237
|
+
* @since 2.0.0
|
|
238
|
+
*/
|
|
239
|
+
export const isEither: (input: unknown) => input is Either<unknown, unknown> = either.isEither
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Determine if a `Either` is a `Left`.
|
|
243
|
+
*
|
|
244
|
+
* @example
|
|
245
|
+
* ```ts
|
|
246
|
+
* import * as assert from "node:assert"
|
|
247
|
+
* import { Either } from "effect"
|
|
248
|
+
*
|
|
249
|
+
* assert.deepStrictEqual(Either.isLeft(Either.right(1)), false)
|
|
250
|
+
* assert.deepStrictEqual(Either.isLeft(Either.left("a")), true)
|
|
251
|
+
* ```
|
|
252
|
+
*
|
|
253
|
+
* @category guards
|
|
254
|
+
* @since 2.0.0
|
|
255
|
+
*/
|
|
256
|
+
export const isLeft: <A, E>(self: Either<A, E>) => self is Left<E, A> = either.isLeft
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Determine if a `Either` is a `Right`.
|
|
260
|
+
*
|
|
261
|
+
* @example
|
|
262
|
+
* ```ts
|
|
263
|
+
* import * as assert from "node:assert"
|
|
264
|
+
* import { Either } from "effect"
|
|
265
|
+
*
|
|
266
|
+
* assert.deepStrictEqual(Either.isRight(Either.right(1)), true)
|
|
267
|
+
* assert.deepStrictEqual(Either.isRight(Either.left("a")), false)
|
|
268
|
+
* ```
|
|
269
|
+
*
|
|
270
|
+
* @category guards
|
|
271
|
+
* @since 2.0.0
|
|
272
|
+
*/
|
|
273
|
+
export const isRight: <A, E>(self: Either<A, E>) => self is Right<E, A> = either.isRight
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Converts a `Either` to an `Option` discarding the `Left`.
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```ts
|
|
280
|
+
* import * as assert from "node:assert"
|
|
281
|
+
* import { Either, Option } from "effect"
|
|
282
|
+
*
|
|
283
|
+
* assert.deepStrictEqual(Either.getRight(Either.right('ok')), Option.some('ok'))
|
|
284
|
+
* assert.deepStrictEqual(Either.getRight(Either.left('err')), Option.none())
|
|
285
|
+
* ```
|
|
286
|
+
*
|
|
287
|
+
* @category getters
|
|
288
|
+
* @since 2.0.0
|
|
289
|
+
*/
|
|
290
|
+
export const getRight: <A, E>(self: Either<A, E>) => Option<A> = either.getRight
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Converts a `Either` to an `Option` discarding the value.
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* ```ts
|
|
297
|
+
* import * as assert from "node:assert"
|
|
298
|
+
* import { Either, Option } from "effect"
|
|
299
|
+
*
|
|
300
|
+
* assert.deepStrictEqual(Either.getLeft(Either.right('ok')), Option.none())
|
|
301
|
+
* assert.deepStrictEqual(Either.getLeft(Either.left('err')), Option.some('err'))
|
|
302
|
+
* ```
|
|
303
|
+
*
|
|
304
|
+
* @category getters
|
|
305
|
+
* @since 2.0.0
|
|
306
|
+
*/
|
|
307
|
+
export const getLeft: <A, E>(self: Either<A, E>) => Option<E> = either.getLeft
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* @category equivalence
|
|
311
|
+
* @since 2.0.0
|
|
312
|
+
*/
|
|
313
|
+
export const getEquivalence = <A, E>({ left, right }: {
|
|
314
|
+
right: Equivalence.Equivalence<A>
|
|
315
|
+
left: Equivalence.Equivalence<E>
|
|
316
|
+
}): Equivalence.Equivalence<Either<A, E>> =>
|
|
317
|
+
Equivalence.make((x, y) =>
|
|
318
|
+
isLeft(x) ?
|
|
319
|
+
isLeft(y) && left(x.left, y.left) :
|
|
320
|
+
isRight(y) && right(x.right, y.right)
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* @category mapping
|
|
325
|
+
* @since 2.0.0
|
|
326
|
+
*/
|
|
327
|
+
export const mapBoth: {
|
|
328
|
+
<E, E2, A, A2>(options: {
|
|
329
|
+
readonly onLeft: (left: E) => E2
|
|
330
|
+
readonly onRight: (right: A) => A2
|
|
331
|
+
}): (self: Either<A, E>) => Either<A2, E2>
|
|
332
|
+
<A, E, E2, A2>(self: Either<A, E>, options: {
|
|
333
|
+
readonly onLeft: (left: E) => E2
|
|
334
|
+
readonly onRight: (right: A) => A2
|
|
335
|
+
}): Either<A2, E2>
|
|
336
|
+
} = dual(
|
|
337
|
+
2,
|
|
338
|
+
<A, E, E2, A2>(self: Either<A, E>, { onLeft, onRight }: {
|
|
339
|
+
readonly onLeft: (left: E) => E2
|
|
340
|
+
readonly onRight: (right: A) => A2
|
|
341
|
+
}): Either<A2, E2> => isLeft(self) ? left(onLeft(self.left)) : right(onRight(self.right))
|
|
342
|
+
)
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Maps the `Left` side of an `Either` value to a new `Either` value.
|
|
346
|
+
*
|
|
347
|
+
* @category mapping
|
|
348
|
+
* @since 2.0.0
|
|
349
|
+
*/
|
|
350
|
+
export const mapLeft: {
|
|
351
|
+
<E, E2>(f: (left: E) => E2): <A>(self: Either<A, E>) => Either<A, E2>
|
|
352
|
+
<A, E, E2>(self: Either<A, E>, f: (left: E) => E2): Either<A, E2>
|
|
353
|
+
} = dual(
|
|
354
|
+
2,
|
|
355
|
+
<A, E, E2>(self: Either<A, E>, f: (left: E) => E2): Either<A, E2> =>
|
|
356
|
+
isLeft(self) ? left(f(self.left)) : right(self.right)
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Maps the `Right` side of an `Either` value to a new `Either` value.
|
|
361
|
+
*
|
|
362
|
+
* @category mapping
|
|
363
|
+
* @since 2.0.0
|
|
364
|
+
*/
|
|
365
|
+
export const map: {
|
|
366
|
+
<A, A2>(f: (right: A) => A2): <E>(self: Either<A, E>) => Either<A2, E>
|
|
367
|
+
<A, E, A2>(self: Either<A, E>, f: (right: A) => A2): Either<A2, E>
|
|
368
|
+
} = dual(
|
|
369
|
+
2,
|
|
370
|
+
<A, E, A2>(self: Either<A, E>, f: (right: A) => A2): Either<A2, E> =>
|
|
371
|
+
isRight(self) ? right(f(self.right)) : left(self.left)
|
|
372
|
+
)
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Takes two functions and an `Either` value, if the value is a `Left` the inner value is applied to the `onLeft function,
|
|
376
|
+
* if the value is a `Right` the inner value is applied to the `onRight` function.
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* ```ts
|
|
380
|
+
* import * as assert from "node:assert"
|
|
381
|
+
* import { pipe, Either } from "effect"
|
|
382
|
+
*
|
|
383
|
+
* const onLeft = (strings: ReadonlyArray<string>): string => `strings: ${strings.join(', ')}`
|
|
384
|
+
*
|
|
385
|
+
* const onRight = (value: number): string => `Ok: ${value}`
|
|
386
|
+
*
|
|
387
|
+
* assert.deepStrictEqual(pipe(Either.right(1), Either.match({ onLeft, onRight })), 'Ok: 1')
|
|
388
|
+
* assert.deepStrictEqual(
|
|
389
|
+
* pipe(Either.left(['string 1', 'string 2']), Either.match({ onLeft, onRight })),
|
|
390
|
+
* 'strings: string 1, string 2'
|
|
391
|
+
* )
|
|
392
|
+
* ```
|
|
393
|
+
*
|
|
394
|
+
* @category pattern matching
|
|
395
|
+
* @since 2.0.0
|
|
396
|
+
*/
|
|
397
|
+
export const match: {
|
|
398
|
+
<E, B, A, C = B>(options: {
|
|
399
|
+
readonly onLeft: (left: E) => B
|
|
400
|
+
readonly onRight: (right: A) => C
|
|
401
|
+
}): (self: Either<A, E>) => B | C
|
|
402
|
+
<A, E, B, C = B>(self: Either<A, E>, options: {
|
|
403
|
+
readonly onLeft: (left: E) => B
|
|
404
|
+
readonly onRight: (right: A) => C
|
|
405
|
+
}): B | C
|
|
406
|
+
} = dual(
|
|
407
|
+
2,
|
|
408
|
+
<A, E, B, C = B>(self: Either<A, E>, { onLeft, onRight }: {
|
|
409
|
+
readonly onLeft: (left: E) => B
|
|
410
|
+
readonly onRight: (right: A) => C
|
|
411
|
+
}): B | C => isLeft(self) ? onLeft(self.left) : onRight(self.right)
|
|
412
|
+
)
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Transforms a `Predicate` function into a `Right` of the input value if the predicate returns `true`
|
|
416
|
+
* or `Left` of the result of the provided function if the predicate returns false
|
|
417
|
+
*
|
|
418
|
+
* @example
|
|
419
|
+
* ```ts
|
|
420
|
+
* import * as assert from "node:assert"
|
|
421
|
+
* import { pipe, Either } from "effect"
|
|
422
|
+
*
|
|
423
|
+
* const isPositive = (n: number): boolean => n > 0
|
|
424
|
+
* const isPositiveEither = Either.liftPredicate(isPositive, n => `${n} is not positive`)
|
|
425
|
+
*
|
|
426
|
+
* assert.deepStrictEqual(
|
|
427
|
+
* isPositiveEither(1),
|
|
428
|
+
* Either.right(1)
|
|
429
|
+
* )
|
|
430
|
+
* assert.deepStrictEqual(
|
|
431
|
+
* isPositiveEither(0),
|
|
432
|
+
* Either.left("0 is not positive")
|
|
433
|
+
* )
|
|
434
|
+
* ```
|
|
435
|
+
*
|
|
436
|
+
* @category lifting
|
|
437
|
+
* @since 3.4.0
|
|
438
|
+
*/
|
|
439
|
+
export const liftPredicate: {
|
|
440
|
+
<A, B extends A, E>(refinement: Refinement<A, B>, orLeftWith: (a: A) => E): (a: A) => Either<B, E>
|
|
441
|
+
<B extends A, E, A = B>(
|
|
442
|
+
predicate: Predicate<A>,
|
|
443
|
+
orLeftWith: (a: A) => E
|
|
444
|
+
): (a: B) => Either<B, E>
|
|
445
|
+
<A, E, B extends A>(
|
|
446
|
+
self: A,
|
|
447
|
+
refinement: Refinement<A, B>,
|
|
448
|
+
orLeftWith: (a: A) => E
|
|
449
|
+
): Either<B, E>
|
|
450
|
+
<B extends A, E, A = B>(
|
|
451
|
+
self: B,
|
|
452
|
+
predicate: Predicate<A>,
|
|
453
|
+
orLeftWith: (a: A) => E
|
|
454
|
+
): Either<B, E>
|
|
455
|
+
} = dual(
|
|
456
|
+
3,
|
|
457
|
+
<A, E>(a: A, predicate: Predicate<A>, orLeftWith: (a: A) => E): Either<A, E> =>
|
|
458
|
+
predicate(a) ? right(a) : left(orLeftWith(a))
|
|
459
|
+
)
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* Filter the right value with the provided function.
|
|
463
|
+
* If the predicate fails, set the left value with the result of the provided function.
|
|
464
|
+
*
|
|
465
|
+
* @example
|
|
466
|
+
* ```ts
|
|
467
|
+
* import * as assert from "node:assert"
|
|
468
|
+
* import { pipe, Either } from "effect"
|
|
469
|
+
*
|
|
470
|
+
* const isPositive = (n: number): boolean => n > 0
|
|
471
|
+
*
|
|
472
|
+
* assert.deepStrictEqual(
|
|
473
|
+
* pipe(
|
|
474
|
+
* Either.right(1),
|
|
475
|
+
* Either.filterOrLeft(isPositive, n => `${n} is not positive`)
|
|
476
|
+
* ),
|
|
477
|
+
* Either.right(1)
|
|
478
|
+
* )
|
|
479
|
+
* assert.deepStrictEqual(
|
|
480
|
+
* pipe(
|
|
481
|
+
* Either.right(0),
|
|
482
|
+
* Either.filterOrLeft(isPositive, n => `${n} is not positive`)
|
|
483
|
+
* ),
|
|
484
|
+
* Either.left("0 is not positive")
|
|
485
|
+
* )
|
|
486
|
+
* ```
|
|
487
|
+
*
|
|
488
|
+
* @since 2.0.0
|
|
489
|
+
* @category filtering & conditionals
|
|
490
|
+
*/
|
|
491
|
+
export const filterOrLeft: {
|
|
492
|
+
<A, B extends A, E2>(
|
|
493
|
+
refinement: Refinement<NoInfer<A>, B>,
|
|
494
|
+
orLeftWith: (right: NoInfer<A>) => E2
|
|
495
|
+
): <E>(self: Either<A, E>) => Either<B, E2 | E>
|
|
496
|
+
<A, E2>(
|
|
497
|
+
predicate: Predicate<NoInfer<A>>,
|
|
498
|
+
orLeftWith: (right: NoInfer<A>) => E2
|
|
499
|
+
): <E>(self: Either<A, E>) => Either<A, E2 | E>
|
|
500
|
+
<A, E, B extends A, E2>(
|
|
501
|
+
self: Either<A, E>,
|
|
502
|
+
refinement: Refinement<A, B>,
|
|
503
|
+
orLeftWith: (right: A) => E2
|
|
504
|
+
): Either<B, E | E2>
|
|
505
|
+
<A, E, E2>(self: Either<A, E>, predicate: Predicate<A>, orLeftWith: (right: A) => E2): Either<A, E | E2>
|
|
506
|
+
} = dual(3, <A, E, E2>(
|
|
507
|
+
self: Either<A, E>,
|
|
508
|
+
predicate: Predicate<A>,
|
|
509
|
+
orLeftWith: (right: A) => E2
|
|
510
|
+
): Either<A, E | E2> => flatMap(self, (r) => predicate(r) ? right(r) : left(orLeftWith(r))))
|
|
511
|
+
|
|
512
|
+
/**
|
|
513
|
+
* @category getters
|
|
514
|
+
* @since 2.0.0
|
|
515
|
+
*/
|
|
516
|
+
export const merge: <A, E>(self: Either<A, E>) => E | A = match({
|
|
517
|
+
onLeft: identity,
|
|
518
|
+
onRight: identity
|
|
519
|
+
})
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Returns the wrapped value if it's a `Right` or a default value if is a `Left`.
|
|
523
|
+
*
|
|
524
|
+
* @example
|
|
525
|
+
* ```ts
|
|
526
|
+
* import * as assert from "node:assert"
|
|
527
|
+
* import { Either } from "effect"
|
|
528
|
+
*
|
|
529
|
+
* assert.deepStrictEqual(Either.getOrElse(Either.right(1), (error) => error + "!"), 1)
|
|
530
|
+
* assert.deepStrictEqual(Either.getOrElse(Either.left("not a number"), (error) => error + "!"), "not a number!")
|
|
531
|
+
* ```
|
|
532
|
+
*
|
|
533
|
+
* @category getters
|
|
534
|
+
* @since 2.0.0
|
|
535
|
+
*/
|
|
536
|
+
export const getOrElse: {
|
|
537
|
+
<E, A2>(onLeft: (left: E) => A2): <A>(self: Either<A, E>) => A2 | A
|
|
538
|
+
<A, E, A2>(self: Either<A, E>, onLeft: (left: E) => A2): A | A2
|
|
539
|
+
} = dual(
|
|
540
|
+
2,
|
|
541
|
+
<A, E, A2>(self: Either<A, E>, onLeft: (left: E) => A2): A | A2 => isLeft(self) ? onLeft(self.left) : self.right
|
|
542
|
+
)
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* @example
|
|
546
|
+
* ```ts
|
|
547
|
+
* import * as assert from "node:assert"
|
|
548
|
+
* import { Either } from "effect"
|
|
549
|
+
*
|
|
550
|
+
* assert.deepStrictEqual(Either.getOrNull(Either.right(1)), 1)
|
|
551
|
+
* assert.deepStrictEqual(Either.getOrNull(Either.left("a")), null)
|
|
552
|
+
* ```
|
|
553
|
+
*
|
|
554
|
+
* @category getters
|
|
555
|
+
* @since 2.0.0
|
|
556
|
+
*/
|
|
557
|
+
export const getOrNull: <A, E>(self: Either<A, E>) => A | null = getOrElse(constNull)
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* @example
|
|
561
|
+
* ```ts
|
|
562
|
+
* import * as assert from "node:assert"
|
|
563
|
+
* import { Either } from "effect"
|
|
564
|
+
*
|
|
565
|
+
* assert.deepStrictEqual(Either.getOrUndefined(Either.right(1)), 1)
|
|
566
|
+
* assert.deepStrictEqual(Either.getOrUndefined(Either.left("a")), undefined)
|
|
567
|
+
* ```
|
|
568
|
+
*
|
|
569
|
+
* @category getters
|
|
570
|
+
* @since 2.0.0
|
|
571
|
+
*/
|
|
572
|
+
export const getOrUndefined: <A, E>(self: Either<A, E>) => A | undefined = getOrElse(constUndefined)
|
|
573
|
+
|
|
574
|
+
/**
|
|
575
|
+
* Extracts the value of an `Either` or throws if the `Either` is `Left`.
|
|
576
|
+
*
|
|
577
|
+
* If a default error is sufficient for your use case and you don't need to configure the thrown error, see {@link getOrThrow}.
|
|
578
|
+
*
|
|
579
|
+
* @example
|
|
580
|
+
* ```ts
|
|
581
|
+
* import * as assert from "node:assert"
|
|
582
|
+
* import { Either } from "effect"
|
|
583
|
+
*
|
|
584
|
+
* assert.deepStrictEqual(
|
|
585
|
+
* Either.getOrThrowWith(Either.right(1), () => new Error('Unexpected Left')),
|
|
586
|
+
* 1
|
|
587
|
+
* )
|
|
588
|
+
* assert.throws(() => Either.getOrThrowWith(Either.left("error"), () => new Error('Unexpected Left')))
|
|
589
|
+
* ```
|
|
590
|
+
*
|
|
591
|
+
* @category getters
|
|
592
|
+
* @since 2.0.0
|
|
593
|
+
*/
|
|
594
|
+
export const getOrThrowWith: {
|
|
595
|
+
<E>(onLeft: (left: E) => unknown): <A>(self: Either<A, E>) => A
|
|
596
|
+
<A, E>(self: Either<A, E>, onLeft: (left: E) => unknown): A
|
|
597
|
+
} = dual(2, <A, E>(self: Either<A, E>, onLeft: (left: E) => unknown): A => {
|
|
598
|
+
if (isRight(self)) {
|
|
599
|
+
return self.right
|
|
600
|
+
}
|
|
601
|
+
throw onLeft(self.left)
|
|
602
|
+
})
|
|
603
|
+
|
|
604
|
+
// TODO(4.0): by default should throw `L` (i.e getOrThrowWith with the identity function)
|
|
605
|
+
/**
|
|
606
|
+
* Extracts the value of an `Either` or throws if the `Either` is `Left`.
|
|
607
|
+
*
|
|
608
|
+
* The thrown error is a default error. To configure the error thrown, see {@link getOrThrowWith}.
|
|
609
|
+
*
|
|
610
|
+
* @example
|
|
611
|
+
* ```ts
|
|
612
|
+
* import * as assert from "node:assert"
|
|
613
|
+
* import { Either } from "effect"
|
|
614
|
+
*
|
|
615
|
+
* assert.deepStrictEqual(Either.getOrThrow(Either.right(1)), 1)
|
|
616
|
+
* assert.throws(() => Either.getOrThrow(Either.left("error")))
|
|
617
|
+
* ```
|
|
618
|
+
*
|
|
619
|
+
* @throws `Error("getOrThrow called on a Left")`
|
|
620
|
+
*
|
|
621
|
+
* @category getters
|
|
622
|
+
* @since 2.0.0
|
|
623
|
+
*/
|
|
624
|
+
export const getOrThrow: <A, E>(self: Either<A, E>) => A = getOrThrowWith(() =>
|
|
625
|
+
new Error("getOrThrow called on a Left")
|
|
626
|
+
)
|
|
627
|
+
|
|
628
|
+
/**
|
|
629
|
+
* Returns `self` if it is a `Right` or `that` otherwise.
|
|
630
|
+
*
|
|
631
|
+
* @category error handling
|
|
632
|
+
* @since 2.0.0
|
|
633
|
+
*/
|
|
634
|
+
export const orElse: {
|
|
635
|
+
<E, A2, E2>(that: (left: E) => Either<A2, E2>): <A>(self: Either<A, E>) => Either<A | A2, E2>
|
|
636
|
+
<A, E, A2, E2>(self: Either<A, E>, that: (left: E) => Either<A2, E2>): Either<A | A2, E2>
|
|
637
|
+
} = dual(
|
|
638
|
+
2,
|
|
639
|
+
<A, E, A2, E2>(self: Either<A, E>, that: (left: E) => Either<A2, E2>): Either<A | A2, E2> =>
|
|
640
|
+
isLeft(self) ? that(self.left) : right(self.right)
|
|
641
|
+
)
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* @category sequencing
|
|
645
|
+
* @since 2.0.0
|
|
646
|
+
*/
|
|
647
|
+
export const flatMap: {
|
|
648
|
+
<A, A2, E2>(f: (right: A) => Either<A2, E2>): <E>(self: Either<A, E>) => Either<A2, E | E2>
|
|
649
|
+
<A, E, A2, E2>(self: Either<A, E>, f: (right: A) => Either<A2, E2>): Either<A2, E | E2>
|
|
650
|
+
} = dual(
|
|
651
|
+
2,
|
|
652
|
+
<A, E, A2, E2>(self: Either<A, E>, f: (right: A) => Either<A2, E2>): Either<A2, E | E2> =>
|
|
653
|
+
isLeft(self) ? left(self.left) : f(self.right)
|
|
654
|
+
)
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* Executes a sequence of two `Either`s. The second `Either` can be dependent on the result of the first `Either`.
|
|
658
|
+
*
|
|
659
|
+
* @category sequencing
|
|
660
|
+
* @since 2.0.0
|
|
661
|
+
*/
|
|
662
|
+
export const andThen: {
|
|
663
|
+
<A, A2, E2>(f: (right: A) => Either<A2, E2>): <E>(self: Either<A, E>) => Either<A2, E | E2>
|
|
664
|
+
<A2, E2>(f: Either<A2, E2>): <E, A>(self: Either<A, E>) => Either<A2, E | E2>
|
|
665
|
+
<A, A2>(f: (right: A) => A2): <E>(self: Either<A, E>) => Either<A2, E>
|
|
666
|
+
<A2>(right: NotFunction<A2>): <A, E>(self: Either<A, E>) => Either<A2, E>
|
|
667
|
+
<A, E, A2, E2>(self: Either<A, E>, f: (right: A) => Either<A2, E2>): Either<A2, E | E2>
|
|
668
|
+
<A, E, A2, E2>(self: Either<A, E>, f: Either<A2, E2>): Either<A2, E | E2>
|
|
669
|
+
<A, E, A2>(self: Either<A, E>, f: (right: A) => A2): Either<A2, E>
|
|
670
|
+
<A, E, A2>(self: Either<A, E>, f: NotFunction<A2>): Either<A2, E>
|
|
671
|
+
} = dual(
|
|
672
|
+
2,
|
|
673
|
+
<A, E, A2, E2>(self: Either<A, E>, f: (right: A) => Either<A2, E2> | Either<A2, E2>): Either<A2, E | E2> =>
|
|
674
|
+
flatMap(self, (a) => {
|
|
675
|
+
const b = isFunction(f) ? f(a) : f
|
|
676
|
+
return isEither(b) ? b : right(b)
|
|
677
|
+
})
|
|
678
|
+
)
|
|
679
|
+
|
|
680
|
+
/**
|
|
681
|
+
* @category zipping
|
|
682
|
+
* @since 2.0.0
|
|
683
|
+
*/
|
|
684
|
+
export const zipWith: {
|
|
685
|
+
<A2, E2, A, B>(
|
|
686
|
+
that: Either<A2, E2>,
|
|
687
|
+
f: (right: A, right2: A2) => B
|
|
688
|
+
): <E>(self: Either<A, E>) => Either<B, E2 | E>
|
|
689
|
+
<A, E, A2, E2, B>(
|
|
690
|
+
self: Either<A, E>,
|
|
691
|
+
that: Either<A2, E2>,
|
|
692
|
+
f: (right: A, right2: A2) => B
|
|
693
|
+
): Either<B, E | E2>
|
|
694
|
+
} = dual(
|
|
695
|
+
3,
|
|
696
|
+
<A, E, A2, E2, B>(self: Either<A, E>, that: Either<A2, E2>, f: (right: A, right2: A2) => B): Either<B, E | E2> =>
|
|
697
|
+
flatMap(self, (r) => map(that, (r2) => f(r, r2)))
|
|
698
|
+
)
|
|
699
|
+
|
|
700
|
+
/**
|
|
701
|
+
* @category combining
|
|
702
|
+
* @since 2.0.0
|
|
703
|
+
*/
|
|
704
|
+
export const ap: {
|
|
705
|
+
<A, E2>(that: Either<A, E2>): <A2, E>(self: Either<(right: A) => A2, E>) => Either<A2, E | E2>
|
|
706
|
+
<A, A2, E, E2>(self: Either<(right: A) => A2, E>, that: Either<A, E2>): Either<A2, E | E2>
|
|
707
|
+
} = dual(
|
|
708
|
+
2,
|
|
709
|
+
<A, E, A2, E2>(self: Either<(right: A) => A2, E>, that: Either<A, E2>): Either<A2, E | E2> =>
|
|
710
|
+
zipWith(self, that, (f, a) => f(a))
|
|
711
|
+
)
|
|
712
|
+
|
|
713
|
+
/**
|
|
714
|
+
* Takes a structure of `Either`s and returns an `Either` of values with the same structure.
|
|
715
|
+
*
|
|
716
|
+
* - If a tuple is supplied, then the returned `Either` will contain a tuple with the same length.
|
|
717
|
+
* - If a struct is supplied, then the returned `Either` will contain a struct with the same keys.
|
|
718
|
+
* - If an iterable is supplied, then the returned `Either` will contain an array.
|
|
719
|
+
*
|
|
720
|
+
* @example
|
|
721
|
+
* ```ts
|
|
722
|
+
* import * as assert from "node:assert"
|
|
723
|
+
* import { Either } from "effect"
|
|
724
|
+
*
|
|
725
|
+
* assert.deepStrictEqual(Either.all([Either.right(1), Either.right(2)]), Either.right([1, 2]))
|
|
726
|
+
* assert.deepStrictEqual(Either.all({ right: Either.right(1), b: Either.right("hello") }), Either.right({ right: 1, b: "hello" }))
|
|
727
|
+
* assert.deepStrictEqual(Either.all({ right: Either.right(1), b: Either.left("error") }), Either.left("error"))
|
|
728
|
+
* ```
|
|
729
|
+
*
|
|
730
|
+
* @category combining
|
|
731
|
+
* @since 2.0.0
|
|
732
|
+
*/
|
|
733
|
+
// @ts-expect-error
|
|
734
|
+
export const all: <const I extends Iterable<Either<any, any>> | Record<string, Either<any, any>>>(
|
|
735
|
+
input: I
|
|
736
|
+
) => [I] extends [ReadonlyArray<Either<any, any>>] ? Either<
|
|
737
|
+
{ -readonly [K in keyof I]: [I[K]] extends [Either<infer A, any>] ? A : never },
|
|
738
|
+
I[number] extends never ? never : [I[number]] extends [Either<any, infer E>] ? E : never
|
|
739
|
+
>
|
|
740
|
+
: [I] extends [Iterable<Either<infer A, infer E>>] ? Either<Array<A>, E>
|
|
741
|
+
: Either<
|
|
742
|
+
{ -readonly [K in keyof I]: [I[K]] extends [Either<infer A, any>] ? A : never },
|
|
743
|
+
I[keyof I] extends never ? never : [I[keyof I]] extends [Either<any, infer E>] ? E : never
|
|
744
|
+
> = (
|
|
745
|
+
input: Iterable<Either<any, any>> | Record<string, Either<any, any>>
|
|
746
|
+
): Either<any, any> => {
|
|
747
|
+
if (Symbol.iterator in input) {
|
|
748
|
+
const out: Array<Either<any, any>> = []
|
|
749
|
+
for (const e of input) {
|
|
750
|
+
if (isLeft(e)) {
|
|
751
|
+
return e
|
|
752
|
+
}
|
|
753
|
+
out.push(e.right)
|
|
754
|
+
}
|
|
755
|
+
return right(out)
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
const out: Record<string, any> = {}
|
|
759
|
+
for (const key of Object.keys(input)) {
|
|
760
|
+
const e = input[key]
|
|
761
|
+
if (isLeft(e)) {
|
|
762
|
+
return e
|
|
763
|
+
}
|
|
764
|
+
out[key] = e.right
|
|
765
|
+
}
|
|
766
|
+
return right(out)
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
/**
|
|
770
|
+
* Returns an `Either` that swaps the error/success cases. This allows you to
|
|
771
|
+
* use all methods on the error channel, possibly before flipping back.
|
|
772
|
+
*
|
|
773
|
+
* @since 2.0.0
|
|
774
|
+
* @category mapping
|
|
775
|
+
*/
|
|
776
|
+
export const flip = <A, E>(self: Either<A, E>): Either<E, A> => isLeft(self) ? right(self.left) : left(self.right)
|
|
777
|
+
|
|
778
|
+
const adapter = Gen.adapter<EitherTypeLambda>()
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* @category generators
|
|
782
|
+
* @since 2.0.0
|
|
783
|
+
*/
|
|
784
|
+
export const gen: Gen.Gen<EitherTypeLambda, Gen.Adapter<EitherTypeLambda>> = (...args) => {
|
|
785
|
+
const f = args.length === 1 ? args[0] : args[1].bind(args[0])
|
|
786
|
+
const iterator = f(adapter)
|
|
787
|
+
let state: IteratorResult<any> = iterator.next()
|
|
788
|
+
while (!state.done) {
|
|
789
|
+
const current = Gen.isGenKind(state.value)
|
|
790
|
+
? state.value.value
|
|
791
|
+
: Gen.yieldWrapGet(state.value)
|
|
792
|
+
if (isLeft(current)) {
|
|
793
|
+
return current
|
|
794
|
+
}
|
|
795
|
+
state = iterator.next(current.right as never)
|
|
796
|
+
}
|
|
797
|
+
return right(state.value) as any
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// -------------------------------------------------------------------------------------
|
|
801
|
+
// do notation
|
|
802
|
+
// -------------------------------------------------------------------------------------
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* The "do simulation" in Effect 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`.
|
|
806
|
+
*
|
|
807
|
+
* Here's how the do simulation works:
|
|
808
|
+
*
|
|
809
|
+
* 1. Start the do simulation using the `Do` value
|
|
810
|
+
* 2. Within the do simulation scope, you can use the `bind` function to define variables and bind them to `Either` values
|
|
811
|
+
* 3. You can accumulate multiple `bind` statements to define multiple variables within the scope
|
|
812
|
+
* 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values
|
|
813
|
+
*
|
|
814
|
+
* @example
|
|
815
|
+
* ```ts
|
|
816
|
+
* import * as assert from "node:assert"
|
|
817
|
+
* import { Either, pipe } from "effect"
|
|
818
|
+
*
|
|
819
|
+
* const result = pipe(
|
|
820
|
+
* Either.Do,
|
|
821
|
+
* Either.bind("x", () => Either.right(2)),
|
|
822
|
+
* Either.bind("y", () => Either.right(3)),
|
|
823
|
+
* Either.let("sum", ({ x, y }) => x + y)
|
|
824
|
+
* )
|
|
825
|
+
* assert.deepStrictEqual(result, Either.right({ x: 2, y: 3, sum: 5 }))
|
|
826
|
+
* ```
|
|
827
|
+
*
|
|
828
|
+
* @see {@link bind}
|
|
829
|
+
* @see {@link bindTo}
|
|
830
|
+
* @see {@link let_ let}
|
|
831
|
+
*
|
|
832
|
+
* @category do notation
|
|
833
|
+
* @since 2.0.0
|
|
834
|
+
*/
|
|
835
|
+
export const Do: Either<{}> = right({})
|
|
836
|
+
|
|
837
|
+
/**
|
|
838
|
+
* The "do simulation" in Effect 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`.
|
|
839
|
+
*
|
|
840
|
+
* Here's how the do simulation works:
|
|
841
|
+
*
|
|
842
|
+
* 1. Start the do simulation using the `Do` value
|
|
843
|
+
* 2. Within the do simulation scope, you can use the `bind` function to define variables and bind them to `Either` values
|
|
844
|
+
* 3. You can accumulate multiple `bind` statements to define multiple variables within the scope
|
|
845
|
+
* 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values
|
|
846
|
+
*
|
|
847
|
+
* @example
|
|
848
|
+
* ```ts
|
|
849
|
+
* import * as assert from "node:assert"
|
|
850
|
+
* import { Either, pipe } from "effect"
|
|
851
|
+
*
|
|
852
|
+
* const result = pipe(
|
|
853
|
+
* Either.Do,
|
|
854
|
+
* Either.bind("x", () => Either.right(2)),
|
|
855
|
+
* Either.bind("y", () => Either.right(3)),
|
|
856
|
+
* Either.let("sum", ({ x, y }) => x + y)
|
|
857
|
+
* )
|
|
858
|
+
* assert.deepStrictEqual(result, Either.right({ x: 2, y: 3, sum: 5 }))
|
|
859
|
+
* ```
|
|
860
|
+
*
|
|
861
|
+
* @see {@link Do}
|
|
862
|
+
* @see {@link bindTo}
|
|
863
|
+
* @see {@link let_ let}
|
|
864
|
+
*
|
|
865
|
+
* @category do notation
|
|
866
|
+
* @since 2.0.0
|
|
867
|
+
*/
|
|
868
|
+
export const bind: {
|
|
869
|
+
<N extends string, A extends object, B, E2>(
|
|
870
|
+
name: Exclude<N, keyof A>,
|
|
871
|
+
f: (a: NoInfer<A>) => Either<B, E2>
|
|
872
|
+
): <E>(self: Either<A, E>) => Either<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }, E | E2>
|
|
873
|
+
<A extends object, E, N extends string, B, E2>(
|
|
874
|
+
self: Either<A, E>,
|
|
875
|
+
name: Exclude<N, keyof A>,
|
|
876
|
+
f: (a: NoInfer<A>) => Either<B, E2>
|
|
877
|
+
): Either<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }, E | E2>
|
|
878
|
+
} = doNotation.bind<EitherTypeLambda>(map, flatMap)
|
|
879
|
+
|
|
880
|
+
/**
|
|
881
|
+
* The "do simulation" in Effect 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`.
|
|
882
|
+
*
|
|
883
|
+
* Here's how the do simulation works:
|
|
884
|
+
*
|
|
885
|
+
* 1. Start the do simulation using the `Do` value
|
|
886
|
+
* 2. Within the do simulation scope, you can use the `bind` function to define variables and bind them to `Either` values
|
|
887
|
+
* 3. You can accumulate multiple `bind` statements to define multiple variables within the scope
|
|
888
|
+
* 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values
|
|
889
|
+
*
|
|
890
|
+
* @example
|
|
891
|
+
* ```ts
|
|
892
|
+
* import * as assert from "node:assert"
|
|
893
|
+
* import { Either, pipe } from "effect"
|
|
894
|
+
*
|
|
895
|
+
* const result = pipe(
|
|
896
|
+
* Either.Do,
|
|
897
|
+
* Either.bind("x", () => Either.right(2)),
|
|
898
|
+
* Either.bind("y", () => Either.right(3)),
|
|
899
|
+
* Either.let("sum", ({ x, y }) => x + y)
|
|
900
|
+
* )
|
|
901
|
+
* assert.deepStrictEqual(result, Either.right({ x: 2, y: 3, sum: 5 }))
|
|
902
|
+
* ```
|
|
903
|
+
*
|
|
904
|
+
* @see {@link Do}
|
|
905
|
+
* @see {@link bind}
|
|
906
|
+
* @see {@link let_ let}
|
|
907
|
+
*
|
|
908
|
+
* @category do notation
|
|
909
|
+
* @since 2.0.0
|
|
910
|
+
*/
|
|
911
|
+
export const bindTo: {
|
|
912
|
+
<N extends string>(name: N): <A, E>(self: Either<A, E>) => Either<{ [K in N]: A }, E>
|
|
913
|
+
<A, E, N extends string>(self: Either<A, E>, name: N): Either<{ [K in N]: A }, E>
|
|
914
|
+
} = doNotation.bindTo<EitherTypeLambda>(map)
|
|
915
|
+
|
|
916
|
+
const let_: {
|
|
917
|
+
<N extends string, A extends object, B>(
|
|
918
|
+
name: Exclude<N, keyof A>,
|
|
919
|
+
f: (r: NoInfer<A>) => B
|
|
920
|
+
): <E>(self: Either<A, E>) => Either<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }, E>
|
|
921
|
+
<A extends object, E, N extends string, B>(
|
|
922
|
+
self: Either<A, E>,
|
|
923
|
+
name: Exclude<N, keyof A>,
|
|
924
|
+
f: (r: NoInfer<A>) => B
|
|
925
|
+
): Either<{ [K in N | keyof A]: K extends keyof A ? A[K] : B }, E>
|
|
926
|
+
} = doNotation.let_<EitherTypeLambda>(map)
|
|
927
|
+
|
|
928
|
+
export {
|
|
929
|
+
/**
|
|
930
|
+
* The "do simulation" in Effect 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`.
|
|
931
|
+
*
|
|
932
|
+
* Here's how the do simulation works:
|
|
933
|
+
*
|
|
934
|
+
* 1. Start the do simulation using the `Do` value
|
|
935
|
+
* 2. Within the do simulation scope, you can use the `bind` function to define variables and bind them to `Either` values
|
|
936
|
+
* 3. You can accumulate multiple `bind` statements to define multiple variables within the scope
|
|
937
|
+
* 4. Inside the do simulation scope, you can also use the `let` function to define variables and bind them to simple values
|
|
938
|
+
*
|
|
939
|
+
* @example
|
|
940
|
+
* ```ts
|
|
941
|
+
* import * as assert from "node:assert"
|
|
942
|
+
* import { Either, pipe } from "effect"
|
|
943
|
+
*
|
|
944
|
+
* const result = pipe(
|
|
945
|
+
* Either.Do,
|
|
946
|
+
* Either.bind("x", () => Either.right(2)),
|
|
947
|
+
* Either.bind("y", () => Either.right(3)),
|
|
948
|
+
* Either.let("sum", ({ x, y }) => x + y)
|
|
949
|
+
* )
|
|
950
|
+
* assert.deepStrictEqual(result, Either.right({ x: 2, y: 3, sum: 5 }))
|
|
951
|
+
* ```
|
|
952
|
+
*
|
|
953
|
+
* @see {@link Do}
|
|
954
|
+
* @see {@link bindTo}
|
|
955
|
+
* @see {@link bind}
|
|
956
|
+
*
|
|
957
|
+
* @category do notation
|
|
958
|
+
* @since 2.0.0
|
|
959
|
+
*/
|
|
960
|
+
let_ as let
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
/**
|
|
964
|
+
* Converts an `Option` of an `Either` into an `Either` of an `Option`.
|
|
965
|
+
*
|
|
966
|
+
* **Details**
|
|
967
|
+
*
|
|
968
|
+
* This function transforms an `Option<Either<A, E>>` into an
|
|
969
|
+
* `Either<Option<A>, E>`. If the `Option` is `None`, the resulting `Either`
|
|
970
|
+
* will be a `Right` with a `None` value. If the `Option` is `Some`, the
|
|
971
|
+
* inner `Either` will be executed, and its result wrapped in a `Some`.
|
|
972
|
+
*
|
|
973
|
+
* @example
|
|
974
|
+
* ```ts
|
|
975
|
+
* import { Effect, Either, Option } from "effect"
|
|
976
|
+
*
|
|
977
|
+
* // ┌─── Option<Either<number, never>>
|
|
978
|
+
* // ▼
|
|
979
|
+
* const maybe = Option.some(Either.right(42))
|
|
980
|
+
*
|
|
981
|
+
* // ┌─── Either<Option<number>, never, never>
|
|
982
|
+
* // ▼
|
|
983
|
+
* const result = Either.transposeOption(maybe)
|
|
984
|
+
*
|
|
985
|
+
* console.log(Effect.runSync(result))
|
|
986
|
+
* // Output: { _id: 'Option', _tag: 'Some', value: 42 }
|
|
987
|
+
* ```
|
|
988
|
+
*
|
|
989
|
+
* @since 3.14.0
|
|
990
|
+
* @category Optional Wrapping & Unwrapping
|
|
991
|
+
*/
|
|
992
|
+
export const transposeOption = <A = never, E = never>(
|
|
993
|
+
self: Option<Either<A, E>>
|
|
994
|
+
): Either<Option<A>, E> => {
|
|
995
|
+
return option_.isNone(self) ? right(option_.none) : map(self.value, option_.some)
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* Applies an `Either` on an `Option` and transposes the result.
|
|
1000
|
+
*
|
|
1001
|
+
* **Details**
|
|
1002
|
+
*
|
|
1003
|
+
* If the `Option` is `None`, the resulting `Either` will immediately succeed with a `Right` value of `None`.
|
|
1004
|
+
* If the `Option` is `Some`, the transformation function will be applied to the inner value, and its result wrapped in a `Some`.
|
|
1005
|
+
*
|
|
1006
|
+
* @example
|
|
1007
|
+
* ```ts
|
|
1008
|
+
* import { Either, Option, pipe } from "effect"
|
|
1009
|
+
*
|
|
1010
|
+
* // ┌─── Either<Option<number>, never>>
|
|
1011
|
+
* // ▼
|
|
1012
|
+
* const noneResult = pipe(
|
|
1013
|
+
* Option.none(),
|
|
1014
|
+
* Either.transposeMapOption(() => Either.right(42)) // will not be executed
|
|
1015
|
+
* )
|
|
1016
|
+
* console.log(noneResult)
|
|
1017
|
+
* // Output: { _id: 'Either', _tag: 'Right', right: { _id: 'Option', _tag: 'None' } }
|
|
1018
|
+
*
|
|
1019
|
+
* // ┌─── Either<Option<number>, never>>
|
|
1020
|
+
* // ▼
|
|
1021
|
+
* const someRightResult = pipe(
|
|
1022
|
+
* Option.some(42),
|
|
1023
|
+
* Either.transposeMapOption((value) => Either.right(value * 2))
|
|
1024
|
+
* )
|
|
1025
|
+
* console.log(someRightResult)
|
|
1026
|
+
* // Output: { _id: 'Either', _tag: 'Right', right: { _id: 'Option', _tag: 'Some', value: 84 } }
|
|
1027
|
+
* ```
|
|
1028
|
+
*
|
|
1029
|
+
* @since 3.15.0
|
|
1030
|
+
* @category Optional Wrapping & Unwrapping
|
|
1031
|
+
*/
|
|
1032
|
+
export const transposeMapOption = dual<
|
|
1033
|
+
<A, B, E = never>(
|
|
1034
|
+
f: (self: A) => Either<B, E>
|
|
1035
|
+
) => (self: Option<A>) => Either<Option<B>, E>,
|
|
1036
|
+
<A, B, E = never>(
|
|
1037
|
+
self: Option<A>,
|
|
1038
|
+
f: (self: A) => Either<B, E>
|
|
1039
|
+
) => Either<Option<B>, E>
|
|
1040
|
+
>(2, (self, f) => option_.isNone(self) ? right(option_.none) : map(f(self.value), option_.some))
|