@clayroach/effect 3.19.14-source-capture.8 → 3.19.14-source-trace.1
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/Duration.ts
ADDED
|
@@ -0,0 +1,1000 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 2.0.0
|
|
3
|
+
*/
|
|
4
|
+
import * as Equal from "./Equal.js"
|
|
5
|
+
import type * as equivalence from "./Equivalence.js"
|
|
6
|
+
import { dual } from "./Function.js"
|
|
7
|
+
import * as Hash from "./Hash.js"
|
|
8
|
+
import type { Inspectable } from "./Inspectable.js"
|
|
9
|
+
import { NodeInspectSymbol } from "./Inspectable.js"
|
|
10
|
+
import * as Option from "./Option.js"
|
|
11
|
+
import * as order from "./Order.js"
|
|
12
|
+
import type { Pipeable } from "./Pipeable.js"
|
|
13
|
+
import { pipeArguments } from "./Pipeable.js"
|
|
14
|
+
import { hasProperty, isBigInt, isNumber, isString } from "./Predicate.js"
|
|
15
|
+
|
|
16
|
+
const TypeId: unique symbol = Symbol.for("effect/Duration")
|
|
17
|
+
|
|
18
|
+
const bigint0 = BigInt(0)
|
|
19
|
+
const bigint24 = BigInt(24)
|
|
20
|
+
const bigint60 = BigInt(60)
|
|
21
|
+
const bigint1e3 = BigInt(1_000)
|
|
22
|
+
const bigint1e6 = BigInt(1_000_000)
|
|
23
|
+
const bigint1e9 = BigInt(1_000_000_000)
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @since 2.0.0
|
|
27
|
+
* @category symbol
|
|
28
|
+
*/
|
|
29
|
+
export type TypeId = typeof TypeId
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @since 2.0.0
|
|
33
|
+
* @category models
|
|
34
|
+
*/
|
|
35
|
+
export interface Duration extends Equal.Equal, Pipeable, Inspectable {
|
|
36
|
+
readonly [TypeId]: TypeId
|
|
37
|
+
readonly value: DurationValue
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @since 2.0.0
|
|
41
|
+
* @category models
|
|
42
|
+
*/
|
|
43
|
+
export type DurationValue =
|
|
44
|
+
| {
|
|
45
|
+
readonly _tag: "Millis"
|
|
46
|
+
readonly millis: number
|
|
47
|
+
}
|
|
48
|
+
| {
|
|
49
|
+
readonly _tag: "Nanos"
|
|
50
|
+
readonly nanos: bigint
|
|
51
|
+
}
|
|
52
|
+
| {
|
|
53
|
+
readonly _tag: "Infinity"
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @since 2.0.0
|
|
58
|
+
* @category models
|
|
59
|
+
*/
|
|
60
|
+
export type Unit =
|
|
61
|
+
| "nano"
|
|
62
|
+
| "nanos"
|
|
63
|
+
| "micro"
|
|
64
|
+
| "micros"
|
|
65
|
+
| "milli"
|
|
66
|
+
| "millis"
|
|
67
|
+
| "second"
|
|
68
|
+
| "seconds"
|
|
69
|
+
| "minute"
|
|
70
|
+
| "minutes"
|
|
71
|
+
| "hour"
|
|
72
|
+
| "hours"
|
|
73
|
+
| "day"
|
|
74
|
+
| "days"
|
|
75
|
+
| "week"
|
|
76
|
+
| "weeks"
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @since 2.0.0
|
|
80
|
+
* @category models
|
|
81
|
+
*/
|
|
82
|
+
export type DurationInput =
|
|
83
|
+
| Duration
|
|
84
|
+
| number // millis
|
|
85
|
+
| bigint // nanos
|
|
86
|
+
| readonly [seconds: number, nanos: number]
|
|
87
|
+
| `${number} ${Unit}`
|
|
88
|
+
|
|
89
|
+
const DURATION_REGEX = /^(-?\d+(?:\.\d+)?)\s+(nanos?|micros?|millis?|seconds?|minutes?|hours?|days?|weeks?)$/
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* @since 2.0.0
|
|
93
|
+
*/
|
|
94
|
+
export const decode = (input: DurationInput): Duration => {
|
|
95
|
+
if (isDuration(input)) {
|
|
96
|
+
return input
|
|
97
|
+
} else if (isNumber(input)) {
|
|
98
|
+
return millis(input)
|
|
99
|
+
} else if (isBigInt(input)) {
|
|
100
|
+
return nanos(input)
|
|
101
|
+
} else if (Array.isArray(input) && input.length === 2 && input.every(isNumber)) {
|
|
102
|
+
if (input[0] === -Infinity || input[1] === -Infinity || Number.isNaN(input[0]) || Number.isNaN(input[1])) {
|
|
103
|
+
return zero
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (input[0] === Infinity || input[1] === Infinity) {
|
|
107
|
+
return infinity
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return nanos(BigInt(Math.round(input[0] * 1_000_000_000)) + BigInt(Math.round(input[1])))
|
|
111
|
+
} else if (isString(input)) {
|
|
112
|
+
const match = DURATION_REGEX.exec(input)
|
|
113
|
+
if (match) {
|
|
114
|
+
const [_, valueStr, unit] = match
|
|
115
|
+
const value = Number(valueStr)
|
|
116
|
+
switch (unit) {
|
|
117
|
+
case "nano":
|
|
118
|
+
case "nanos":
|
|
119
|
+
return nanos(BigInt(valueStr))
|
|
120
|
+
case "micro":
|
|
121
|
+
case "micros":
|
|
122
|
+
return micros(BigInt(valueStr))
|
|
123
|
+
case "milli":
|
|
124
|
+
case "millis":
|
|
125
|
+
return millis(value)
|
|
126
|
+
case "second":
|
|
127
|
+
case "seconds":
|
|
128
|
+
return seconds(value)
|
|
129
|
+
case "minute":
|
|
130
|
+
case "minutes":
|
|
131
|
+
return minutes(value)
|
|
132
|
+
case "hour":
|
|
133
|
+
case "hours":
|
|
134
|
+
return hours(value)
|
|
135
|
+
case "day":
|
|
136
|
+
case "days":
|
|
137
|
+
return days(value)
|
|
138
|
+
case "week":
|
|
139
|
+
case "weeks":
|
|
140
|
+
return weeks(value)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
throw new Error("Invalid DurationInput")
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @since 2.5.0
|
|
149
|
+
*/
|
|
150
|
+
export const decodeUnknown: (u: unknown) => Option.Option<Duration> = Option.liftThrowable(decode) as any
|
|
151
|
+
|
|
152
|
+
const zeroValue: DurationValue = { _tag: "Millis", millis: 0 }
|
|
153
|
+
const infinityValue: DurationValue = { _tag: "Infinity" }
|
|
154
|
+
|
|
155
|
+
const DurationProto: Omit<Duration, "value"> = {
|
|
156
|
+
[TypeId]: TypeId,
|
|
157
|
+
[Hash.symbol](this: Duration) {
|
|
158
|
+
return Hash.cached(this, Hash.structure(this.value))
|
|
159
|
+
},
|
|
160
|
+
[Equal.symbol](this: Duration, that: unknown): boolean {
|
|
161
|
+
return isDuration(that) && equals(this, that)
|
|
162
|
+
},
|
|
163
|
+
toString(this: Duration) {
|
|
164
|
+
return `Duration(${format(this)})`
|
|
165
|
+
},
|
|
166
|
+
toJSON(this: Duration) {
|
|
167
|
+
switch (this.value._tag) {
|
|
168
|
+
case "Millis":
|
|
169
|
+
return { _id: "Duration", _tag: "Millis", millis: this.value.millis }
|
|
170
|
+
case "Nanos":
|
|
171
|
+
return { _id: "Duration", _tag: "Nanos", hrtime: toHrTime(this) }
|
|
172
|
+
case "Infinity":
|
|
173
|
+
return { _id: "Duration", _tag: "Infinity" }
|
|
174
|
+
}
|
|
175
|
+
},
|
|
176
|
+
[NodeInspectSymbol]() {
|
|
177
|
+
return this.toJSON()
|
|
178
|
+
},
|
|
179
|
+
pipe() {
|
|
180
|
+
return pipeArguments(this, arguments)
|
|
181
|
+
}
|
|
182
|
+
} as const
|
|
183
|
+
|
|
184
|
+
const make = (input: number | bigint): Duration => {
|
|
185
|
+
const duration = Object.create(DurationProto)
|
|
186
|
+
if (isNumber(input)) {
|
|
187
|
+
if (isNaN(input) || input <= 0) {
|
|
188
|
+
duration.value = zeroValue
|
|
189
|
+
} else if (!Number.isFinite(input)) {
|
|
190
|
+
duration.value = infinityValue
|
|
191
|
+
} else if (!Number.isInteger(input)) {
|
|
192
|
+
duration.value = { _tag: "Nanos", nanos: BigInt(Math.round(input * 1_000_000)) }
|
|
193
|
+
} else {
|
|
194
|
+
duration.value = { _tag: "Millis", millis: input }
|
|
195
|
+
}
|
|
196
|
+
} else if (input <= bigint0) {
|
|
197
|
+
duration.value = zeroValue
|
|
198
|
+
} else {
|
|
199
|
+
duration.value = { _tag: "Nanos", nanos: input }
|
|
200
|
+
}
|
|
201
|
+
return duration
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* @since 2.0.0
|
|
206
|
+
* @category guards
|
|
207
|
+
*/
|
|
208
|
+
export const isDuration = (u: unknown): u is Duration => hasProperty(u, TypeId)
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* @since 2.0.0
|
|
212
|
+
* @category guards
|
|
213
|
+
*/
|
|
214
|
+
export const isFinite = (self: Duration): boolean => self.value._tag !== "Infinity"
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* @since 3.5.0
|
|
218
|
+
* @category guards
|
|
219
|
+
*/
|
|
220
|
+
export const isZero = (self: Duration): boolean => {
|
|
221
|
+
switch (self.value._tag) {
|
|
222
|
+
case "Millis": {
|
|
223
|
+
return self.value.millis === 0
|
|
224
|
+
}
|
|
225
|
+
case "Nanos": {
|
|
226
|
+
return self.value.nanos === bigint0
|
|
227
|
+
}
|
|
228
|
+
case "Infinity": {
|
|
229
|
+
return false
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* @since 2.0.0
|
|
236
|
+
* @category constructors
|
|
237
|
+
*/
|
|
238
|
+
export const zero: Duration = make(0)
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* @since 2.0.0
|
|
242
|
+
* @category constructors
|
|
243
|
+
*/
|
|
244
|
+
export const infinity: Duration = make(Infinity)
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* @since 2.0.0
|
|
248
|
+
* @category constructors
|
|
249
|
+
*/
|
|
250
|
+
export const nanos = (nanos: bigint): Duration => make(nanos)
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* @since 2.0.0
|
|
254
|
+
* @category constructors
|
|
255
|
+
*/
|
|
256
|
+
export const micros = (micros: bigint): Duration => make(micros * bigint1e3)
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* @since 2.0.0
|
|
260
|
+
* @category constructors
|
|
261
|
+
*/
|
|
262
|
+
export const millis = (millis: number): Duration => make(millis)
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* @since 2.0.0
|
|
266
|
+
* @category constructors
|
|
267
|
+
*/
|
|
268
|
+
export const seconds = (seconds: number): Duration => make(seconds * 1000)
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* @since 2.0.0
|
|
272
|
+
* @category constructors
|
|
273
|
+
*/
|
|
274
|
+
export const minutes = (minutes: number): Duration => make(minutes * 60_000)
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* @since 2.0.0
|
|
278
|
+
* @category constructors
|
|
279
|
+
*/
|
|
280
|
+
export const hours = (hours: number): Duration => make(hours * 3_600_000)
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* @since 2.0.0
|
|
284
|
+
* @category constructors
|
|
285
|
+
*/
|
|
286
|
+
export const days = (days: number): Duration => make(days * 86_400_000)
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* @since 2.0.0
|
|
290
|
+
* @category constructors
|
|
291
|
+
*/
|
|
292
|
+
export const weeks = (weeks: number): Duration => make(weeks * 604_800_000)
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* @since 2.0.0
|
|
296
|
+
* @category getters
|
|
297
|
+
*/
|
|
298
|
+
export const toMillis = (self: DurationInput): number =>
|
|
299
|
+
match(self, {
|
|
300
|
+
onMillis: (millis) => millis,
|
|
301
|
+
onNanos: (nanos) => Number(nanos) / 1_000_000
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* @since 2.0.0
|
|
306
|
+
* @category getters
|
|
307
|
+
*/
|
|
308
|
+
export const toSeconds = (self: DurationInput): number =>
|
|
309
|
+
match(self, {
|
|
310
|
+
onMillis: (millis) => millis / 1_000,
|
|
311
|
+
onNanos: (nanos) => Number(nanos) / 1_000_000_000
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* @since 3.8.0
|
|
316
|
+
* @category getters
|
|
317
|
+
*/
|
|
318
|
+
export const toMinutes = (self: DurationInput): number =>
|
|
319
|
+
match(self, {
|
|
320
|
+
onMillis: (millis) => millis / 60_000,
|
|
321
|
+
onNanos: (nanos) => Number(nanos) / 60_000_000_000
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* @since 3.8.0
|
|
326
|
+
* @category getters
|
|
327
|
+
*/
|
|
328
|
+
export const toHours = (self: DurationInput): number =>
|
|
329
|
+
match(self, {
|
|
330
|
+
onMillis: (millis) => millis / 3_600_000,
|
|
331
|
+
onNanos: (nanos) => Number(nanos) / 3_600_000_000_000
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* @since 3.8.0
|
|
336
|
+
* @category getters
|
|
337
|
+
*/
|
|
338
|
+
export const toDays = (self: DurationInput): number =>
|
|
339
|
+
match(self, {
|
|
340
|
+
onMillis: (millis) => millis / 86_400_000,
|
|
341
|
+
onNanos: (nanos) => Number(nanos) / 86_400_000_000_000
|
|
342
|
+
})
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* @since 3.8.0
|
|
346
|
+
* @category getters
|
|
347
|
+
*/
|
|
348
|
+
export const toWeeks = (self: DurationInput): number =>
|
|
349
|
+
match(self, {
|
|
350
|
+
onMillis: (millis) => millis / 604_800_000,
|
|
351
|
+
onNanos: (nanos) => Number(nanos) / 604_800_000_000_000
|
|
352
|
+
})
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Get the duration in nanoseconds as a bigint.
|
|
356
|
+
*
|
|
357
|
+
* If the duration is infinite, returns `Option.none()`
|
|
358
|
+
*
|
|
359
|
+
* @since 2.0.0
|
|
360
|
+
* @category getters
|
|
361
|
+
*/
|
|
362
|
+
export const toNanos = (self: DurationInput): Option.Option<bigint> => {
|
|
363
|
+
const _self = decode(self)
|
|
364
|
+
switch (_self.value._tag) {
|
|
365
|
+
case "Infinity":
|
|
366
|
+
return Option.none()
|
|
367
|
+
case "Nanos":
|
|
368
|
+
return Option.some(_self.value.nanos)
|
|
369
|
+
case "Millis":
|
|
370
|
+
return Option.some(BigInt(Math.round(_self.value.millis * 1_000_000)))
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Get the duration in nanoseconds as a bigint.
|
|
376
|
+
*
|
|
377
|
+
* If the duration is infinite, it throws an error.
|
|
378
|
+
*
|
|
379
|
+
* @since 2.0.0
|
|
380
|
+
* @category getters
|
|
381
|
+
*/
|
|
382
|
+
export const unsafeToNanos = (self: DurationInput): bigint => {
|
|
383
|
+
const _self = decode(self)
|
|
384
|
+
switch (_self.value._tag) {
|
|
385
|
+
case "Infinity":
|
|
386
|
+
throw new Error("Cannot convert infinite duration to nanos")
|
|
387
|
+
case "Nanos":
|
|
388
|
+
return _self.value.nanos
|
|
389
|
+
case "Millis":
|
|
390
|
+
return BigInt(Math.round(_self.value.millis * 1_000_000))
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* @since 2.0.0
|
|
396
|
+
* @category getters
|
|
397
|
+
*/
|
|
398
|
+
export const toHrTime = (self: DurationInput): [seconds: number, nanos: number] => {
|
|
399
|
+
const _self = decode(self)
|
|
400
|
+
switch (_self.value._tag) {
|
|
401
|
+
case "Infinity":
|
|
402
|
+
return [Infinity, 0]
|
|
403
|
+
case "Nanos":
|
|
404
|
+
return [
|
|
405
|
+
Number(_self.value.nanos / bigint1e9),
|
|
406
|
+
Number(_self.value.nanos % bigint1e9)
|
|
407
|
+
]
|
|
408
|
+
case "Millis":
|
|
409
|
+
return [
|
|
410
|
+
Math.floor(_self.value.millis / 1000),
|
|
411
|
+
Math.round((_self.value.millis % 1000) * 1_000_000)
|
|
412
|
+
]
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* @since 2.0.0
|
|
418
|
+
* @category pattern matching
|
|
419
|
+
*/
|
|
420
|
+
export const match: {
|
|
421
|
+
<A, B>(
|
|
422
|
+
options: {
|
|
423
|
+
readonly onMillis: (millis: number) => A
|
|
424
|
+
readonly onNanos: (nanos: bigint) => B
|
|
425
|
+
}
|
|
426
|
+
): (self: DurationInput) => A | B
|
|
427
|
+
<A, B>(
|
|
428
|
+
self: DurationInput,
|
|
429
|
+
options: {
|
|
430
|
+
readonly onMillis: (millis: number) => A
|
|
431
|
+
readonly onNanos: (nanos: bigint) => B
|
|
432
|
+
}
|
|
433
|
+
): A | B
|
|
434
|
+
} = dual(2, <A, B>(
|
|
435
|
+
self: DurationInput,
|
|
436
|
+
options: {
|
|
437
|
+
readonly onMillis: (millis: number) => A
|
|
438
|
+
readonly onNanos: (nanos: bigint) => B
|
|
439
|
+
}
|
|
440
|
+
): A | B => {
|
|
441
|
+
const _self = decode(self)
|
|
442
|
+
switch (_self.value._tag) {
|
|
443
|
+
case "Nanos":
|
|
444
|
+
return options.onNanos(_self.value.nanos)
|
|
445
|
+
case "Infinity":
|
|
446
|
+
return options.onMillis(Infinity)
|
|
447
|
+
case "Millis":
|
|
448
|
+
return options.onMillis(_self.value.millis)
|
|
449
|
+
}
|
|
450
|
+
})
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* @since 2.0.0
|
|
454
|
+
* @category pattern matching
|
|
455
|
+
*/
|
|
456
|
+
export const matchWith: {
|
|
457
|
+
<A, B>(
|
|
458
|
+
that: DurationInput,
|
|
459
|
+
options: {
|
|
460
|
+
readonly onMillis: (self: number, that: number) => A
|
|
461
|
+
readonly onNanos: (self: bigint, that: bigint) => B
|
|
462
|
+
}
|
|
463
|
+
): (self: DurationInput) => A | B
|
|
464
|
+
<A, B>(
|
|
465
|
+
self: DurationInput,
|
|
466
|
+
that: DurationInput,
|
|
467
|
+
options: {
|
|
468
|
+
readonly onMillis: (self: number, that: number) => A
|
|
469
|
+
readonly onNanos: (self: bigint, that: bigint) => B
|
|
470
|
+
}
|
|
471
|
+
): A | B
|
|
472
|
+
} = dual(3, <A, B>(
|
|
473
|
+
self: DurationInput,
|
|
474
|
+
that: DurationInput,
|
|
475
|
+
options: {
|
|
476
|
+
readonly onMillis: (self: number, that: number) => A
|
|
477
|
+
readonly onNanos: (self: bigint, that: bigint) => B
|
|
478
|
+
}
|
|
479
|
+
): A | B => {
|
|
480
|
+
const _self = decode(self)
|
|
481
|
+
const _that = decode(that)
|
|
482
|
+
if (_self.value._tag === "Infinity" || _that.value._tag === "Infinity") {
|
|
483
|
+
return options.onMillis(
|
|
484
|
+
toMillis(_self),
|
|
485
|
+
toMillis(_that)
|
|
486
|
+
)
|
|
487
|
+
} else if (_self.value._tag === "Nanos" || _that.value._tag === "Nanos") {
|
|
488
|
+
const selfNanos = _self.value._tag === "Nanos" ?
|
|
489
|
+
_self.value.nanos :
|
|
490
|
+
BigInt(Math.round(_self.value.millis * 1_000_000))
|
|
491
|
+
const thatNanos = _that.value._tag === "Nanos" ?
|
|
492
|
+
_that.value.nanos :
|
|
493
|
+
BigInt(Math.round(_that.value.millis * 1_000_000))
|
|
494
|
+
return options.onNanos(selfNanos, thatNanos)
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
return options.onMillis(
|
|
498
|
+
_self.value.millis,
|
|
499
|
+
_that.value.millis
|
|
500
|
+
)
|
|
501
|
+
})
|
|
502
|
+
|
|
503
|
+
/**
|
|
504
|
+
* @category instances
|
|
505
|
+
* @since 2.0.0
|
|
506
|
+
*/
|
|
507
|
+
export const Order: order.Order<Duration> = order.make((self, that) =>
|
|
508
|
+
matchWith(self, that, {
|
|
509
|
+
onMillis: (self, that) => (self < that ? -1 : self > that ? 1 : 0),
|
|
510
|
+
onNanos: (self, that) => (self < that ? -1 : self > that ? 1 : 0)
|
|
511
|
+
})
|
|
512
|
+
)
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Checks if a `Duration` is between a `minimum` and `maximum` value.
|
|
516
|
+
*
|
|
517
|
+
* @category predicates
|
|
518
|
+
* @since 2.0.0
|
|
519
|
+
*/
|
|
520
|
+
export const between: {
|
|
521
|
+
(options: {
|
|
522
|
+
minimum: DurationInput
|
|
523
|
+
maximum: DurationInput
|
|
524
|
+
}): (self: DurationInput) => boolean
|
|
525
|
+
(self: DurationInput, options: {
|
|
526
|
+
minimum: DurationInput
|
|
527
|
+
maximum: DurationInput
|
|
528
|
+
}): boolean
|
|
529
|
+
} = order.between(order.mapInput(Order, decode))
|
|
530
|
+
|
|
531
|
+
/**
|
|
532
|
+
* @category instances
|
|
533
|
+
* @since 2.0.0
|
|
534
|
+
*/
|
|
535
|
+
export const Equivalence: equivalence.Equivalence<Duration> = (self, that) =>
|
|
536
|
+
matchWith(self, that, {
|
|
537
|
+
onMillis: (self, that) => self === that,
|
|
538
|
+
onNanos: (self, that) => self === that
|
|
539
|
+
})
|
|
540
|
+
|
|
541
|
+
const _min = order.min(Order)
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* @since 2.0.0
|
|
545
|
+
*/
|
|
546
|
+
export const min: {
|
|
547
|
+
(that: DurationInput): (self: DurationInput) => Duration
|
|
548
|
+
(self: DurationInput, that: DurationInput): Duration
|
|
549
|
+
} = dual(2, (self: DurationInput, that: DurationInput): Duration => _min(decode(self), decode(that)))
|
|
550
|
+
|
|
551
|
+
const _max = order.max(Order)
|
|
552
|
+
|
|
553
|
+
/**
|
|
554
|
+
* @since 2.0.0
|
|
555
|
+
* @category order
|
|
556
|
+
*/
|
|
557
|
+
export const max: {
|
|
558
|
+
(that: DurationInput): (self: DurationInput) => Duration
|
|
559
|
+
(self: DurationInput, that: DurationInput): Duration
|
|
560
|
+
} = dual(2, (self: DurationInput, that: DurationInput): Duration => _max(decode(self), decode(that)))
|
|
561
|
+
|
|
562
|
+
const _clamp = order.clamp(Order)
|
|
563
|
+
|
|
564
|
+
/**
|
|
565
|
+
* @since 2.0.0
|
|
566
|
+
* @category order
|
|
567
|
+
*/
|
|
568
|
+
export const clamp: {
|
|
569
|
+
(options: {
|
|
570
|
+
minimum: DurationInput
|
|
571
|
+
maximum: DurationInput
|
|
572
|
+
}): (self: DurationInput) => Duration
|
|
573
|
+
(self: DurationInput, options: {
|
|
574
|
+
minimum: DurationInput
|
|
575
|
+
maximum: DurationInput
|
|
576
|
+
}): Duration
|
|
577
|
+
} = dual(
|
|
578
|
+
2,
|
|
579
|
+
(self: DurationInput, options: {
|
|
580
|
+
minimum: DurationInput
|
|
581
|
+
maximum: DurationInput
|
|
582
|
+
}): Duration =>
|
|
583
|
+
_clamp(decode(self), {
|
|
584
|
+
minimum: decode(options.minimum),
|
|
585
|
+
maximum: decode(options.maximum)
|
|
586
|
+
})
|
|
587
|
+
)
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* @since 2.4.19
|
|
591
|
+
* @category math
|
|
592
|
+
*/
|
|
593
|
+
export const divide: {
|
|
594
|
+
(by: number): (self: DurationInput) => Option.Option<Duration>
|
|
595
|
+
(self: DurationInput, by: number): Option.Option<Duration>
|
|
596
|
+
} = dual(
|
|
597
|
+
2,
|
|
598
|
+
(self: DurationInput, by: number): Option.Option<Duration> =>
|
|
599
|
+
match(self, {
|
|
600
|
+
onMillis: (millis) => {
|
|
601
|
+
if (by === 0 || isNaN(by) || !Number.isFinite(by)) {
|
|
602
|
+
return Option.none()
|
|
603
|
+
}
|
|
604
|
+
return Option.some(make(millis / by))
|
|
605
|
+
},
|
|
606
|
+
onNanos: (nanos) => {
|
|
607
|
+
if (isNaN(by) || by <= 0 || !Number.isFinite(by)) {
|
|
608
|
+
return Option.none()
|
|
609
|
+
}
|
|
610
|
+
try {
|
|
611
|
+
return Option.some(make(nanos / BigInt(by)))
|
|
612
|
+
} catch {
|
|
613
|
+
return Option.none()
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
})
|
|
617
|
+
)
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* @since 2.4.19
|
|
621
|
+
* @category math
|
|
622
|
+
*/
|
|
623
|
+
export const unsafeDivide: {
|
|
624
|
+
(by: number): (self: DurationInput) => Duration
|
|
625
|
+
(self: DurationInput, by: number): Duration
|
|
626
|
+
} = dual(
|
|
627
|
+
2,
|
|
628
|
+
(self: DurationInput, by: number): Duration =>
|
|
629
|
+
match(self, {
|
|
630
|
+
onMillis: (millis) => make(millis / by),
|
|
631
|
+
onNanos: (nanos) => {
|
|
632
|
+
if (isNaN(by) || by < 0 || Object.is(by, -0)) {
|
|
633
|
+
return zero
|
|
634
|
+
} else if (Object.is(by, 0) || !Number.isFinite(by)) {
|
|
635
|
+
return infinity
|
|
636
|
+
}
|
|
637
|
+
return make(nanos / BigInt(by))
|
|
638
|
+
}
|
|
639
|
+
})
|
|
640
|
+
)
|
|
641
|
+
|
|
642
|
+
/**
|
|
643
|
+
* @since 2.0.0
|
|
644
|
+
* @category math
|
|
645
|
+
*/
|
|
646
|
+
export const times: {
|
|
647
|
+
(times: number): (self: DurationInput) => Duration
|
|
648
|
+
(self: DurationInput, times: number): Duration
|
|
649
|
+
} = dual(
|
|
650
|
+
2,
|
|
651
|
+
(self: DurationInput, times: number): Duration =>
|
|
652
|
+
match(self, {
|
|
653
|
+
onMillis: (millis) => make(millis * times),
|
|
654
|
+
onNanos: (nanos) => make(nanos * BigInt(times))
|
|
655
|
+
})
|
|
656
|
+
)
|
|
657
|
+
|
|
658
|
+
/**
|
|
659
|
+
* @since 2.0.0
|
|
660
|
+
* @category math
|
|
661
|
+
*/
|
|
662
|
+
export const subtract: {
|
|
663
|
+
(that: DurationInput): (self: DurationInput) => Duration
|
|
664
|
+
(self: DurationInput, that: DurationInput): Duration
|
|
665
|
+
} = dual(
|
|
666
|
+
2,
|
|
667
|
+
(self: DurationInput, that: DurationInput): Duration =>
|
|
668
|
+
matchWith(self, that, {
|
|
669
|
+
onMillis: (self, that) => make(self - that),
|
|
670
|
+
onNanos: (self, that) => make(self - that)
|
|
671
|
+
})
|
|
672
|
+
)
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
* @since 2.0.0
|
|
676
|
+
* @category math
|
|
677
|
+
*/
|
|
678
|
+
export const sum: {
|
|
679
|
+
(that: DurationInput): (self: DurationInput) => Duration
|
|
680
|
+
(self: DurationInput, that: DurationInput): Duration
|
|
681
|
+
} = dual(
|
|
682
|
+
2,
|
|
683
|
+
(self: DurationInput, that: DurationInput): Duration =>
|
|
684
|
+
matchWith(self, that, {
|
|
685
|
+
onMillis: (self, that) => make(self + that),
|
|
686
|
+
onNanos: (self, that) => make(self + that)
|
|
687
|
+
})
|
|
688
|
+
)
|
|
689
|
+
|
|
690
|
+
/**
|
|
691
|
+
* @since 2.0.0
|
|
692
|
+
* @category predicates
|
|
693
|
+
*/
|
|
694
|
+
export const lessThan: {
|
|
695
|
+
(that: DurationInput): (self: DurationInput) => boolean
|
|
696
|
+
(self: DurationInput, that: DurationInput): boolean
|
|
697
|
+
} = dual(
|
|
698
|
+
2,
|
|
699
|
+
(self: DurationInput, that: DurationInput): boolean =>
|
|
700
|
+
matchWith(self, that, {
|
|
701
|
+
onMillis: (self, that) => self < that,
|
|
702
|
+
onNanos: (self, that) => self < that
|
|
703
|
+
})
|
|
704
|
+
)
|
|
705
|
+
|
|
706
|
+
/**
|
|
707
|
+
* @since 2.0.0
|
|
708
|
+
* @category predicates
|
|
709
|
+
*/
|
|
710
|
+
export const lessThanOrEqualTo: {
|
|
711
|
+
(that: DurationInput): (self: DurationInput) => boolean
|
|
712
|
+
(self: DurationInput, that: DurationInput): boolean
|
|
713
|
+
} = dual(
|
|
714
|
+
2,
|
|
715
|
+
(self: DurationInput, that: DurationInput): boolean =>
|
|
716
|
+
matchWith(self, that, {
|
|
717
|
+
onMillis: (self, that) => self <= that,
|
|
718
|
+
onNanos: (self, that) => self <= that
|
|
719
|
+
})
|
|
720
|
+
)
|
|
721
|
+
|
|
722
|
+
/**
|
|
723
|
+
* @since 2.0.0
|
|
724
|
+
* @category predicates
|
|
725
|
+
*/
|
|
726
|
+
export const greaterThan: {
|
|
727
|
+
(that: DurationInput): (self: DurationInput) => boolean
|
|
728
|
+
(self: DurationInput, that: DurationInput): boolean
|
|
729
|
+
} = dual(
|
|
730
|
+
2,
|
|
731
|
+
(self: DurationInput, that: DurationInput): boolean =>
|
|
732
|
+
matchWith(self, that, {
|
|
733
|
+
onMillis: (self, that) => self > that,
|
|
734
|
+
onNanos: (self, that) => self > that
|
|
735
|
+
})
|
|
736
|
+
)
|
|
737
|
+
|
|
738
|
+
/**
|
|
739
|
+
* @since 2.0.0
|
|
740
|
+
* @category predicates
|
|
741
|
+
*/
|
|
742
|
+
export const greaterThanOrEqualTo: {
|
|
743
|
+
(that: DurationInput): (self: DurationInput) => boolean
|
|
744
|
+
(self: DurationInput, that: DurationInput): boolean
|
|
745
|
+
} = dual(
|
|
746
|
+
2,
|
|
747
|
+
(self: DurationInput, that: DurationInput): boolean =>
|
|
748
|
+
matchWith(self, that, {
|
|
749
|
+
onMillis: (self, that) => self >= that,
|
|
750
|
+
onNanos: (self, that) => self >= that
|
|
751
|
+
})
|
|
752
|
+
)
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* @since 2.0.0
|
|
756
|
+
* @category predicates
|
|
757
|
+
*/
|
|
758
|
+
export const equals: {
|
|
759
|
+
(that: DurationInput): (self: DurationInput) => boolean
|
|
760
|
+
(self: DurationInput, that: DurationInput): boolean
|
|
761
|
+
} = dual(2, (self: DurationInput, that: DurationInput): boolean => Equivalence(decode(self), decode(that)))
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Converts a `Duration` to its parts.
|
|
765
|
+
*
|
|
766
|
+
* @since 3.8.0
|
|
767
|
+
* @category conversions
|
|
768
|
+
*/
|
|
769
|
+
export const parts = (self: DurationInput): {
|
|
770
|
+
days: number
|
|
771
|
+
hours: number
|
|
772
|
+
minutes: number
|
|
773
|
+
seconds: number
|
|
774
|
+
millis: number
|
|
775
|
+
nanos: number
|
|
776
|
+
} => {
|
|
777
|
+
const duration = decode(self)
|
|
778
|
+
if (duration.value._tag === "Infinity") {
|
|
779
|
+
return {
|
|
780
|
+
days: Infinity,
|
|
781
|
+
hours: Infinity,
|
|
782
|
+
minutes: Infinity,
|
|
783
|
+
seconds: Infinity,
|
|
784
|
+
millis: Infinity,
|
|
785
|
+
nanos: Infinity
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
const nanos = unsafeToNanos(duration)
|
|
790
|
+
const ms = nanos / bigint1e6
|
|
791
|
+
const sec = ms / bigint1e3
|
|
792
|
+
const min = sec / bigint60
|
|
793
|
+
const hr = min / bigint60
|
|
794
|
+
const days = hr / bigint24
|
|
795
|
+
|
|
796
|
+
return {
|
|
797
|
+
days: Number(days),
|
|
798
|
+
hours: Number(hr % bigint24),
|
|
799
|
+
minutes: Number(min % bigint60),
|
|
800
|
+
seconds: Number(sec % bigint60),
|
|
801
|
+
millis: Number(ms % bigint1e3),
|
|
802
|
+
nanos: Number(nanos % bigint1e6)
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* Converts a `Duration` to a human readable string.
|
|
808
|
+
*
|
|
809
|
+
* @since 2.0.0
|
|
810
|
+
* @category conversions
|
|
811
|
+
* @example
|
|
812
|
+
* ```ts
|
|
813
|
+
* import { Duration } from "effect"
|
|
814
|
+
*
|
|
815
|
+
* Duration.format(Duration.millis(1000)) // "1s"
|
|
816
|
+
* Duration.format(Duration.millis(1001)) // "1s 1ms"
|
|
817
|
+
* ```
|
|
818
|
+
*/
|
|
819
|
+
export const format = (self: DurationInput): string => {
|
|
820
|
+
const duration = decode(self)
|
|
821
|
+
if (duration.value._tag === "Infinity") {
|
|
822
|
+
return "Infinity"
|
|
823
|
+
}
|
|
824
|
+
if (isZero(duration)) {
|
|
825
|
+
return "0"
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
const fragments = parts(duration)
|
|
829
|
+
const pieces = []
|
|
830
|
+
if (fragments.days !== 0) {
|
|
831
|
+
pieces.push(`${fragments.days}d`)
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
if (fragments.hours !== 0) {
|
|
835
|
+
pieces.push(`${fragments.hours}h`)
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
if (fragments.minutes !== 0) {
|
|
839
|
+
pieces.push(`${fragments.minutes}m`)
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
if (fragments.seconds !== 0) {
|
|
843
|
+
pieces.push(`${fragments.seconds}s`)
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
if (fragments.millis !== 0) {
|
|
847
|
+
pieces.push(`${fragments.millis}ms`)
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
if (fragments.nanos !== 0) {
|
|
851
|
+
pieces.push(`${fragments.nanos}ns`)
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
return pieces.join(" ")
|
|
855
|
+
}
|
|
856
|
+
|
|
857
|
+
/**
|
|
858
|
+
* Formats a Duration into an ISO8601 duration string.
|
|
859
|
+
*
|
|
860
|
+
* Months are assumed to be 30 days and years are assumed to be 365 days.
|
|
861
|
+
*
|
|
862
|
+
* Milliseconds and nanoseconds are expressed as fractional seconds.
|
|
863
|
+
*
|
|
864
|
+
* @example
|
|
865
|
+
* ```ts
|
|
866
|
+
* import { Duration } from "effect"
|
|
867
|
+
*
|
|
868
|
+
* Duration.unsafeFormatIso(Duration.days(1)) // => "P1D"
|
|
869
|
+
* Duration.unsafeFormatIso(Duration.minutes(90)) // => "PT1H30M"
|
|
870
|
+
* Duration.unsafeFormatIso(Duration.millis(1500)) // => "PT1.5S"
|
|
871
|
+
* ```
|
|
872
|
+
*
|
|
873
|
+
* @throws `RangeError` If the duration is not finite.
|
|
874
|
+
*
|
|
875
|
+
* @since 3.13.0
|
|
876
|
+
* @category conversions
|
|
877
|
+
*/
|
|
878
|
+
export const unsafeFormatIso = (self: DurationInput): string => {
|
|
879
|
+
const duration = decode(self)
|
|
880
|
+
if (!isFinite(duration)) {
|
|
881
|
+
throw new RangeError("Cannot format infinite duration")
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
const fragments = []
|
|
885
|
+
const {
|
|
886
|
+
days,
|
|
887
|
+
hours,
|
|
888
|
+
millis,
|
|
889
|
+
minutes,
|
|
890
|
+
nanos,
|
|
891
|
+
seconds
|
|
892
|
+
} = parts(duration)
|
|
893
|
+
|
|
894
|
+
let rest = days
|
|
895
|
+
if (rest >= 365) {
|
|
896
|
+
const years = Math.floor(rest / 365)
|
|
897
|
+
rest %= 365
|
|
898
|
+
fragments.push(`${years}Y`)
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
if (rest >= 30) {
|
|
902
|
+
const months = Math.floor(rest / 30)
|
|
903
|
+
rest %= 30
|
|
904
|
+
fragments.push(`${months}M`)
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
if (rest >= 7) {
|
|
908
|
+
const weeks = Math.floor(rest / 7)
|
|
909
|
+
rest %= 7
|
|
910
|
+
fragments.push(`${weeks}W`)
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
if (rest > 0) {
|
|
914
|
+
fragments.push(`${rest}D`)
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
if (hours !== 0 || minutes !== 0 || seconds !== 0 || millis !== 0 || nanos !== 0) {
|
|
918
|
+
fragments.push("T")
|
|
919
|
+
|
|
920
|
+
if (hours !== 0) {
|
|
921
|
+
fragments.push(`${hours}H`)
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
if (minutes !== 0) {
|
|
925
|
+
fragments.push(`${minutes}M`)
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
if (seconds !== 0 || millis !== 0 || nanos !== 0) {
|
|
929
|
+
const total = BigInt(seconds) * bigint1e9 + BigInt(millis) * bigint1e6 + BigInt(nanos)
|
|
930
|
+
const str = (Number(total) / 1e9).toFixed(9).replace(/\.?0+$/, "")
|
|
931
|
+
fragments.push(`${str}S`)
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
return `P${fragments.join("") || "T0S"}`
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
/**
|
|
939
|
+
* Formats a Duration into an ISO8601 duration string.
|
|
940
|
+
*
|
|
941
|
+
* Months are assumed to be 30 days and years are assumed to be 365 days.
|
|
942
|
+
*
|
|
943
|
+
* Returns `Option.none()` if the duration is infinite.
|
|
944
|
+
*
|
|
945
|
+
* @example
|
|
946
|
+
* ```ts
|
|
947
|
+
* import { Duration, Option } from "effect"
|
|
948
|
+
*
|
|
949
|
+
* Duration.formatIso(Duration.days(1)) // => Option.some("P1D")
|
|
950
|
+
* Duration.formatIso(Duration.minutes(90)) // => Option.some("PT1H30M")
|
|
951
|
+
* Duration.formatIso(Duration.millis(1500)) // => Option.some("PT1.5S")
|
|
952
|
+
* Duration.formatIso(Duration.infinity) // => Option.none()
|
|
953
|
+
* ```
|
|
954
|
+
*
|
|
955
|
+
* @since 3.13.0
|
|
956
|
+
* @category conversions
|
|
957
|
+
*/
|
|
958
|
+
export const formatIso = (self: DurationInput): Option.Option<string> => {
|
|
959
|
+
const duration = decode(self)
|
|
960
|
+
return isFinite(duration) ? Option.some(unsafeFormatIso(duration)) : Option.none()
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
/**
|
|
964
|
+
* Parses an ISO8601 duration string into a `Duration`.
|
|
965
|
+
*
|
|
966
|
+
* Months are assumed to be 30 days and years are assumed to be 365 days.
|
|
967
|
+
*
|
|
968
|
+
* @example
|
|
969
|
+
* ```ts
|
|
970
|
+
* import { Duration, Option } from "effect"
|
|
971
|
+
*
|
|
972
|
+
* Duration.fromIso("P1D") // => Option.some(Duration.days(1))
|
|
973
|
+
* Duration.fromIso("PT1H") // => Option.some(Duration.hours(1))
|
|
974
|
+
* Duration.fromIso("PT1M") // => Option.some(Duration.minutes(1))
|
|
975
|
+
* Duration.fromIso("PT1.5S") // => Option.some(Duration.seconds(1.5))
|
|
976
|
+
* ```
|
|
977
|
+
*
|
|
978
|
+
* @since 3.13.0
|
|
979
|
+
* @category conversions
|
|
980
|
+
*/
|
|
981
|
+
export const fromIso = (iso: string): Option.Option<Duration> => {
|
|
982
|
+
const result = DURATION_ISO_REGEX.exec(iso)
|
|
983
|
+
if (result == null) {
|
|
984
|
+
return Option.none()
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
const [years, months, weeks, days, hours, mins, secs] = result.slice(1, 8).map((_) => _ ? Number(_) : 0)
|
|
988
|
+
const value = years * 365 * 24 * 60 * 60 +
|
|
989
|
+
months * 30 * 24 * 60 * 60 +
|
|
990
|
+
weeks * 7 * 24 * 60 * 60 +
|
|
991
|
+
days * 24 * 60 * 60 +
|
|
992
|
+
hours * 60 * 60 +
|
|
993
|
+
mins * 60 +
|
|
994
|
+
secs
|
|
995
|
+
|
|
996
|
+
return Option.some(seconds(value))
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
const DURATION_ISO_REGEX =
|
|
1000
|
+
/^P(?!$)(?:(\d+)Y)?(?:(\d+)M)?(?:(\d+)W)?(?:(\d+)D)?(?:T(?!$)(?:(\d+)H)?(?:(\d+)M)?(?:(\d+(?:\.\d+)?)S)?)?$/
|