@clayroach/effect 3.19.14-source-capture.6 → 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/Number.ts
DELETED
|
@@ -1,1071 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* # Number
|
|
3
|
-
*
|
|
4
|
-
* This module provides utility functions and type class instances for working
|
|
5
|
-
* with the `number` type in TypeScript. It includes functions for basic
|
|
6
|
-
* arithmetic operations, as well as type class instances for `Equivalence` and
|
|
7
|
-
* `Order`.
|
|
8
|
-
*
|
|
9
|
-
* ## Operations Reference
|
|
10
|
-
*
|
|
11
|
-
* | Category | Operation | Description | Domain | Co-domain |
|
|
12
|
-
* | ------------ | ------------------------------------------ | ------------------------------------------------------- | ------------------------------ | --------------------- |
|
|
13
|
-
* | constructors | {@link module:Number.parse} | Safely parses a string to a number | `string` | `Option<number>` |
|
|
14
|
-
* | | | | | |
|
|
15
|
-
* | math | {@link module:Number.sum} | Adds two numbers | `number`, `number` | `number` |
|
|
16
|
-
* | math | {@link module:Number.sumAll} | Sums all numbers in a collection | `Iterable<number>` | `number` |
|
|
17
|
-
* | math | {@link module:Number.subtract} | Subtracts one number from another | `number`, `number` | `number` |
|
|
18
|
-
* | math | {@link module:Number.multiply} | Multiplies two numbers | `number`, `number` | `number` |
|
|
19
|
-
* | math | {@link module:Number.multiplyAll} | Multiplies all numbers in a collection | `Iterable<number>` | `number` |
|
|
20
|
-
* | math | {@link module:Number.divide} | Safely divides handling division by zero | `number`, `number` | `Option<number>` |
|
|
21
|
-
* | math | {@link module:Number.unsafeDivide} | Divides but misbehaves for division by zero | `number`, `number` | `number` |
|
|
22
|
-
* | math | {@link module:Number.remainder} | Calculates remainder of division | `number`, `number` | `number` |
|
|
23
|
-
* | math | {@link module:Number.increment} | Adds 1 to a number | `number` | `number` |
|
|
24
|
-
* | math | {@link module:Number.decrement} | Subtracts 1 from a number | `number` | `number` |
|
|
25
|
-
* | math | {@link module:Number.sign} | Determines the sign of a number | `number` | `Ordering` |
|
|
26
|
-
* | math | {@link module:Number.nextPow2} | Finds the next power of 2 | `number` | `number` |
|
|
27
|
-
* | math | {@link module:Number.round} | Rounds a number with specified precision | `number`, `number` | `number` |
|
|
28
|
-
* | | | | | |
|
|
29
|
-
* | predicates | {@link module:Number.between} | Checks if a number is in a range | `number`, `{minimum, maximum}` | `boolean` |
|
|
30
|
-
* | predicates | {@link module:Number.lessThan} | Checks if one number is less than another | `number`, `number` | `boolean` |
|
|
31
|
-
* | predicates | {@link module:Number.lessThanOrEqualTo} | Checks if one number is less than or equal | `number`, `number` | `boolean` |
|
|
32
|
-
* | predicates | {@link module:Number.greaterThan} | Checks if one number is greater than another | `number`, `number` | `boolean` |
|
|
33
|
-
* | predicates | {@link module:Number.greaterThanOrEqualTo} | Checks if one number is greater or equal | `number`, `number` | `boolean` |
|
|
34
|
-
* | | | | | |
|
|
35
|
-
* | guards | {@link module:Number.isNumber} | Type guard for JavaScript numbers | `unknown` | `boolean` |
|
|
36
|
-
* | | | | | |
|
|
37
|
-
* | comparison | {@link module:Number.min} | Returns the minimum of two numbers | `number`, `number` | `number` |
|
|
38
|
-
* | comparison | {@link module:Number.max} | Returns the maximum of two numbers | `number`, `number` | `number` |
|
|
39
|
-
* | comparison | {@link module:Number.clamp} | Restricts a number to a range | `number`, `{minimum, maximum}` | `number` |
|
|
40
|
-
* | | | | | |
|
|
41
|
-
* | instances | {@link module:Number.Equivalence} | Equivalence instance for numbers | | `Equivalence<number>` |
|
|
42
|
-
* | instances | {@link module:Number.Order} | Order instance for numbers | | `Order<number>` |
|
|
43
|
-
* | | | | | |
|
|
44
|
-
* | errors | {@link module:Number.DivisionByZeroError} | Error thrown by unsafeDivide | | |
|
|
45
|
-
*
|
|
46
|
-
* ## Composition Patterns and Type Safety
|
|
47
|
-
*
|
|
48
|
-
* When building function pipelines, understanding how types flow through
|
|
49
|
-
* operations is critical:
|
|
50
|
-
*
|
|
51
|
-
* ### Composing with type-preserving operations
|
|
52
|
-
*
|
|
53
|
-
* Most operations in this module are type-preserving (`number → number`),
|
|
54
|
-
* making them easily composable in pipelines:
|
|
55
|
-
*
|
|
56
|
-
* ```ts
|
|
57
|
-
* import { pipe } from "effect"
|
|
58
|
-
* import * as Number from "effect/Number"
|
|
59
|
-
*
|
|
60
|
-
* const result = pipe(
|
|
61
|
-
* 10,
|
|
62
|
-
* Number.increment, // number → number
|
|
63
|
-
* Number.multiply(2), // number → number
|
|
64
|
-
* Number.round(1) // number → number
|
|
65
|
-
* ) // Result: number (21)
|
|
66
|
-
* ```
|
|
67
|
-
*
|
|
68
|
-
* ### Working with Option results
|
|
69
|
-
*
|
|
70
|
-
* Operations that might fail (like division by zero) return Option types and
|
|
71
|
-
* require Option combinators:
|
|
72
|
-
*
|
|
73
|
-
* ```ts
|
|
74
|
-
* import { pipe, Option } from "effect"
|
|
75
|
-
* import * as Number from "effect/Number"
|
|
76
|
-
*
|
|
77
|
-
* const result = pipe(
|
|
78
|
-
* 10,
|
|
79
|
-
* Number.divide(0), // number → Option<number>
|
|
80
|
-
* Option.getOrElse(() => 0) // Option<number> → number
|
|
81
|
-
* ) // Result: number (0)
|
|
82
|
-
* ```
|
|
83
|
-
*
|
|
84
|
-
* ### Composition best practices
|
|
85
|
-
*
|
|
86
|
-
* - Chain type-preserving operations for maximum composability
|
|
87
|
-
* - Use Option combinators when working with potentially failing operations
|
|
88
|
-
* - Consider using Effect for operations that might fail with specific errors
|
|
89
|
-
* - Remember that all operations maintain JavaScript's floating-point precision
|
|
90
|
-
* limitations
|
|
91
|
-
*
|
|
92
|
-
* @module Number
|
|
93
|
-
* @since 2.0.0
|
|
94
|
-
* @see {@link module:BigInt} for more similar operations on `bigint` types
|
|
95
|
-
* @see {@link module:BigDecimal} for more similar operations on `BigDecimal` types
|
|
96
|
-
*/
|
|
97
|
-
|
|
98
|
-
import * as equivalence from "./Equivalence.js"
|
|
99
|
-
import { dual } from "./Function.js"
|
|
100
|
-
import * as option from "./internal/option.js"
|
|
101
|
-
import * as _Iterable from "./Iterable.js"
|
|
102
|
-
import type { Option } from "./Option.js"
|
|
103
|
-
import * as order from "./Order.js"
|
|
104
|
-
import type { Ordering } from "./Ordering.js"
|
|
105
|
-
import * as predicate from "./Predicate.js"
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Type guard that tests if a value is a member of the set of JavaScript
|
|
109
|
-
* numbers.
|
|
110
|
-
*
|
|
111
|
-
* @memberof Number
|
|
112
|
-
* @since 2.0.0
|
|
113
|
-
* @category guards
|
|
114
|
-
* @example
|
|
115
|
-
*
|
|
116
|
-
* ```ts
|
|
117
|
-
* import * as assert from "node:assert/strict"
|
|
118
|
-
* import * as Number from "effect/Number"
|
|
119
|
-
*
|
|
120
|
-
* // Regular numbers
|
|
121
|
-
* assert.equal(Number.isNumber(2), true)
|
|
122
|
-
* assert.equal(Number.isNumber(-3.14), true)
|
|
123
|
-
* assert.equal(Number.isNumber(0), true)
|
|
124
|
-
*
|
|
125
|
-
* // Special numeric values
|
|
126
|
-
* assert.equal(Number.isNumber(Infinity), true)
|
|
127
|
-
* assert.equal(Number.isNumber(NaN), true)
|
|
128
|
-
*
|
|
129
|
-
* // Non-number values
|
|
130
|
-
* assert.equal(Number.isNumber("2"), false)
|
|
131
|
-
* assert.equal(Number.isNumber(true), false)
|
|
132
|
-
* assert.equal(Number.isNumber(null), false)
|
|
133
|
-
* assert.equal(Number.isNumber(undefined), false)
|
|
134
|
-
* assert.equal(Number.isNumber({}), false)
|
|
135
|
-
* assert.equal(Number.isNumber([]), false)
|
|
136
|
-
*
|
|
137
|
-
* // Using as a type guard in conditionals
|
|
138
|
-
* function processValue(value: unknown): string {
|
|
139
|
-
* if (Number.isNumber(value)) {
|
|
140
|
-
* // TypeScript now knows 'value' is a number
|
|
141
|
-
* return `Numeric value: ${value.toFixed(2)}`
|
|
142
|
-
* }
|
|
143
|
-
* return "Not a number"
|
|
144
|
-
* }
|
|
145
|
-
*
|
|
146
|
-
* assert.strictEqual(processValue(42), "Numeric value: 42.00")
|
|
147
|
-
* assert.strictEqual(processValue("hello"), "Not a number")
|
|
148
|
-
*
|
|
149
|
-
* // Filtering for numbers in an array
|
|
150
|
-
* const mixed = [1, "two", 3, false, 5]
|
|
151
|
-
* const onlyNumbers = mixed.filter(Number.isNumber)
|
|
152
|
-
* assert.equal(onlyNumbers, [1, 3, 5])
|
|
153
|
-
* ```
|
|
154
|
-
*
|
|
155
|
-
* @param input - The value to test for membership in the set of JavaScript
|
|
156
|
-
* numbers
|
|
157
|
-
*
|
|
158
|
-
* @returns `true` if the input is a JavaScript number, `false` otherwise
|
|
159
|
-
*/
|
|
160
|
-
export const isNumber: (input: unknown) => input is number = predicate.isNumber
|
|
161
|
-
|
|
162
|
-
/**
|
|
163
|
-
* Returns the additive inverse of a number, effectively negating it.
|
|
164
|
-
*
|
|
165
|
-
* @memberof Number
|
|
166
|
-
* @since 3.14.6
|
|
167
|
-
* @example
|
|
168
|
-
*
|
|
169
|
-
* ```ts
|
|
170
|
-
* import * as assert from "node:assert/strict"
|
|
171
|
-
* import { pipe } from "effect"
|
|
172
|
-
* import * as Number from "effect/Number"
|
|
173
|
-
*
|
|
174
|
-
* assert.equal(
|
|
175
|
-
* Number.negate(5), //
|
|
176
|
-
* -5
|
|
177
|
-
* )
|
|
178
|
-
*
|
|
179
|
-
* assert.equal(
|
|
180
|
-
* Number.negate(-5), //
|
|
181
|
-
* 5
|
|
182
|
-
* )
|
|
183
|
-
*
|
|
184
|
-
* assert.equal(
|
|
185
|
-
* Number.negate(0), //
|
|
186
|
-
* 0
|
|
187
|
-
* )
|
|
188
|
-
* ```
|
|
189
|
-
*
|
|
190
|
-
* @param n - The number value to be negated.
|
|
191
|
-
*
|
|
192
|
-
* @returns The negated number value.
|
|
193
|
-
*/
|
|
194
|
-
export const negate = (n: number): number => multiply(n, -1)
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Performs addition in the set of JavaScript numbers.
|
|
198
|
-
*
|
|
199
|
-
* @memberof Number
|
|
200
|
-
* @since 2.0.0
|
|
201
|
-
* @category math
|
|
202
|
-
* @example
|
|
203
|
-
*
|
|
204
|
-
* ```ts
|
|
205
|
-
* import * as assert from "node:assert/strict"
|
|
206
|
-
* import { pipe } from "effect"
|
|
207
|
-
* import * as Number from "effect/Number"
|
|
208
|
-
*
|
|
209
|
-
* // Data-first style (direct application)
|
|
210
|
-
* assert.equal(Number.sum(2, 3), 5)
|
|
211
|
-
* assert.equal(Number.sum(-10, 5), -5)
|
|
212
|
-
* assert.equal(Number.sum(0.1, 0.2), 0.30000000000000004) // Note: floating-point precision limitation
|
|
213
|
-
*
|
|
214
|
-
* // Data-last style (pipeable)
|
|
215
|
-
* assert.equal(
|
|
216
|
-
* pipe(
|
|
217
|
-
* 10,
|
|
218
|
-
* Number.sum(5) // 10 + 5 = 15
|
|
219
|
-
* ),
|
|
220
|
-
* 15
|
|
221
|
-
* )
|
|
222
|
-
*
|
|
223
|
-
* // Chaining multiple additions
|
|
224
|
-
* assert.equal(
|
|
225
|
-
* pipe(
|
|
226
|
-
* 1,
|
|
227
|
-
* Number.sum(2), // 1 + 2 = 3
|
|
228
|
-
* Number.sum(3), // 3 + 3 = 6
|
|
229
|
-
* Number.sum(4) // 6 + 4 = 10
|
|
230
|
-
* ),
|
|
231
|
-
* 10
|
|
232
|
-
* )
|
|
233
|
-
*
|
|
234
|
-
* // Identity property: a + 0 = a
|
|
235
|
-
* assert.equal(Number.sum(42, 0), 42)
|
|
236
|
-
*
|
|
237
|
-
* // Commutative property: a + b = b + a
|
|
238
|
-
* assert.equal(Number.sum(5, 3), Number.sum(3, 5))
|
|
239
|
-
* ```
|
|
240
|
-
*/
|
|
241
|
-
export const sum: {
|
|
242
|
-
/**
|
|
243
|
-
* Returns a function that adds a specified number to its argument.
|
|
244
|
-
*
|
|
245
|
-
* @param that - The number to add to the input of the resulting function
|
|
246
|
-
*
|
|
247
|
-
* @returns A function that takes a number and returns the sum of that number
|
|
248
|
-
* and `that`
|
|
249
|
-
*/
|
|
250
|
-
(that: number): (self: number) => number
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* Adds two numbers together.
|
|
254
|
-
*
|
|
255
|
-
* @param self - The first addend
|
|
256
|
-
* @param that - The second addend
|
|
257
|
-
*
|
|
258
|
-
* @returns The sum of the two numbers
|
|
259
|
-
*/
|
|
260
|
-
(self: number, that: number): number
|
|
261
|
-
} = dual(2, (self: number, that: number): number => self + that)
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* Computes the sum of all elements in an iterable collection of numbers.
|
|
265
|
-
*
|
|
266
|
-
* @memberof Number
|
|
267
|
-
* @since 2.0.0
|
|
268
|
-
* @category math
|
|
269
|
-
* @example
|
|
270
|
-
*
|
|
271
|
-
* ```ts
|
|
272
|
-
* import * as assert from "node:assert/strict"
|
|
273
|
-
* import * as Number from "effect/Number"
|
|
274
|
-
*
|
|
275
|
-
* // Basic sums
|
|
276
|
-
* assert.equal(Number.sumAll([2, 3, 4]), 9) // 2 + 3 + 4 = 9
|
|
277
|
-
* assert.equal(Number.sumAll([1.1, 2.2, 3.3]), 6.6) // 1.1 + 2.2 + 3.3 = 6.6
|
|
278
|
-
*
|
|
279
|
-
* // Empty collection returns the additive identity (0)
|
|
280
|
-
* assert.equal(Number.sumAll([]), 0)
|
|
281
|
-
*
|
|
282
|
-
* // Single element collection
|
|
283
|
-
* assert.equal(Number.sumAll([42]), 42)
|
|
284
|
-
*
|
|
285
|
-
* // Sums with negative numbers
|
|
286
|
-
* assert.equal(Number.sumAll([2, -3, 4]), 3) // 2 + (-3) + 4 = 3
|
|
287
|
-
* assert.equal(Number.sumAll([-2, -3, -4]), -9) // (-2) + (-3) + (-4) = -9
|
|
288
|
-
*
|
|
289
|
-
* // Works with any iterable
|
|
290
|
-
* assert.equal(Number.sumAll(new Set([2, 3, 4])), 9)
|
|
291
|
-
*
|
|
292
|
-
* // Using with generated sequences
|
|
293
|
-
* function* range(start: number, end: number) {
|
|
294
|
-
* for (let i = start; i <= end; i++) yield i
|
|
295
|
-
* }
|
|
296
|
-
*
|
|
297
|
-
* // Compute sum of first 5 natural numbers: 1 + 2 + 3 + 4 + 5 = 15
|
|
298
|
-
* assert.equal(Number.sumAll(range(1, 5)), 15)
|
|
299
|
-
*
|
|
300
|
-
* // Floating point precision example
|
|
301
|
-
* assert.equal(
|
|
302
|
-
* Number.sumAll([0.1, 0.2]),
|
|
303
|
-
* 0.30000000000000004 // Note IEEE 754 precision limitation
|
|
304
|
-
* )
|
|
305
|
-
* ```
|
|
306
|
-
*
|
|
307
|
-
* @param collection - An `iterable` containing the `numbers` to sum
|
|
308
|
-
*
|
|
309
|
-
* @returns The sum of all numbers in the collection, or 0 if the collection is
|
|
310
|
-
* empty
|
|
311
|
-
*/
|
|
312
|
-
export const sumAll = (collection: Iterable<number>): number => _Iterable.reduce(collection, 0, sum)
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* Performs subtraction in the set of JavaScript numbers.
|
|
316
|
-
*
|
|
317
|
-
* @memberof Number
|
|
318
|
-
* @since 2.0.0
|
|
319
|
-
* @category math
|
|
320
|
-
* @example
|
|
321
|
-
*
|
|
322
|
-
* ```ts
|
|
323
|
-
* import * as assert from "node:assert/strict"
|
|
324
|
-
* import { pipe } from "effect"
|
|
325
|
-
* import * as Number from "effect/Number"
|
|
326
|
-
*
|
|
327
|
-
* // Data-first style (direct application)
|
|
328
|
-
* assert.equal(Number.subtract(2, 3), -1) // 2 - 3 = -1
|
|
329
|
-
* assert.equal(Number.subtract(10, 5), 5) // 10 - 5 = 5
|
|
330
|
-
* assert.equal(Number.subtract(0.3, 0.1), 0.19999999999999998) // Note: floating-point precision limitation
|
|
331
|
-
*
|
|
332
|
-
* // Data-last style (pipeable)
|
|
333
|
-
* assert.equal(
|
|
334
|
-
* pipe(
|
|
335
|
-
* 10,
|
|
336
|
-
* Number.subtract(5) // 10 - 5 = 5
|
|
337
|
-
* ),
|
|
338
|
-
* 5
|
|
339
|
-
* )
|
|
340
|
-
*
|
|
341
|
-
* // Chaining multiple subtractions
|
|
342
|
-
* assert.equal(
|
|
343
|
-
* pipe(
|
|
344
|
-
* 20,
|
|
345
|
-
* Number.subtract(5), // 20 - 5 = 15
|
|
346
|
-
* Number.subtract(3), // 15 - 3 = 12
|
|
347
|
-
* Number.subtract(2) // 12 - 2 = 10
|
|
348
|
-
* ),
|
|
349
|
-
* 10
|
|
350
|
-
* )
|
|
351
|
-
*
|
|
352
|
-
* // Right identity property: a - 0 = a
|
|
353
|
-
* assert.equal(Number.subtract(42, 0), 42)
|
|
354
|
-
*
|
|
355
|
-
* // Self-annihilation property: a - a = 0
|
|
356
|
-
* assert.equal(Number.subtract(42, 42), 0)
|
|
357
|
-
*
|
|
358
|
-
* // Non-commutative property: a - b ≠ b - a
|
|
359
|
-
* assert.equal(Number.subtract(5, 3), 2) // 5 - 3 = 2
|
|
360
|
-
* assert.equal(Number.subtract(3, 5), -2) // 3 - 5 = -2
|
|
361
|
-
*
|
|
362
|
-
* // Inverse relation: a - b = -(b - a)
|
|
363
|
-
* assert.equal(Number.subtract(5, 3), -Number.subtract(3, 5))
|
|
364
|
-
* ```
|
|
365
|
-
*/
|
|
366
|
-
export const subtract: {
|
|
367
|
-
/**
|
|
368
|
-
* Returns a function that subtracts a specified number from its argument.
|
|
369
|
-
*
|
|
370
|
-
* @param subtrahend - The number to subtract from the input of the resulting
|
|
371
|
-
* function
|
|
372
|
-
*
|
|
373
|
-
* @returns A function that takes a minuend and returns the difference of
|
|
374
|
-
* subtracting the subtrahend from it
|
|
375
|
-
*/
|
|
376
|
-
(subtrahend: number): (minuend: number) => number
|
|
377
|
-
|
|
378
|
-
/**
|
|
379
|
-
* Subtracts the subtrahend from the minuend and returns the difference.
|
|
380
|
-
*
|
|
381
|
-
* @param minuend - The number from which another number is to be subtracted
|
|
382
|
-
* @param subtrahend - The number to subtract from the minuend
|
|
383
|
-
*
|
|
384
|
-
* @returns The difference of the minuend minus the subtrahend
|
|
385
|
-
*/
|
|
386
|
-
(minuend: number, subtrahend: number): number
|
|
387
|
-
} = dual(
|
|
388
|
-
2,
|
|
389
|
-
(minuend: number, subtrahend: number): number => minuend - subtrahend
|
|
390
|
-
)
|
|
391
|
-
|
|
392
|
-
/**
|
|
393
|
-
* Performs **multiplication** in the set of JavaScript numbers.
|
|
394
|
-
*
|
|
395
|
-
* @memberof Number
|
|
396
|
-
* @since 2.0.0
|
|
397
|
-
* @category math
|
|
398
|
-
* @example
|
|
399
|
-
*
|
|
400
|
-
* ```ts
|
|
401
|
-
* import * as assert from "node:assert/strict"
|
|
402
|
-
* import { pipe } from "effect"
|
|
403
|
-
* import * as Number from "effect/Number"
|
|
404
|
-
*
|
|
405
|
-
* // Data-first style (direct application)
|
|
406
|
-
* assert.equal(Number.multiply(2, 3), 6) // 2 × 3 = 6
|
|
407
|
-
* assert.equal(Number.multiply(-4, 5), -20) // (-4) × 5 = -20
|
|
408
|
-
* assert.equal(Number.multiply(-3, -2), 6) // (-3) × (-2) = 6
|
|
409
|
-
* assert.equal(Number.multiply(0.1, 0.2), 0.020000000000000004) // Note: floating-point precision limitation
|
|
410
|
-
*
|
|
411
|
-
* // Data-last style (pipeable)
|
|
412
|
-
* assert.equal(
|
|
413
|
-
* pipe(
|
|
414
|
-
* 10,
|
|
415
|
-
* Number.multiply(5) // 10 × 5 = 50
|
|
416
|
-
* ),
|
|
417
|
-
* 50
|
|
418
|
-
* )
|
|
419
|
-
*
|
|
420
|
-
* // Chaining multiple multiplications
|
|
421
|
-
* assert.equal(
|
|
422
|
-
* pipe(
|
|
423
|
-
* 2,
|
|
424
|
-
* Number.multiply(3), // 2 × 3 = 6
|
|
425
|
-
* Number.multiply(4), // 6 × 4 = 24
|
|
426
|
-
* Number.multiply(0.5) // 24 × 0.5 = 12
|
|
427
|
-
* ),
|
|
428
|
-
* 12
|
|
429
|
-
* )
|
|
430
|
-
*
|
|
431
|
-
* // Identity property: a × 1 = a
|
|
432
|
-
* assert.equal(Number.multiply(42, 1), 42)
|
|
433
|
-
*
|
|
434
|
-
* // Zero property: a × 0 = 0
|
|
435
|
-
* assert.equal(Number.multiply(42, 0), 0)
|
|
436
|
-
*
|
|
437
|
-
* // Commutative property: a × b = b × a
|
|
438
|
-
* assert.equal(Number.multiply(5, 3), Number.multiply(3, 5))
|
|
439
|
-
*
|
|
440
|
-
* // Associative property: (a × b) × c = a × (b × c)
|
|
441
|
-
* const a = 2,
|
|
442
|
-
* b = 3,
|
|
443
|
-
* c = 4
|
|
444
|
-
* assert.equal(
|
|
445
|
-
* Number.multiply(Number.multiply(a, b), c),
|
|
446
|
-
* Number.multiply(a, Number.multiply(b, c))
|
|
447
|
-
* )
|
|
448
|
-
* ```
|
|
449
|
-
*/
|
|
450
|
-
export const multiply: {
|
|
451
|
-
/**
|
|
452
|
-
* Returns a function that multiplies a specified number with its argument.
|
|
453
|
-
*
|
|
454
|
-
* @param multiplicand - The number to multiply with the input of the
|
|
455
|
-
* resulting function
|
|
456
|
-
*
|
|
457
|
-
* @returns A function that takes a multiplier and returns the product of that
|
|
458
|
-
* multiplier and the multiplicand
|
|
459
|
-
*/
|
|
460
|
-
(multiplicand: number): (multiplier: number) => number
|
|
461
|
-
|
|
462
|
-
/**
|
|
463
|
-
* Multiplies two numbers together.
|
|
464
|
-
*
|
|
465
|
-
* @param multiplier - The first factor
|
|
466
|
-
* @param multiplicand - The second factor
|
|
467
|
-
*
|
|
468
|
-
* @returns The product of the two numbers
|
|
469
|
-
*/
|
|
470
|
-
(multiplier: number, multiplicand: number): number
|
|
471
|
-
} = dual(
|
|
472
|
-
2,
|
|
473
|
-
(multiplier: number, multiplicand: number): number => multiplier * multiplicand
|
|
474
|
-
)
|
|
475
|
-
|
|
476
|
-
/**
|
|
477
|
-
* Computes the product of all elements in an iterable collection of numbers.
|
|
478
|
-
*
|
|
479
|
-
* @memberof Number
|
|
480
|
-
* @since 2.0.0
|
|
481
|
-
* @category math
|
|
482
|
-
* @example
|
|
483
|
-
*
|
|
484
|
-
* ```ts
|
|
485
|
-
* import * as assert from "node:assert/strict"
|
|
486
|
-
* import * as Number from "effect/Number"
|
|
487
|
-
*
|
|
488
|
-
* // Basic products
|
|
489
|
-
* assert.equal(Number.multiplyAll([2, 3, 4]), 24) // 2 × 3 × 4 = 24
|
|
490
|
-
* assert.equal(Number.multiplyAll([1.5, 2, 3]), 9) // 1.5 × 2 × 3 = 9
|
|
491
|
-
*
|
|
492
|
-
* // Empty collection returns the multiplicative identity (1)
|
|
493
|
-
* assert.equal(Number.multiplyAll([]), 1)
|
|
494
|
-
*
|
|
495
|
-
* // Single element collection
|
|
496
|
-
* assert.equal(Number.multiplyAll([42]), 42)
|
|
497
|
-
*
|
|
498
|
-
* // Products with negative numbers
|
|
499
|
-
* assert.equal(Number.multiplyAll([2, -3, 4]), -24) // 2 × (-3) × 4 = -24
|
|
500
|
-
* assert.equal(Number.multiplyAll([-2, -3]), 6) // (-2) × (-3) = 6
|
|
501
|
-
*
|
|
502
|
-
* // Zero property - if any element is zero, product is zero
|
|
503
|
-
* assert.equal(Number.multiplyAll([2, 0, 3]), 0)
|
|
504
|
-
*
|
|
505
|
-
* // Works with any iterable
|
|
506
|
-
* assert.equal(Number.multiplyAll(new Set([2, 3, 4])), 24)
|
|
507
|
-
*
|
|
508
|
-
* // Using with generated sequences
|
|
509
|
-
* function* range(start: number, end: number) {
|
|
510
|
-
* for (let i = start; i <= end; i++) yield i
|
|
511
|
-
* }
|
|
512
|
-
*
|
|
513
|
-
* // Compute factorial: 5! = 5 × 4 × 3 × 2 × 1 = 120
|
|
514
|
-
* assert.equal(Number.multiplyAll(range(1, 5)), 120)
|
|
515
|
-
* ```
|
|
516
|
-
*
|
|
517
|
-
* @param collection - An `iterable` containing the `numbers` to multiply
|
|
518
|
-
*
|
|
519
|
-
* @returns The product of all numbers in the collection, or 1 if the collection
|
|
520
|
-
* is empty
|
|
521
|
-
*/
|
|
522
|
-
export const multiplyAll = (collection: Iterable<number>): number => {
|
|
523
|
-
let out = 1
|
|
524
|
-
for (const n of collection) {
|
|
525
|
-
if (n === 0) {
|
|
526
|
-
return 0
|
|
527
|
-
}
|
|
528
|
-
out *= n
|
|
529
|
-
}
|
|
530
|
-
return out
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
/**
|
|
534
|
-
* Performs division in the set of JavaScript numbers, returning the result
|
|
535
|
-
* wrapped in an `Option` to handle division by zero.
|
|
536
|
-
*
|
|
537
|
-
* @memberof Number
|
|
538
|
-
* @since 2.0.0
|
|
539
|
-
* @category math
|
|
540
|
-
* @example
|
|
541
|
-
*
|
|
542
|
-
* ```ts
|
|
543
|
-
* import * as assert from "node:assert/strict"
|
|
544
|
-
* import { pipe, Option } from "effect"
|
|
545
|
-
* import * as Number from "effect/Number"
|
|
546
|
-
*
|
|
547
|
-
* // Data-first style (direct application)
|
|
548
|
-
* assert.equal(Number.divide(6, 3), Option.some(2)) // 6 ÷ 3 = 2
|
|
549
|
-
* assert.equal(Number.divide(-8, 4), Option.some(-2)) // (-8) ÷ 4 = -2
|
|
550
|
-
* assert.equal(Number.divide(-10, -5), Option.some(2)) // (-10) ÷ (-5) = 2
|
|
551
|
-
* assert.equal(Number.divide(1, 3), Option.some(0.3333333333333333)) // Note: floating-point approximation
|
|
552
|
-
*
|
|
553
|
-
* // Handling division by zero
|
|
554
|
-
* assert.equal(Number.divide(6, 0), Option.none()) // 6 ÷ 0 is undefined
|
|
555
|
-
*
|
|
556
|
-
* // Data-last style (pipeable)
|
|
557
|
-
* assert.equal(
|
|
558
|
-
* pipe(
|
|
559
|
-
* 10,
|
|
560
|
-
* Number.divide(2) // 10 ÷ 2 = 5
|
|
561
|
-
* ),
|
|
562
|
-
* Option.some(5)
|
|
563
|
-
* )
|
|
564
|
-
*
|
|
565
|
-
* // Chaining multiple divisions using Option combinators
|
|
566
|
-
* assert.equal(
|
|
567
|
-
* pipe(
|
|
568
|
-
* Option.some(24),
|
|
569
|
-
* Option.flatMap((n) => Number.divide(n, 2)), // 24 ÷ 2 = 12
|
|
570
|
-
* Option.flatMap(Number.divide(3)), // 12 ÷ 3 = 4
|
|
571
|
-
* Option.flatMap(Number.divide(2)) // 4 ÷ 2 = 2
|
|
572
|
-
* ),
|
|
573
|
-
* Option.some(2)
|
|
574
|
-
* )
|
|
575
|
-
*
|
|
576
|
-
* // Division-by-one property: a ÷ 1 = a
|
|
577
|
-
* assert.equal(Number.divide(42, 1), Option.some(42))
|
|
578
|
-
*
|
|
579
|
-
* // Self-division property: a ÷ a = 1 (for a ≠ 0)
|
|
580
|
-
* assert.equal(Number.divide(42, 42), Option.some(1))
|
|
581
|
-
*
|
|
582
|
-
* // Non-commutative property: a ÷ b ≠ b ÷ a
|
|
583
|
-
* assert.notDeepStrictEqual(
|
|
584
|
-
* Number.divide(6, 3), // 6 ÷ 3 = 2
|
|
585
|
-
* Number.divide(3, 6) // 3 ÷ 6 = 0.5
|
|
586
|
-
* )
|
|
587
|
-
* ```
|
|
588
|
-
*/
|
|
589
|
-
export const divide: {
|
|
590
|
-
/**
|
|
591
|
-
* Returns a function that divides its input by a specified divisor.
|
|
592
|
-
*
|
|
593
|
-
* @param divisor - The number to divide by
|
|
594
|
-
*
|
|
595
|
-
* @returns A function that takes a dividend and returns the quotient wrapped
|
|
596
|
-
* in an Option (Option.none() if divisor is 0)
|
|
597
|
-
*/
|
|
598
|
-
(divisor: number): (dividend: number) => Option<number>
|
|
599
|
-
|
|
600
|
-
/**
|
|
601
|
-
* Divides the dividend by the divisor and returns the quotient wrapped in an
|
|
602
|
-
* Option.
|
|
603
|
-
*
|
|
604
|
-
* @param dividend - The number to be divided
|
|
605
|
-
* @param divisor - The number to divide by
|
|
606
|
-
*
|
|
607
|
-
* @returns Some(quotient) if the divisor is not 0, None otherwise
|
|
608
|
-
*/
|
|
609
|
-
(dividend: number, divisor: number): Option<number>
|
|
610
|
-
} = dual(2, (dividend: number, divisor: number) => divisor === 0 ? option.none : option.some(dividend / divisor))
|
|
611
|
-
|
|
612
|
-
/**
|
|
613
|
-
* Performs division in the set of JavaScript numbers, but misbehaves for
|
|
614
|
-
* division by zero.
|
|
615
|
-
*
|
|
616
|
-
* Unlike {@link module:Number.divide} which returns an Option, this function
|
|
617
|
-
* directly returns a number or `Infinity` or `NaN`.
|
|
618
|
-
*
|
|
619
|
-
* - If the `divisor` is zero, it returns `Infinity`.
|
|
620
|
-
* - If both the `dividend` and the `divisor` are zero, then it returns `NaN`.
|
|
621
|
-
*
|
|
622
|
-
* @memberof Number
|
|
623
|
-
* @since 2.0.0
|
|
624
|
-
* @category math
|
|
625
|
-
* @example
|
|
626
|
-
*
|
|
627
|
-
* ```ts
|
|
628
|
-
* import * as assert from "node:assert/strict"
|
|
629
|
-
* import { pipe } from "effect"
|
|
630
|
-
* import * as Number from "effect/Number"
|
|
631
|
-
*
|
|
632
|
-
* // Data-first style (direct application)
|
|
633
|
-
* assert.equal(Number.unsafeDivide(6, 3), 2) // 6 ÷ 3 = 2
|
|
634
|
-
* assert.equal(Number.unsafeDivide(-8, 4), -2) // (-8) ÷ 4 = -2
|
|
635
|
-
* assert.equal(Number.unsafeDivide(-10, -5), 2) // (-10) ÷ (-5) = 2
|
|
636
|
-
* assert.equal(Number.unsafeDivide(1, 3), 0.3333333333333333)
|
|
637
|
-
*
|
|
638
|
-
* // Data-last style (pipeable)
|
|
639
|
-
* assert.equal(
|
|
640
|
-
* pipe(
|
|
641
|
-
* 10,
|
|
642
|
-
* Number.unsafeDivide(2) // 10 ÷ 2 = 5
|
|
643
|
-
* ),
|
|
644
|
-
* 5
|
|
645
|
-
* )
|
|
646
|
-
*
|
|
647
|
-
* // Chaining multiple divisions
|
|
648
|
-
* assert.equal(
|
|
649
|
-
* pipe(
|
|
650
|
-
* 24,
|
|
651
|
-
* Number.unsafeDivide(2), // 24 ÷ 2 = 12
|
|
652
|
-
* Number.unsafeDivide(3), // 12 ÷ 3 = 4
|
|
653
|
-
* Number.unsafeDivide(2) // 4 ÷ 2 = 2
|
|
654
|
-
* ),
|
|
655
|
-
* 2
|
|
656
|
-
* )
|
|
657
|
-
*
|
|
658
|
-
* assert.equal(Number.unsafeDivide(6, 0), Infinity)
|
|
659
|
-
*
|
|
660
|
-
* assert.equal(Number.unsafeDivide(0, 0), NaN)
|
|
661
|
-
*
|
|
662
|
-
* // Compare with safe division
|
|
663
|
-
* const safeResult = Number.divide(6, 3) // Option.some(2)
|
|
664
|
-
* const unsafeResult = Number.unsafeDivide(6, 3) // 2 directly
|
|
665
|
-
* ```
|
|
666
|
-
*
|
|
667
|
-
* @throws - An {@link module:Number.DivisionByZeroError} if the divisor is zero.
|
|
668
|
-
* @see {@link module:Number.divide} - Safe division returning an Option
|
|
669
|
-
*/
|
|
670
|
-
export const unsafeDivide: {
|
|
671
|
-
/**
|
|
672
|
-
* Returns a function that divides its input by a specified divisor.
|
|
673
|
-
*
|
|
674
|
-
* @param divisor - The number to divide by
|
|
675
|
-
*
|
|
676
|
-
* @returns A function that takes a dividend and returns the quotient
|
|
677
|
-
* @throws - An {@link module:Number.DivisionByZeroError} if the divisor is
|
|
678
|
-
* zero
|
|
679
|
-
*/
|
|
680
|
-
(divisor: number): (dividend: number) => number
|
|
681
|
-
|
|
682
|
-
/**
|
|
683
|
-
* Divides the dividend by the divisor and returns the quotient.
|
|
684
|
-
*
|
|
685
|
-
* If the divisor is zero, it returns Infinity.
|
|
686
|
-
*
|
|
687
|
-
* @param dividend - The number to be divided
|
|
688
|
-
* @param divisor - The number to divide by
|
|
689
|
-
*
|
|
690
|
-
* @returns The quotient of the division
|
|
691
|
-
*/
|
|
692
|
-
(dividend: number, divisor: number): number
|
|
693
|
-
} = dual(2, (dividend: number, divisor: number): number => dividend / divisor)
|
|
694
|
-
|
|
695
|
-
/**
|
|
696
|
-
* Returns the result of adding `1` to a given number.
|
|
697
|
-
*
|
|
698
|
-
* @memberof Number
|
|
699
|
-
* @since 2.0.0
|
|
700
|
-
* @category math
|
|
701
|
-
* @example
|
|
702
|
-
*
|
|
703
|
-
* ```ts
|
|
704
|
-
* import * as assert from "node:assert/strict"
|
|
705
|
-
* import { increment } from "effect/Number"
|
|
706
|
-
*
|
|
707
|
-
* assert.equal(increment(2), 3)
|
|
708
|
-
* ```
|
|
709
|
-
*/
|
|
710
|
-
export const increment = (n: number): number => sum(n, 1)
|
|
711
|
-
|
|
712
|
-
/**
|
|
713
|
-
* Decrements a number by `1`.
|
|
714
|
-
*
|
|
715
|
-
* @memberof Number
|
|
716
|
-
* @since 2.0.0
|
|
717
|
-
* @category math
|
|
718
|
-
* @example
|
|
719
|
-
*
|
|
720
|
-
* ```ts
|
|
721
|
-
* import * as assert from "node:assert/strict"
|
|
722
|
-
* import { decrement } from "effect/Number"
|
|
723
|
-
*
|
|
724
|
-
* assert.equal(decrement(3), 2)
|
|
725
|
-
* ```
|
|
726
|
-
*/
|
|
727
|
-
export const decrement = (n: number): number => subtract(n, 1)
|
|
728
|
-
|
|
729
|
-
/**
|
|
730
|
-
* @memberof Number
|
|
731
|
-
* @since 2.0.0
|
|
732
|
-
* @category instances
|
|
733
|
-
*/
|
|
734
|
-
export const Equivalence: equivalence.Equivalence<number> = equivalence.number
|
|
735
|
-
|
|
736
|
-
/**
|
|
737
|
-
* @memberof Number
|
|
738
|
-
* @since 2.0.0
|
|
739
|
-
* @category instances
|
|
740
|
-
*/
|
|
741
|
-
export const Order: order.Order<number> = order.number
|
|
742
|
-
|
|
743
|
-
/**
|
|
744
|
-
* Returns `true` if the first argument is less than the second, otherwise
|
|
745
|
-
* `false`.
|
|
746
|
-
*
|
|
747
|
-
* @memberof Number
|
|
748
|
-
* @since 2.0.0
|
|
749
|
-
* @category predicates
|
|
750
|
-
* @example
|
|
751
|
-
*
|
|
752
|
-
* ```ts
|
|
753
|
-
* import * as assert from "node:assert/strict"
|
|
754
|
-
* import { lessThan } from "effect/Number"
|
|
755
|
-
*
|
|
756
|
-
* assert.equal(lessThan(2, 3), true)
|
|
757
|
-
* assert.equal(lessThan(3, 3), false)
|
|
758
|
-
* assert.equal(lessThan(4, 3), false)
|
|
759
|
-
* ```
|
|
760
|
-
*/
|
|
761
|
-
export const lessThan: {
|
|
762
|
-
(that: number): (self: number) => boolean
|
|
763
|
-
(self: number, that: number): boolean
|
|
764
|
-
} = order.lessThan(Order)
|
|
765
|
-
|
|
766
|
-
/**
|
|
767
|
-
* Returns a function that checks if a given `number` is less than or equal to
|
|
768
|
-
* the provided one.
|
|
769
|
-
*
|
|
770
|
-
* @memberof Number
|
|
771
|
-
* @since 2.0.0
|
|
772
|
-
* @category predicates
|
|
773
|
-
* @example
|
|
774
|
-
*
|
|
775
|
-
* ```ts
|
|
776
|
-
* import * as assert from "node:assert/strict"
|
|
777
|
-
* import { lessThanOrEqualTo } from "effect/Number"
|
|
778
|
-
*
|
|
779
|
-
* assert.equal(lessThanOrEqualTo(2, 3), true)
|
|
780
|
-
* assert.equal(lessThanOrEqualTo(3, 3), true)
|
|
781
|
-
* assert.equal(lessThanOrEqualTo(4, 3), false)
|
|
782
|
-
* ```
|
|
783
|
-
*/
|
|
784
|
-
export const lessThanOrEqualTo: {
|
|
785
|
-
(that: number): (self: number) => boolean
|
|
786
|
-
(self: number, that: number): boolean
|
|
787
|
-
} = order.lessThanOrEqualTo(Order)
|
|
788
|
-
|
|
789
|
-
/**
|
|
790
|
-
* Returns `true` if the first argument is greater than the second, otherwise
|
|
791
|
-
* `false`.
|
|
792
|
-
*
|
|
793
|
-
* @memberof Number
|
|
794
|
-
* @since 2.0.0
|
|
795
|
-
* @category predicates
|
|
796
|
-
* @example
|
|
797
|
-
*
|
|
798
|
-
* ```ts
|
|
799
|
-
* import * as assert from "node:assert/strict"
|
|
800
|
-
* import { greaterThan } from "effect/Number"
|
|
801
|
-
*
|
|
802
|
-
* assert.equal(greaterThan(2, 3), false)
|
|
803
|
-
* assert.equal(greaterThan(3, 3), false)
|
|
804
|
-
* assert.equal(greaterThan(4, 3), true)
|
|
805
|
-
* ```
|
|
806
|
-
*/
|
|
807
|
-
export const greaterThan: {
|
|
808
|
-
(that: number): (self: number) => boolean
|
|
809
|
-
(self: number, that: number): boolean
|
|
810
|
-
} = order.greaterThan(Order)
|
|
811
|
-
|
|
812
|
-
/**
|
|
813
|
-
* Returns a function that checks if a given `number` is greater than or equal
|
|
814
|
-
* to the provided one.
|
|
815
|
-
*
|
|
816
|
-
* @memberof Number
|
|
817
|
-
* @since 2.0.0
|
|
818
|
-
* @category predicates
|
|
819
|
-
* @example
|
|
820
|
-
*
|
|
821
|
-
* ```ts
|
|
822
|
-
* import * as assert from "node:assert/strict"
|
|
823
|
-
* import { greaterThanOrEqualTo } from "effect/Number"
|
|
824
|
-
*
|
|
825
|
-
* assert.equal(greaterThanOrEqualTo(2, 3), false)
|
|
826
|
-
* assert.equal(greaterThanOrEqualTo(3, 3), true)
|
|
827
|
-
* assert.equal(greaterThanOrEqualTo(4, 3), true)
|
|
828
|
-
* ```
|
|
829
|
-
*/
|
|
830
|
-
export const greaterThanOrEqualTo: {
|
|
831
|
-
(that: number): (self: number) => boolean
|
|
832
|
-
(self: number, that: number): boolean
|
|
833
|
-
} = order.greaterThanOrEqualTo(Order)
|
|
834
|
-
|
|
835
|
-
/**
|
|
836
|
-
* Checks if a `number` is between a `minimum` and `maximum` value (inclusive).
|
|
837
|
-
*
|
|
838
|
-
* @memberof Number
|
|
839
|
-
* @since 2.0.0
|
|
840
|
-
* @category predicates
|
|
841
|
-
* @example
|
|
842
|
-
*
|
|
843
|
-
* ```ts
|
|
844
|
-
* import * as assert from "node:assert/strict"
|
|
845
|
-
* import { Number } from "effect"
|
|
846
|
-
*
|
|
847
|
-
* const between = Number.between({ minimum: 0, maximum: 5 })
|
|
848
|
-
*
|
|
849
|
-
* assert.equal(between(3), true)
|
|
850
|
-
* assert.equal(between(-1), false)
|
|
851
|
-
* assert.equal(between(6), false)
|
|
852
|
-
* ```
|
|
853
|
-
*/
|
|
854
|
-
export const between: {
|
|
855
|
-
(options: { minimum: number; maximum: number }): (self: number) => boolean
|
|
856
|
-
(
|
|
857
|
-
self: number,
|
|
858
|
-
options: {
|
|
859
|
-
minimum: number
|
|
860
|
-
maximum: number
|
|
861
|
-
}
|
|
862
|
-
): boolean
|
|
863
|
-
} = order.between(Order)
|
|
864
|
-
|
|
865
|
-
/**
|
|
866
|
-
* Restricts the given `number` to be within the range specified by the
|
|
867
|
-
* `minimum` and `maximum` values.
|
|
868
|
-
*
|
|
869
|
-
* - If the `number` is less than the `minimum` value, the function returns the
|
|
870
|
-
* `minimum` value.
|
|
871
|
-
* - If the `number` is greater than the `maximum` value, the function returns the
|
|
872
|
-
* `maximum` value.
|
|
873
|
-
* - Otherwise, it returns the original `number`.
|
|
874
|
-
*
|
|
875
|
-
* @memberof Number
|
|
876
|
-
* @since 2.0.0
|
|
877
|
-
* @example
|
|
878
|
-
*
|
|
879
|
-
* ```ts
|
|
880
|
-
* import * as assert from "node:assert/strict"
|
|
881
|
-
* import { Number } from "effect"
|
|
882
|
-
*
|
|
883
|
-
* const clamp = Number.clamp({ minimum: 1, maximum: 5 })
|
|
884
|
-
*
|
|
885
|
-
* assert.equal(clamp(3), 3)
|
|
886
|
-
* assert.equal(clamp(0), 1)
|
|
887
|
-
* assert.equal(clamp(6), 5)
|
|
888
|
-
* ```
|
|
889
|
-
*/
|
|
890
|
-
export const clamp: {
|
|
891
|
-
(options: { minimum: number; maximum: number }): (self: number) => number
|
|
892
|
-
(
|
|
893
|
-
self: number,
|
|
894
|
-
options: {
|
|
895
|
-
minimum: number
|
|
896
|
-
maximum: number
|
|
897
|
-
}
|
|
898
|
-
): number
|
|
899
|
-
} = order.clamp(Order)
|
|
900
|
-
|
|
901
|
-
/**
|
|
902
|
-
* Returns the minimum between two `number`s.
|
|
903
|
-
*
|
|
904
|
-
* @memberof Number
|
|
905
|
-
* @since 2.0.0
|
|
906
|
-
* @example
|
|
907
|
-
*
|
|
908
|
-
* ```ts
|
|
909
|
-
* import * as assert from "node:assert/strict"
|
|
910
|
-
* import { min } from "effect/Number"
|
|
911
|
-
*
|
|
912
|
-
* assert.equal(min(2, 3), 2)
|
|
913
|
-
* ```
|
|
914
|
-
*/
|
|
915
|
-
export const min: {
|
|
916
|
-
(that: number): (self: number) => number
|
|
917
|
-
(self: number, that: number): number
|
|
918
|
-
} = order.min(Order)
|
|
919
|
-
|
|
920
|
-
/**
|
|
921
|
-
* Returns the maximum between two `number`s.
|
|
922
|
-
*
|
|
923
|
-
* @memberof Number
|
|
924
|
-
* @since 2.0.0
|
|
925
|
-
* @example
|
|
926
|
-
*
|
|
927
|
-
* ```ts
|
|
928
|
-
* import * as assert from "node:assert/strict"
|
|
929
|
-
* import { max } from "effect/Number"
|
|
930
|
-
*
|
|
931
|
-
* assert.equal(max(2, 3), 3)
|
|
932
|
-
* ```
|
|
933
|
-
*/
|
|
934
|
-
export const max: {
|
|
935
|
-
(that: number): (self: number) => number
|
|
936
|
-
(self: number, that: number): number
|
|
937
|
-
} = order.max(Order)
|
|
938
|
-
|
|
939
|
-
/**
|
|
940
|
-
* Determines the sign of a given `number`.
|
|
941
|
-
*
|
|
942
|
-
* @memberof Number
|
|
943
|
-
* @since 2.0.0
|
|
944
|
-
* @category math
|
|
945
|
-
* @example
|
|
946
|
-
*
|
|
947
|
-
* ```ts
|
|
948
|
-
* import * as assert from "node:assert/strict"
|
|
949
|
-
* import { sign } from "effect/Number"
|
|
950
|
-
*
|
|
951
|
-
* assert.equal(sign(-5), -1)
|
|
952
|
-
* assert.equal(sign(0), 0)
|
|
953
|
-
* assert.equal(sign(5), 1)
|
|
954
|
-
* ```
|
|
955
|
-
*/
|
|
956
|
-
export const sign = (n: number): Ordering => Order(n, 0)
|
|
957
|
-
|
|
958
|
-
/**
|
|
959
|
-
* Returns the remainder left over when one operand is divided by a second
|
|
960
|
-
* operand.
|
|
961
|
-
*
|
|
962
|
-
* It always takes the sign of the dividend.
|
|
963
|
-
*
|
|
964
|
-
* @memberof Number
|
|
965
|
-
* @since 2.0.0
|
|
966
|
-
* @category math
|
|
967
|
-
* @example
|
|
968
|
-
*
|
|
969
|
-
* ```ts
|
|
970
|
-
* import * as assert from "node:assert/strict"
|
|
971
|
-
* import { remainder } from "effect/Number"
|
|
972
|
-
*
|
|
973
|
-
* assert.equal(remainder(2, 2), 0)
|
|
974
|
-
* assert.equal(remainder(3, 2), 1)
|
|
975
|
-
* assert.equal(remainder(-4, 2), -0)
|
|
976
|
-
* ```
|
|
977
|
-
*/
|
|
978
|
-
export const remainder: {
|
|
979
|
-
(divisor: number): (dividend: number) => number
|
|
980
|
-
(dividend: number, divisor: number): number
|
|
981
|
-
} = dual(2, (dividend: number, divisor: number): number => {
|
|
982
|
-
// https://stackoverflow.com/questions/3966484/why-does-modulus-operator-return-fractional-number-in-javascript/31711034#31711034
|
|
983
|
-
const selfDecCount = (dividend.toString().split(".")[1] || "").length
|
|
984
|
-
const divisorDecCount = (divisor.toString().split(".")[1] || "").length
|
|
985
|
-
const decCount = selfDecCount > divisorDecCount ? selfDecCount : divisorDecCount
|
|
986
|
-
const selfInt = parseInt(dividend.toFixed(decCount).replace(".", ""))
|
|
987
|
-
const divisorInt = parseInt(divisor.toFixed(decCount).replace(".", ""))
|
|
988
|
-
return (selfInt % divisorInt) / Math.pow(10, decCount)
|
|
989
|
-
})
|
|
990
|
-
|
|
991
|
-
/**
|
|
992
|
-
* Returns the next power of 2 greater than or equal to the given number.
|
|
993
|
-
*
|
|
994
|
-
* - For `positive` inputs, returns the smallest power of 2 that is >= the input
|
|
995
|
-
* - For `zero`, returns 2
|
|
996
|
-
* - For `negative` inputs, returns NaN (as logarithms of negative numbers are
|
|
997
|
-
* undefined)
|
|
998
|
-
* - For `NaN` input, returns NaN
|
|
999
|
-
* - For `Infinity`, returns Infinity
|
|
1000
|
-
*
|
|
1001
|
-
* @memberof Number
|
|
1002
|
-
* @since 2.0.0
|
|
1003
|
-
* @category math
|
|
1004
|
-
* @example
|
|
1005
|
-
*
|
|
1006
|
-
* ```ts
|
|
1007
|
-
* import * as assert from "node:assert/strict"
|
|
1008
|
-
* import { nextPow2 } from "effect/Number"
|
|
1009
|
-
*
|
|
1010
|
-
* assert.equal(nextPow2(5), 8)
|
|
1011
|
-
* assert.equal(nextPow2(17), 32)
|
|
1012
|
-
* assert.equal(nextPow2(0), 2)
|
|
1013
|
-
* assert.equal(Number.isNaN(nextPow2(-1)), true) // Negative inputs result in NaN
|
|
1014
|
-
* ```
|
|
1015
|
-
*/
|
|
1016
|
-
export const nextPow2 = (n: number): number => {
|
|
1017
|
-
const nextPow = Math.ceil(Math.log(n) / Math.log(2))
|
|
1018
|
-
return Math.max(Math.pow(2, nextPow), 2)
|
|
1019
|
-
}
|
|
1020
|
-
|
|
1021
|
-
/**
|
|
1022
|
-
* Tries to parse a `number` from a `string` using the `Number()` function. The
|
|
1023
|
-
* following special string values are supported: "NaN", "Infinity",
|
|
1024
|
-
* "-Infinity".
|
|
1025
|
-
*
|
|
1026
|
-
* @memberof Number
|
|
1027
|
-
* @since 2.0.0
|
|
1028
|
-
* @category constructors
|
|
1029
|
-
*/
|
|
1030
|
-
export const parse: {
|
|
1031
|
-
(s: string): Option<number>
|
|
1032
|
-
} = (s) => {
|
|
1033
|
-
if (s === "NaN") {
|
|
1034
|
-
return option.some(NaN)
|
|
1035
|
-
}
|
|
1036
|
-
if (s === "Infinity") {
|
|
1037
|
-
return option.some(Infinity)
|
|
1038
|
-
}
|
|
1039
|
-
if (s === "-Infinity") {
|
|
1040
|
-
return option.some(-Infinity)
|
|
1041
|
-
}
|
|
1042
|
-
if (s.trim() === "") {
|
|
1043
|
-
return option.none
|
|
1044
|
-
}
|
|
1045
|
-
const n = Number(s)
|
|
1046
|
-
return Number.isNaN(n) ? option.none : option.some(n)
|
|
1047
|
-
}
|
|
1048
|
-
|
|
1049
|
-
/**
|
|
1050
|
-
* Returns the number rounded with the given precision.
|
|
1051
|
-
*
|
|
1052
|
-
* @memberof Number
|
|
1053
|
-
* @since 3.8.0
|
|
1054
|
-
* @category math
|
|
1055
|
-
* @example
|
|
1056
|
-
*
|
|
1057
|
-
* ```ts
|
|
1058
|
-
* import * as assert from "node:assert/strict"
|
|
1059
|
-
* import { round } from "effect/Number"
|
|
1060
|
-
*
|
|
1061
|
-
* assert.equal(round(1.1234, 2), 1.12)
|
|
1062
|
-
* assert.equal(round(1.567, 2), 1.57)
|
|
1063
|
-
* ```
|
|
1064
|
-
*/
|
|
1065
|
-
export const round: {
|
|
1066
|
-
(precision: number): (self: number) => number
|
|
1067
|
-
(self: number, precision: number): number
|
|
1068
|
-
} = dual(2, (self: number, precision: number): number => {
|
|
1069
|
-
const factor = Math.pow(10, precision)
|
|
1070
|
-
return Math.round(self * factor) / factor
|
|
1071
|
-
})
|