@clayroach/effect 3.19.14-source-capture.7 → 3.19.14-source-capture.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Utils.js +1 -1
- package/dist/cjs/Utils.js.map +1 -1
- package/dist/cjs/internal/clock.js +1 -1
- package/dist/cjs/internal/clock.js.map +1 -1
- package/dist/esm/Utils.js +1 -1
- package/dist/esm/Utils.js.map +1 -1
- package/dist/esm/internal/clock.js +1 -1
- package/dist/esm/internal/clock.js.map +1 -1
- package/package.json +1 -1
- package/src/Arbitrary.ts +0 -1101
- package/src/Array.ts +0 -3589
- package/src/BigDecimal.ts +0 -1349
- package/src/BigInt.ts +0 -643
- package/src/Boolean.ts +0 -287
- package/src/Brand.ts +0 -360
- package/src/Cache.ts +0 -281
- package/src/Cause.ts +0 -1555
- package/src/Channel.ts +0 -2355
- package/src/ChildExecutorDecision.ts +0 -146
- package/src/Chunk.ts +0 -1495
- package/src/Clock.ts +0 -111
- package/src/Config.ts +0 -542
- package/src/ConfigError.ts +0 -270
- package/src/ConfigProvider.ts +0 -333
- package/src/ConfigProviderPathPatch.ts +0 -100
- package/src/Console.ts +0 -226
- package/src/Context.ts +0 -585
- package/src/Cron.ts +0 -706
- package/src/Data.ts +0 -596
- package/src/DateTime.ts +0 -1686
- package/src/DefaultServices.ts +0 -34
- package/src/Deferred.ts +0 -301
- package/src/Differ.ts +0 -450
- package/src/Duration.ts +0 -1000
- package/src/Effect.ts +0 -14839
- package/src/Effectable.ts +0 -107
- package/src/Either.ts +0 -1040
- package/src/Encoding.ts +0 -195
- package/src/Equal.ts +0 -98
- package/src/Equivalence.ts +0 -235
- package/src/ExecutionPlan.ts +0 -308
- package/src/ExecutionStrategy.ts +0 -119
- package/src/Exit.ts +0 -467
- package/src/FastCheck.ts +0 -9
- package/src/Fiber.ts +0 -744
- package/src/FiberHandle.ts +0 -540
- package/src/FiberId.ts +0 -195
- package/src/FiberMap.ts +0 -656
- package/src/FiberRef.ts +0 -431
- package/src/FiberRefs.ts +0 -204
- package/src/FiberRefsPatch.ts +0 -105
- package/src/FiberSet.ts +0 -491
- package/src/FiberStatus.ts +0 -108
- package/src/Function.ts +0 -1222
- package/src/GlobalValue.ts +0 -53
- package/src/Graph.ts +0 -3732
- package/src/GroupBy.ts +0 -103
- package/src/HKT.ts +0 -45
- package/src/Hash.ts +0 -195
- package/src/HashMap.ts +0 -519
- package/src/HashRing.ts +0 -317
- package/src/HashSet.ts +0 -2346
- package/src/Inspectable.ts +0 -287
- package/src/Iterable.ts +0 -1119
- package/src/JSONSchema.ts +0 -1044
- package/src/KeyedPool.ts +0 -167
- package/src/Layer.ts +0 -1251
- package/src/LayerMap.ts +0 -436
- package/src/List.ts +0 -977
- package/src/LogLevel.ts +0 -285
- package/src/LogSpan.ts +0 -25
- package/src/Logger.ts +0 -702
- package/src/Mailbox.ts +0 -268
- package/src/ManagedRuntime.ts +0 -180
- package/src/Match.ts +0 -1477
- package/src/MergeDecision.ts +0 -95
- package/src/MergeState.ts +0 -172
- package/src/MergeStrategy.ts +0 -107
- package/src/Metric.ts +0 -780
- package/src/MetricBoundaries.ts +0 -69
- package/src/MetricHook.ts +0 -151
- package/src/MetricKey.ts +0 -224
- package/src/MetricKeyType.ts +0 -262
- package/src/MetricLabel.ts +0 -47
- package/src/MetricPair.ts +0 -71
- package/src/MetricPolling.ts +0 -148
- package/src/MetricRegistry.ts +0 -48
- package/src/MetricState.ts +0 -257
- package/src/Micro.ts +0 -4405
- package/src/ModuleVersion.ts +0 -18
- package/src/MutableHashMap.ts +0 -411
- package/src/MutableHashSet.ts +0 -706
- package/src/MutableList.ts +0 -297
- package/src/MutableQueue.ts +0 -227
- package/src/MutableRef.ts +0 -202
- package/src/NonEmptyIterable.ts +0 -32
- package/src/Number.ts +0 -1071
- package/src/Option.ts +0 -2170
- package/src/Order.ts +0 -373
- package/src/Ordering.ts +0 -111
- package/src/ParseResult.ts +0 -2031
- package/src/PartitionedSemaphore.ts +0 -200
- package/src/Pipeable.ts +0 -566
- package/src/Pool.ts +0 -204
- package/src/Predicate.ts +0 -1405
- package/src/Pretty.ts +0 -205
- package/src/PrimaryKey.ts +0 -23
- package/src/PubSub.ts +0 -182
- package/src/Queue.ts +0 -644
- package/src/Random.ts +0 -204
- package/src/RateLimiter.ts +0 -138
- package/src/RcMap.ts +0 -141
- package/src/RcRef.ts +0 -122
- package/src/Readable.ts +0 -93
- package/src/Record.ts +0 -1274
- package/src/RedBlackTree.ts +0 -421
- package/src/Redacted.ts +0 -144
- package/src/Ref.ts +0 -180
- package/src/RegExp.ts +0 -38
- package/src/Reloadable.ts +0 -127
- package/src/Request.ts +0 -347
- package/src/RequestBlock.ts +0 -118
- package/src/RequestResolver.ts +0 -366
- package/src/Resource.ts +0 -119
- package/src/Runtime.ts +0 -383
- package/src/RuntimeFlags.ts +0 -368
- package/src/RuntimeFlagsPatch.ts +0 -183
- package/src/STM.ts +0 -2045
- package/src/Schedule.ts +0 -2219
- package/src/ScheduleDecision.ts +0 -62
- package/src/ScheduleInterval.ts +0 -151
- package/src/ScheduleIntervals.ts +0 -122
- package/src/Scheduler.ts +0 -353
- package/src/Schema.ts +0 -10914
- package/src/SchemaAST.ts +0 -3043
- package/src/Scope.ts +0 -204
- package/src/ScopedCache.ts +0 -151
- package/src/ScopedRef.ts +0 -117
- package/src/Secret.ts +0 -88
- package/src/SingleProducerAsyncInput.ts +0 -67
- package/src/Sink.ts +0 -1461
- package/src/SortedMap.ts +0 -287
- package/src/SortedSet.ts +0 -390
- package/src/Stream.ts +0 -6468
- package/src/StreamEmit.ts +0 -136
- package/src/StreamHaltStrategy.ts +0 -123
- package/src/Streamable.ts +0 -45
- package/src/String.ts +0 -778
- package/src/Struct.ts +0 -243
- package/src/Subscribable.ts +0 -100
- package/src/SubscriptionRef.ts +0 -298
- package/src/Supervisor.ts +0 -240
- package/src/Symbol.ts +0 -29
- package/src/SynchronizedRef.ts +0 -270
- package/src/TArray.ts +0 -495
- package/src/TDeferred.ts +0 -100
- package/src/TMap.ts +0 -515
- package/src/TPriorityQueue.ts +0 -223
- package/src/TPubSub.ts +0 -200
- package/src/TQueue.ts +0 -432
- package/src/TRandom.ts +0 -129
- package/src/TReentrantLock.ts +0 -224
- package/src/TRef.ts +0 -178
- package/src/TSemaphore.ts +0 -129
- package/src/TSet.ts +0 -365
- package/src/TSubscriptionRef.ts +0 -192
- package/src/Take.ts +0 -258
- package/src/TestAnnotation.ts +0 -158
- package/src/TestAnnotationMap.ts +0 -119
- package/src/TestAnnotations.ts +0 -117
- package/src/TestClock.ts +0 -556
- package/src/TestConfig.ts +0 -47
- package/src/TestContext.ts +0 -36
- package/src/TestLive.ts +0 -53
- package/src/TestServices.ts +0 -390
- package/src/TestSized.ts +0 -55
- package/src/Tracer.ts +0 -198
- package/src/Trie.ts +0 -840
- package/src/Tuple.ts +0 -305
- package/src/Types.ts +0 -353
- package/src/Unify.ts +0 -113
- package/src/UpstreamPullRequest.ts +0 -117
- package/src/UpstreamPullStrategy.ts +0 -121
- package/src/Utils.ts +0 -809
- package/src/index.ts +0 -1561
- package/src/internal/array.ts +0 -8
- package/src/internal/blockedRequests.ts +0 -520
- package/src/internal/cache.ts +0 -733
- package/src/internal/cause.ts +0 -1050
- package/src/internal/channel/channelExecutor.ts +0 -1200
- package/src/internal/channel/channelState.ts +0 -134
- package/src/internal/channel/childExecutorDecision.ts +0 -96
- package/src/internal/channel/continuation.ts +0 -200
- package/src/internal/channel/mergeDecision.ts +0 -113
- package/src/internal/channel/mergeState.ts +0 -120
- package/src/internal/channel/mergeStrategy.ts +0 -72
- package/src/internal/channel/singleProducerAsyncInput.ts +0 -259
- package/src/internal/channel/subexecutor.ts +0 -229
- package/src/internal/channel/upstreamPullRequest.ts +0 -84
- package/src/internal/channel/upstreamPullStrategy.ts +0 -87
- package/src/internal/channel.ts +0 -2603
- package/src/internal/clock.ts +0 -95
- package/src/internal/completedRequestMap.ts +0 -9
- package/src/internal/concurrency.ts +0 -54
- package/src/internal/config.ts +0 -716
- package/src/internal/configError.ts +0 -304
- package/src/internal/configProvider/pathPatch.ts +0 -97
- package/src/internal/configProvider.ts +0 -799
- package/src/internal/console.ts +0 -153
- package/src/internal/context.ts +0 -337
- package/src/internal/core-effect.ts +0 -2293
- package/src/internal/core-stream.ts +0 -998
- package/src/internal/core.ts +0 -3273
- package/src/internal/data.ts +0 -36
- package/src/internal/dataSource.ts +0 -327
- package/src/internal/dateTime.ts +0 -1277
- package/src/internal/defaultServices/console.ts +0 -100
- package/src/internal/defaultServices.ts +0 -163
- package/src/internal/deferred.ts +0 -46
- package/src/internal/differ/chunkPatch.ts +0 -211
- package/src/internal/differ/contextPatch.ts +0 -232
- package/src/internal/differ/hashMapPatch.ts +0 -220
- package/src/internal/differ/hashSetPatch.ts +0 -176
- package/src/internal/differ/orPatch.ts +0 -311
- package/src/internal/differ/readonlyArrayPatch.ts +0 -210
- package/src/internal/differ.ts +0 -200
- package/src/internal/doNotation.ts +0 -80
- package/src/internal/effect/circular.ts +0 -905
- package/src/internal/effectable.ts +0 -131
- package/src/internal/either.ts +0 -110
- package/src/internal/encoding/base64.ts +0 -286
- package/src/internal/encoding/base64Url.ts +0 -29
- package/src/internal/encoding/common.ts +0 -51
- package/src/internal/encoding/hex.ts +0 -315
- package/src/internal/errors.ts +0 -7
- package/src/internal/executionPlan.ts +0 -114
- package/src/internal/executionStrategy.ts +0 -74
- package/src/internal/fiber.ts +0 -388
- package/src/internal/fiberId.ts +0 -267
- package/src/internal/fiberMessage.ts +0 -82
- package/src/internal/fiberRefs/patch.ts +0 -144
- package/src/internal/fiberRefs.ts +0 -297
- package/src/internal/fiberRuntime.ts +0 -3915
- package/src/internal/fiberScope.ts +0 -71
- package/src/internal/fiberStatus.ts +0 -119
- package/src/internal/groupBy.ts +0 -530
- package/src/internal/hashMap/array.ts +0 -49
- package/src/internal/hashMap/bitwise.ts +0 -32
- package/src/internal/hashMap/config.ts +0 -14
- package/src/internal/hashMap/keySet.ts +0 -8
- package/src/internal/hashMap/node.ts +0 -391
- package/src/internal/hashMap.ts +0 -586
- package/src/internal/hashSet.ts +0 -323
- package/src/internal/keyedPool.ts +0 -244
- package/src/internal/layer/circular.ts +0 -228
- package/src/internal/layer.ts +0 -1487
- package/src/internal/logSpan.ts +0 -20
- package/src/internal/logger-circular.ts +0 -24
- package/src/internal/logger.ts +0 -485
- package/src/internal/mailbox.ts +0 -561
- package/src/internal/managedRuntime/circular.ts +0 -6
- package/src/internal/managedRuntime.ts +0 -134
- package/src/internal/matcher.ts +0 -652
- package/src/internal/metric/boundaries.ts +0 -75
- package/src/internal/metric/hook.ts +0 -483
- package/src/internal/metric/key.ts +0 -167
- package/src/internal/metric/keyType.ts +0 -238
- package/src/internal/metric/label.ts +0 -41
- package/src/internal/metric/pair.ts +0 -48
- package/src/internal/metric/polling.ts +0 -149
- package/src/internal/metric/registry.ts +0 -187
- package/src/internal/metric/state.ts +0 -290
- package/src/internal/metric.ts +0 -577
- package/src/internal/opCodes/cause.ts +0 -35
- package/src/internal/opCodes/channel.ts +0 -83
- package/src/internal/opCodes/channelChildExecutorDecision.ts +0 -17
- package/src/internal/opCodes/channelMergeDecision.ts +0 -11
- package/src/internal/opCodes/channelMergeState.ts +0 -17
- package/src/internal/opCodes/channelMergeStrategy.ts +0 -11
- package/src/internal/opCodes/channelState.ts +0 -23
- package/src/internal/opCodes/channelUpstreamPullRequest.ts +0 -11
- package/src/internal/opCodes/channelUpstreamPullStrategy.ts +0 -11
- package/src/internal/opCodes/config.ts +0 -65
- package/src/internal/opCodes/configError.ts +0 -35
- package/src/internal/opCodes/continuation.ts +0 -11
- package/src/internal/opCodes/deferred.ts +0 -11
- package/src/internal/opCodes/effect.ts +0 -89
- package/src/internal/opCodes/layer.ts +0 -59
- package/src/internal/opCodes/streamHaltStrategy.ts +0 -23
- package/src/internal/option.ts +0 -80
- package/src/internal/pool.ts +0 -432
- package/src/internal/pubsub.ts +0 -1762
- package/src/internal/query.ts +0 -204
- package/src/internal/queue.ts +0 -766
- package/src/internal/random.ts +0 -161
- package/src/internal/rateLimiter.ts +0 -93
- package/src/internal/rcMap.ts +0 -285
- package/src/internal/rcRef.ts +0 -192
- package/src/internal/redBlackTree/iterator.ts +0 -200
- package/src/internal/redBlackTree/node.ts +0 -68
- package/src/internal/redBlackTree.ts +0 -1245
- package/src/internal/redacted.ts +0 -73
- package/src/internal/ref.ts +0 -171
- package/src/internal/reloadable.ts +0 -140
- package/src/internal/request.ts +0 -177
- package/src/internal/resource.ts +0 -76
- package/src/internal/ringBuffer.ts +0 -68
- package/src/internal/runtime.ts +0 -558
- package/src/internal/runtimeFlags.ts +0 -188
- package/src/internal/runtimeFlagsPatch.ts +0 -103
- package/src/internal/schedule/decision.ts +0 -47
- package/src/internal/schedule/interval.ts +0 -101
- package/src/internal/schedule/intervals.ts +0 -180
- package/src/internal/schedule.ts +0 -2199
- package/src/internal/schema/errors.ts +0 -191
- package/src/internal/schema/schemaId.ts +0 -106
- package/src/internal/schema/util.ts +0 -50
- package/src/internal/scopedCache.ts +0 -644
- package/src/internal/scopedRef.ts +0 -118
- package/src/internal/secret.ts +0 -89
- package/src/internal/singleShotGen.ts +0 -35
- package/src/internal/sink.ts +0 -2120
- package/src/internal/stack.ts +0 -10
- package/src/internal/stm/core.ts +0 -817
- package/src/internal/stm/entry.ts +0 -59
- package/src/internal/stm/journal.ts +0 -123
- package/src/internal/stm/opCodes/stm.ts +0 -71
- package/src/internal/stm/opCodes/stmState.ts +0 -17
- package/src/internal/stm/opCodes/strategy.ts +0 -17
- package/src/internal/stm/opCodes/tExit.ts +0 -29
- package/src/internal/stm/opCodes/tryCommit.ts +0 -11
- package/src/internal/stm/stm.ts +0 -1453
- package/src/internal/stm/stmState.ts +0 -136
- package/src/internal/stm/tArray.ts +0 -550
- package/src/internal/stm/tDeferred.ts +0 -81
- package/src/internal/stm/tExit.ts +0 -190
- package/src/internal/stm/tMap.ts +0 -824
- package/src/internal/stm/tPriorityQueue.ts +0 -267
- package/src/internal/stm/tPubSub.ts +0 -551
- package/src/internal/stm/tQueue.ts +0 -393
- package/src/internal/stm/tRandom.ts +0 -140
- package/src/internal/stm/tReentrantLock.ts +0 -352
- package/src/internal/stm/tRef.ts +0 -195
- package/src/internal/stm/tSemaphore.ts +0 -113
- package/src/internal/stm/tSet.ts +0 -259
- package/src/internal/stm/tSubscriptionRef.ts +0 -286
- package/src/internal/stm/tryCommit.ts +0 -34
- package/src/internal/stm/txnId.ts +0 -14
- package/src/internal/stm/versioned.ts +0 -4
- package/src/internal/stream/debounceState.ts +0 -57
- package/src/internal/stream/emit.ts +0 -123
- package/src/internal/stream/haltStrategy.ts +0 -94
- package/src/internal/stream/handoff.ts +0 -187
- package/src/internal/stream/handoffSignal.ts +0 -59
- package/src/internal/stream/pull.ts +0 -34
- package/src/internal/stream/sinkEndReason.ts +0 -30
- package/src/internal/stream/zipAllState.ts +0 -88
- package/src/internal/stream/zipChunksState.ts +0 -56
- package/src/internal/stream.ts +0 -8801
- package/src/internal/string-utils.ts +0 -107
- package/src/internal/subscriptionRef.ts +0 -138
- package/src/internal/supervisor/patch.ts +0 -190
- package/src/internal/supervisor.ts +0 -303
- package/src/internal/synchronizedRef.ts +0 -114
- package/src/internal/take.ts +0 -199
- package/src/internal/testing/sleep.ts +0 -27
- package/src/internal/testing/suspendedWarningData.ts +0 -85
- package/src/internal/testing/warningData.ts +0 -94
- package/src/internal/tracer.ts +0 -293
- package/src/internal/trie.ts +0 -722
- package/src/internal/version.ts +0 -7
package/src/Duration.ts
DELETED
|
@@ -1,1000 +0,0 @@
|
|
|
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)?)?$/
|