effect 2.4.11 → 2.4.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.
Files changed (87) hide show
  1. package/dist/cjs/BigInt.js +75 -3
  2. package/dist/cjs/BigInt.js.map +1 -1
  3. package/dist/cjs/Duration.js +45 -33
  4. package/dist/cjs/Duration.js.map +1 -1
  5. package/dist/cjs/Number.js +1 -1
  6. package/dist/cjs/internal/clock.js +3 -3
  7. package/dist/cjs/internal/clock.js.map +1 -1
  8. package/dist/cjs/internal/version.js +1 -1
  9. package/dist/dts/BigInt.d.ts +57 -2
  10. package/dist/dts/BigInt.d.ts.map +1 -1
  11. package/dist/dts/Brand.d.ts.map +1 -1
  12. package/dist/dts/Chunk.d.ts +3 -3
  13. package/dist/dts/Chunk.d.ts.map +1 -1
  14. package/dist/dts/Data.d.ts +1 -1
  15. package/dist/dts/Data.d.ts.map +1 -1
  16. package/dist/dts/Duration.d.ts +8 -0
  17. package/dist/dts/Duration.d.ts.map +1 -1
  18. package/dist/dts/Either.d.ts.map +1 -1
  19. package/dist/dts/Equivalence.d.ts +3 -3
  20. package/dist/dts/Equivalence.d.ts.map +1 -1
  21. package/dist/dts/FiberMap.d.ts +1 -1
  22. package/dist/dts/FiberMap.d.ts.map +1 -1
  23. package/dist/dts/FiberSet.d.ts +1 -1
  24. package/dist/dts/FiberSet.d.ts.map +1 -1
  25. package/dist/dts/Function.d.ts.map +1 -1
  26. package/dist/dts/GlobalValue.d.ts.map +1 -1
  27. package/dist/dts/Hash.d.ts +2 -2
  28. package/dist/dts/Hash.d.ts.map +1 -1
  29. package/dist/dts/HashMap.d.ts +1 -1
  30. package/dist/dts/HashMap.d.ts.map +1 -1
  31. package/dist/dts/List.d.ts +1 -1
  32. package/dist/dts/List.d.ts.map +1 -1
  33. package/dist/dts/MutableHashMap.d.ts.map +1 -1
  34. package/dist/dts/MutableHashSet.d.ts.map +1 -1
  35. package/dist/dts/MutableList.d.ts +1 -1
  36. package/dist/dts/MutableList.d.ts.map +1 -1
  37. package/dist/dts/MutableQueue.d.ts.map +1 -1
  38. package/dist/dts/MutableRef.d.ts.map +1 -1
  39. package/dist/dts/NonEmptyIterable.d.ts +1 -1
  40. package/dist/dts/NonEmptyIterable.d.ts.map +1 -1
  41. package/dist/dts/Number.d.ts +1 -1
  42. package/dist/dts/Option.d.ts +2 -2
  43. package/dist/dts/Option.d.ts.map +1 -1
  44. package/dist/dts/Order.d.ts +2 -2
  45. package/dist/dts/Order.d.ts.map +1 -1
  46. package/dist/dts/Pipeable.d.ts.map +1 -1
  47. package/dist/dts/Predicate.d.ts +2 -2
  48. package/dist/dts/Predicate.d.ts.map +1 -1
  49. package/dist/dts/ReadonlyArray.d.ts +22 -22
  50. package/dist/dts/ReadonlyArray.d.ts.map +1 -1
  51. package/dist/dts/ReadonlyRecord.d.ts +2 -2
  52. package/dist/dts/ReadonlyRecord.d.ts.map +1 -1
  53. package/dist/dts/RequestResolver.d.ts.map +1 -1
  54. package/dist/dts/STM.d.ts.map +1 -1
  55. package/dist/dts/SortedMap.d.ts.map +1 -1
  56. package/dist/dts/SortedSet.d.ts.map +1 -1
  57. package/dist/dts/String.d.ts +1 -1
  58. package/dist/dts/String.d.ts.map +1 -1
  59. package/dist/dts/Struct.d.ts.map +1 -1
  60. package/dist/dts/TestAnnotation.d.ts.map +1 -1
  61. package/dist/dts/TestAnnotationMap.d.ts.map +1 -1
  62. package/dist/dts/TestClock.d.ts.map +1 -1
  63. package/dist/dts/TestServices.d.ts +1 -1
  64. package/dist/dts/TestServices.d.ts.map +1 -1
  65. package/dist/dts/Trie.d.ts +1 -1
  66. package/dist/dts/Trie.d.ts.map +1 -1
  67. package/dist/dts/Tuple.d.ts.map +1 -1
  68. package/dist/dts/Utils.d.ts.map +1 -1
  69. package/dist/dts/internal/core-stream.d.ts +1 -1
  70. package/dist/dts/internal/core-stream.d.ts.map +1 -1
  71. package/dist/dts/internal/hashMap.d.ts.map +1 -1
  72. package/dist/dts/internal/stack.d.ts +1 -1
  73. package/dist/dts/internal/stack.d.ts.map +1 -1
  74. package/dist/dts/internal/stm/stm.d.ts.map +1 -1
  75. package/dist/esm/BigInt.js +72 -2
  76. package/dist/esm/BigInt.js.map +1 -1
  77. package/dist/esm/Duration.js +44 -32
  78. package/dist/esm/Duration.js.map +1 -1
  79. package/dist/esm/Number.js +1 -1
  80. package/dist/esm/internal/clock.js +3 -3
  81. package/dist/esm/internal/clock.js.map +1 -1
  82. package/dist/esm/internal/version.js +1 -1
  83. package/package.json +1 -1
  84. package/src/BigInt.ts +77 -2
  85. package/src/Duration.ts +38 -18
  86. package/src/Number.ts +1 -1
  87. package/src/internal/version.ts +1 -1
