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