package/src/BigInt.ts CHANGED
@@ -547,10 +547,24 @@ export const multiplyAll = (collection: Iterable<bigint>): bigint => {
547
547
  }
548
548
 
549
549
  /**
550
- * Convers a bigint into a number
550
+ * Takes a `bigint` and returns an `Option` of `number`.
551
+ *
552
+ * If the `bigint` is outside the safe integer range for JavaScript (`Number.MAX_SAFE_INTEGER`
553
+ * and `Number.MIN_SAFE_INTEGER`), it returns `Option.none()`. Otherwise, it converts the `bigint`
554
+ * to a number and returns `Option.some(number)`.
555
+ *
556
+ * @param b - The `bigint` to be converted to a `number`.
557
+ *
558
+ * @example
559
+ * import { toNumber } from "effect/BigInt"
560
+ * import { Option } from "effect"
561
+ *
562
+ * assert.deepStrictEqual(toNumber(BigInt(42)), Option.some(42))
563
+ * assert.deepStrictEqual(toNumber(BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1)), Option.none())
564
+ * assert.deepStrictEqual(toNumber(BigInt(Number.MIN_SAFE_INTEGER) - BigInt(1)), Option.none())
551
565
  *
552
- * @since 2.0.0
553
566
  * @category conversions
567
+ * @since 2.0.0
554
568
  */
555
569
  export const toNumber = (b: bigint): Option.Option<number> => {
556
570
  if (b > BigInt(Number.MAX_SAFE_INTEGER) || b < BigInt(Number.MIN_SAFE_INTEGER)) {
@@ -558,3 +572,64 @@ export const toNumber = (b: bigint): Option.Option<number> => {
558
572
  }
559
573
  return Option.some(Number(b))
560
574
  }
575
+
576
+ /**
577
+ * Takes a string and returns an `Option` of `bigint`.
578
+ *
579
+ * If the string is empty or contains characters that cannot be converted into a `bigint`,
580
+ * it returns `Option.none()`, otherwise, it returns `Option.some(bigint)`.
581
+ *
582
+ * @param s - The string to be converted to a `bigint`.
583
+ *
584
+ * @example
585
+ * import { fromString } from "effect/BigInt"
586
+ * import { Option } from "effect"
587
+ *
588
+ * assert.deepStrictEqual(fromString("42"), Option.some(BigInt(42)))
589
+ * assert.deepStrictEqual(fromString(" "), Option.none())
590
+ * assert.deepStrictEqual(fromString("a"), Option.none())
591
+ *
592
+ * @category conversions
593
+ * @since 2.4.12
594
+ */
595
+ export const fromString = (s: string): Option.Option<bigint> => {
596
+ try {
597
+ return s.trim() === ""
598
+ ? Option.none()
599
+ : Option.some(BigInt(s))
600
+ } catch (_) {
601
+ return Option.none()
602
+ }
603
+ }
604
+
605
+ /**
606
+ * Takes a number and returns an `Option` of `bigint`.
607
+ *
608
+ * If the number is outside the safe integer range for JavaScript (`Number.MAX_SAFE_INTEGER`
609
+ * and `Number.MIN_SAFE_INTEGER`), it returns `Option.none()`. Otherwise, it attempts to
610
+ * convert the number to a `bigint` and returns `Option.some(bigint)`.
611
+ *
612
+ * @param n - The number to be converted to a `bigint`.
613
+ *
614
+ * @example
615
+ * import { fromNumber } from "effect/BigInt"
616
+ * import { Option } from "effect"
617
+ *
618
+ * assert.deepStrictEqual(fromNumber(42), Option.some(BigInt(42)))
619
+ * assert.deepStrictEqual(fromNumber(Number.MAX_SAFE_INTEGER + 1), Option.none())
620
+ * assert.deepStrictEqual(fromNumber(Number.MIN_SAFE_INTEGER - 1), Option.none())
621
+ *
622
+ * @category conversions
623
+ * @since 2.4.12
624
+ */
625
+ export const fromNumber = (n: number): Option.Option<bigint> => {
626
+ if (n > Number.MAX_SAFE_INTEGER || n < Number.MIN_SAFE_INTEGER) {
627
+ return Option.none()
628
+ }
629
+
630
+ try {
631
+ return Option.some(BigInt(n))
632
+ } catch (_) {
633
+ return Option.none()
634
+ }
635
+ }
package/src/Duration.ts CHANGED
@@ -15,7 +15,11 @@ import { hasProperty, isBigInt, isNumber } from "./Predicate.js"
15
15
 
16
16
  const TypeId: unique symbol = Symbol.for("effect/Duration")
17
17
 
18
+ const bigint0 = BigInt(0)
19
+ const bigint24 = BigInt(24)
20
+ const bigint60 = BigInt(60)
18
21
  const bigint1e3 = BigInt(1_000)
22
+ const bigint1e6 = BigInt(1_000_000)
19
23
  const bigint1e9 = BigInt(1_000_000_000)
20
24
 
21
25
  /**
@@ -162,7 +166,7 @@ const DurationProto: Omit<Duration, "value"> = {
162
166
  const make = (input: number | bigint): Duration => {
163
167
  const duration = Object.create(DurationProto)
164
168
  if (isNumber(input)) {
165
- if (isNaN(input) || input < 0) {
169
+ if (isNaN(input) || input <= 0) {
166
170
  duration.value = zeroValue
167
171
  } else if (!Number.isFinite(input)) {
168
172
  duration.value = infinityValue
@@ -171,7 +175,7 @@ const make = (input: number | bigint): Duration => {
171
175
  } else {
172
176
  duration.value = { _tag: "Millis", millis: input }
173
177
  }
174
- } else if (input < BigInt(0)) {
178
+ } else if (input <= bigint0) {
175
179
  duration.value = zeroValue
176
180
  } else {
177
181
  duration.value = { _tag: "Nanos", nanos: input }
@@ -522,6 +526,22 @@ export const times: {
522
526
  })
523
527
  )
524
528
 
529
+ /**
530
+ * @since 2.0.0
531
+ * @category math
532
+ */
533
+ export const subtract: {
534
+ (that: DurationInput): (self: DurationInput) => Duration
535
+ (self: DurationInput, that: DurationInput): Duration
536
+ } = dual(
537
+ 2,
538
+ (self: DurationInput, that: DurationInput): Duration =>
539
+ matchWith(self, that, {
540
+ onMillis: (self, that) => make(self - that),
541
+ onNanos: (self, that) => make(self - that)
542
+ })
543
+ )
544
+
525
545
  /**
526
546
  * @since 2.0.0
527
547
  * @category math
@@ -631,32 +651,32 @@ export const format = (self: DurationInput): string => {
631
651
 
632
652
  const nanos = unsafeToNanos(duration)
633
653
 
634
- if (nanos % 1000000n) {
635
- parts.push(`${nanos % 1000000n}ns`)
654
+ if (nanos % bigint1e6) {
655
+ parts.push(`${nanos % bigint1e6}ns`)
636
656
  }
637
657
 
638
- const ms = nanos / 1000000n
639
- if (ms % 1000n !== 0n) {
640
- parts.push(`${ms % 1000n}ms`)
658
+ const ms = nanos / bigint1e6
659
+ if (ms % bigint1e3 !== bigint0) {
660
+ parts.push(`${ms % bigint1e3}ms`)
641
661
  }
642
662
 
643
- const sec = ms / 1000n
644
- if (sec % 60n !== 0n) {
645
- parts.push(`${sec % 60n}s`)
663
+ const sec = ms / bigint1e3
664
+ if (sec % bigint60 !== bigint0) {
665
+ parts.push(`${sec % bigint60}s`)
646
666
  }
647
667
 
648
- const min = sec / 60n
649
- if (min % 60n !== 0n) {
650
- parts.push(`${min % 60n}m`)
668
+ const min = sec / bigint60
669
+ if (min % bigint60 !== bigint0) {
670
+ parts.push(`${min % bigint60}m`)
651
671
  }
652
672
 
653
- const hr = min / 60n
654
- if (hr % 24n !== 0n) {
655
- parts.push(`${hr % 24n}h`)
673
+ const hr = min / bigint60
674
+ if (hr % bigint24 !== bigint0) {
675
+ parts.push(`${hr % bigint24}h`)
656
676
  }
657
677
 
658
- const days = hr / 24n
659
- if (days !== 0n) {
678
+ const days = hr / bigint24
679
+ if (days !== bigint0) {
660
680
  parts.push(`${days}d`)
661
681
  }
662
682
 
package/src/Number.ts CHANGED
@@ -400,7 +400,7 @@ export const sumAll = (collection: Iterable<number>): number => {
400
400
  * @param collection - The collection of `number`s to multiply.
401
401
  *
402
402
  * @example
403
- * import { multiplyAll } from 'effect/Number'
403
+ * import { multiplyAll } from "effect/Number"
404
404
  *
405
405
  * assert.deepStrictEqual(multiplyAll([2, 3, 4]), 24)
406
406
  *
@@ -1,4 +1,4 @@
1
- let moduleVersion = "2.4.11"
1
+ let moduleVersion = "2.4.13"
2
2
 
3
3
  export const getCurrentVersion = () => moduleVersion
4
4