atomic-fns 1.1.7 → 2.0.0

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 (216) hide show
  1. package/README.md +405 -60
  2. package/dist/array/index.d.ts +9 -0
  3. package/dist/array/index.js +9 -0
  4. package/dist/array/list.d.ts +99 -0
  5. package/dist/array/list.js +133 -0
  6. package/dist/array/sets.d.ts +130 -0
  7. package/dist/array/sets.js +213 -0
  8. package/dist/async/index.d.ts +6 -0
  9. package/dist/async/index.js +6 -0
  10. package/dist/async/retry.d.ts +38 -0
  11. package/dist/async/retry.js +56 -0
  12. package/dist/async/sleep.d.ts +42 -0
  13. package/dist/async/sleep.js +76 -0
  14. package/dist/collections/bisect.d.ts +103 -0
  15. package/dist/collections/bisect.js +157 -0
  16. package/dist/collections/deque.d.ts +67 -97
  17. package/dist/collections/deque.js +197 -249
  18. package/dist/collections/entries.d.ts +89 -0
  19. package/dist/collections/entries.js +131 -0
  20. package/dist/collections/frozen-set.d.ts +42 -0
  21. package/dist/collections/frozen-set.js +64 -0
  22. package/dist/collections/group.d.ts +141 -0
  23. package/dist/collections/group.js +241 -0
  24. package/dist/collections/heap.d.ts +64 -0
  25. package/dist/collections/heap.js +205 -0
  26. package/dist/collections/index.d.ts +15 -642
  27. package/dist/collections/index.js +14 -716
  28. package/dist/collections/internal/arrayKeys.d.ts +19 -0
  29. package/dist/collections/internal/arrayKeys.js +83 -0
  30. package/dist/collections/internal/invariants.d.ts +4 -0
  31. package/dist/collections/internal/invariants.js +12 -0
  32. package/dist/collections/internal/iterable.d.ts +25 -0
  33. package/dist/collections/internal/iterable.js +62 -0
  34. package/dist/collections/iterate.d.ts +162 -0
  35. package/dist/collections/iterate.js +188 -0
  36. package/dist/collections/lru-cache.d.ts +58 -0
  37. package/dist/collections/lru-cache.js +164 -0
  38. package/dist/collections/order.d.ts +19 -0
  39. package/dist/collections/order.js +38 -0
  40. package/dist/collections/shape.d.ts +32 -0
  41. package/dist/collections/shape.js +86 -0
  42. package/dist/collections/sorted-map.d.ts +171 -0
  43. package/dist/collections/sorted-map.js +845 -0
  44. package/dist/collections/sorted-set.d.ts +103 -0
  45. package/dist/collections/sorted-set.js +147 -0
  46. package/dist/collections/splay-tree.d.ts +82 -0
  47. package/dist/collections/splay-tree.js +354 -0
  48. package/dist/collections/trie.d.ts +58 -0
  49. package/dist/collections/trie.js +196 -0
  50. package/dist/decimal/currency.d.ts +211 -0
  51. package/dist/decimal/currency.js +321 -0
  52. package/dist/decimal/decimal.d.ts +299 -0
  53. package/dist/decimal/decimal.js +660 -0
  54. package/dist/decimal/index.d.ts +11 -145
  55. package/dist/decimal/index.js +10 -374
  56. package/dist/decimal/internal/bits.d.ts +2 -0
  57. package/dist/decimal/internal/bits.js +7 -0
  58. package/dist/decimal/internal/echo.d.ts +4 -0
  59. package/dist/decimal/internal/echo.js +6 -0
  60. package/dist/decimal/internal/isqrt.d.ts +9 -0
  61. package/dist/decimal/internal/isqrt.js +34 -0
  62. package/dist/decimal/internal/locale.d.ts +7 -0
  63. package/dist/decimal/internal/locale.js +24 -0
  64. package/dist/decimal/internal/minor-units.d.ts +2 -0
  65. package/dist/decimal/internal/minor-units.js +33 -0
  66. package/dist/decimal/internal/pow10.d.ts +24 -0
  67. package/dist/decimal/internal/pow10.js +96 -0
  68. package/dist/encoding/base64.d.ts +39 -0
  69. package/dist/encoding/base64.js +53 -0
  70. package/dist/encoding/hex.d.ts +23 -0
  71. package/dist/encoding/hex.js +34 -0
  72. package/dist/encoding/index.d.ts +6 -0
  73. package/dist/encoding/index.js +6 -0
  74. package/dist/function/debounce.d.ts +68 -0
  75. package/dist/function/debounce.js +203 -0
  76. package/dist/function/index.d.ts +6 -0
  77. package/dist/function/index.js +6 -0
  78. package/dist/function/shared.d.ts +2 -0
  79. package/dist/function/shared.js +5 -0
  80. package/dist/function/wrap.d.ts +130 -0
  81. package/dist/function/wrap.js +113 -0
  82. package/dist/hash/digest.d.ts +18 -0
  83. package/dist/hash/digest.js +26 -0
  84. package/dist/hash/hash.d.ts +22 -0
  85. package/dist/hash/hash.js +252 -0
  86. package/dist/hash/hashString.d.ts +13 -0
  87. package/dist/hash/hashString.js +53 -0
  88. package/dist/hash/index.d.ts +8 -0
  89. package/dist/hash/index.js +8 -0
  90. package/dist/hash/stableStringify.d.ts +19 -0
  91. package/dist/hash/stableStringify.js +102 -0
  92. package/dist/id/index.d.ts +7 -0
  93. package/dist/id/index.js +7 -0
  94. package/dist/id/nanoid.d.ts +16 -0
  95. package/dist/id/nanoid.js +65 -0
  96. package/dist/id/uniqueId.d.ts +14 -0
  97. package/dist/id/uniqueId.js +20 -0
  98. package/dist/id/uuid.d.ts +25 -0
  99. package/dist/id/uuid.js +64 -0
  100. package/dist/index.d.ts +15 -8
  101. package/dist/index.js +15 -8
  102. package/dist/internal/bytes.d.ts +11 -0
  103. package/dist/internal/bytes.js +33 -0
  104. package/dist/internal/codec.d.ts +11 -0
  105. package/dist/internal/codec.js +84 -0
  106. package/dist/internal/compare.d.ts +10 -0
  107. package/dist/internal/compare.js +26 -0
  108. package/dist/internal/crypto.d.ts +5 -0
  109. package/dist/internal/crypto.js +11 -0
  110. package/dist/internal/keys.d.ts +12 -0
  111. package/dist/internal/keys.js +23 -0
  112. package/dist/internal/kind.d.ts +44 -0
  113. package/dist/internal/kind.js +139 -0
  114. package/dist/internal/sparse.d.ts +51 -0
  115. package/dist/internal/sparse.js +104 -0
  116. package/dist/internal/text.d.ts +7 -0
  117. package/dist/internal/text.js +15 -0
  118. package/dist/internal/timer.d.ts +10 -0
  119. package/dist/internal/timer.js +17 -0
  120. package/dist/internal/truncate.d.ts +10 -0
  121. package/dist/internal/truncate.js +22 -0
  122. package/dist/internal/walk.d.ts +47 -0
  123. package/dist/internal/walk.js +100 -0
  124. package/dist/is/index.d.ts +8 -0
  125. package/dist/is/index.js +8 -0
  126. package/dist/is/isArrayLike.d.ts +18 -0
  127. package/dist/is/isArrayLike.js +25 -0
  128. package/dist/is/isError.d.ts +13 -0
  129. package/dist/is/isError.js +18 -0
  130. package/dist/is/isNil.d.ts +12 -0
  131. package/dist/is/isNil.js +14 -0
  132. package/dist/is/isPlainObject.d.ts +15 -0
  133. package/dist/is/isPlainObject.js +34 -0
  134. package/dist/number/index.d.ts +7 -0
  135. package/dist/number/index.js +7 -0
  136. package/dist/number/round.d.ts +86 -0
  137. package/dist/number/round.js +130 -0
  138. package/dist/number/stats.d.ts +52 -0
  139. package/dist/number/stats.js +115 -0
  140. package/dist/object/cloneDeep.d.ts +28 -0
  141. package/dist/object/cloneDeep.js +200 -0
  142. package/dist/object/index.d.ts +14 -0
  143. package/dist/object/index.js +13 -0
  144. package/dist/object/isEqual.d.ts +64 -0
  145. package/dist/object/isEqual.js +289 -0
  146. package/dist/object/matches.d.ts +40 -0
  147. package/dist/object/matches.js +284 -0
  148. package/dist/object/merge.d.ts +66 -0
  149. package/dist/object/merge.js +272 -0
  150. package/dist/object/parsePath.d.ts +22 -0
  151. package/dist/object/parsePath.js +111 -0
  152. package/dist/object/path.d.ts +85 -0
  153. package/dist/object/path.js +208 -0
  154. package/dist/object/pick.d.ts +91 -0
  155. package/dist/object/pick.js +180 -0
  156. package/dist/object/reshape.d.ts +73 -0
  157. package/dist/object/reshape.js +144 -0
  158. package/dist/random/index.d.ts +96 -32
  159. package/dist/random/index.js +215 -50
  160. package/dist/string/case.d.ts +71 -0
  161. package/dist/string/case.js +152 -0
  162. package/dist/string/fold.d.ts +35 -0
  163. package/dist/string/fold.js +100 -0
  164. package/dist/string/index.d.ts +6 -187
  165. package/dist/string/index.js +6 -226
  166. package/dist/string/truncate.d.ts +25 -0
  167. package/dist/string/truncate.js +44 -0
  168. package/dist/string/words.d.ts +27 -0
  169. package/dist/string/words.js +241 -0
  170. package/package.json +101 -58
  171. package/dist/collections/BTree.d.ts +0 -229
  172. package/dist/collections/BTree.js +0 -1165
  173. package/dist/collections/Heap.d.ts +0 -69
  174. package/dist/collections/Heap.js +0 -181
  175. package/dist/collections/LRUCache.d.ts +0 -80
  176. package/dist/collections/LRUCache.js +0 -126
  177. package/dist/collections/SortedMap.d.ts +0 -92
  178. package/dist/collections/SortedMap.js +0 -131
  179. package/dist/collections/SortedSet.d.ts +0 -69
  180. package/dist/collections/SortedSet.js +0 -99
  181. package/dist/collections/SortedTree.d.ts +0 -103
  182. package/dist/collections/SortedTree.js +0 -141
  183. package/dist/collections/SplayTree.d.ts +0 -105
  184. package/dist/collections/SplayTree.js +0 -338
  185. package/dist/collections/Trie.d.ts +0 -42
  186. package/dist/collections/Trie.js +0 -104
  187. package/dist/collections/abc.d.ts +0 -152
  188. package/dist/collections/abc.js +0 -39
  189. package/dist/collections/frozenset.d.ts +0 -19
  190. package/dist/collections/frozenset.js +0 -30
  191. package/dist/functools/index.d.ts +0 -130
  192. package/dist/functools/index.js +0 -235
  193. package/dist/globals/index.d.ts +0 -285
  194. package/dist/globals/index.js +0 -463
  195. package/dist/i18n/currency.d.ts +0 -24
  196. package/dist/i18n/currency.js +0 -85
  197. package/dist/i18n/date/index.d.ts +0 -203
  198. package/dist/i18n/date/index.js +0 -531
  199. package/dist/i18n/date/utils.d.ts +0 -65
  200. package/dist/i18n/date/utils.js +0 -290
  201. package/dist/i18n/duration.d.ts +0 -112
  202. package/dist/i18n/duration.js +0 -392
  203. package/dist/i18n/format.d.ts +0 -31
  204. package/dist/i18n/format.js +0 -506
  205. package/dist/i18n/index.d.ts +0 -8
  206. package/dist/i18n/index.js +0 -8
  207. package/dist/itertools/index.d.ts +0 -255
  208. package/dist/itertools/index.js +0 -437
  209. package/dist/math/index.d.ts +0 -96
  210. package/dist/math/index.js +0 -98
  211. package/dist/mod.d.ts +0 -1
  212. package/dist/mod.js +0 -1
  213. package/dist/operators/index.d.ts +0 -152
  214. package/dist/operators/index.js +0 -305
  215. package/dist/types.d.ts +0 -19
  216. package/dist/types.js +0 -1
@@ -1,148 +1,14 @@
1
1
  /**
2
- * `Decimal` provides support for correct rounded floating point arithmetic.
2
+ * Exact decimal arithmetic shaped after the TC39 Decimal draft, with Currency.
3
3
  *
4
- * @module decimal
5
- */
6
- /**
7
- * Casts the given value as a {@link Decimal}.
8
- */
9
- export declare function decimal(x: any): Decimal;
10
- /**
11
- * `Decimal` provides support for correct rounded floating point arithmetic, unlike the standard
12
- * `Number` type. It also supports user-defined precision (default is 20 decimal places), which
13
- * can be as large as needed.
4
+ * @example
5
+ * ```ts
6
+ * import { Decimal } from 'atomic-fns/decimal'
7
+ *
8
+ * new Decimal('0.1').add('0.2').toString() // '0.3'
9
+ * ```
10
+ * @module
14
11
  */
15
- export declare class Decimal {
16
- static PRECISION: number;
17
- private i;
18
- private e;
19
- /**
20
- * Returns a new `Decimal` instance from the value or `0`.
21
- * @param {*} value
22
- * @returns {Decimal}
23
- */
24
- constructor(value?: any);
25
- /**
26
- * Returns a new `Decimal` that has the opposite sign of this `Decimal`.
27
- * @example
28
- ```js
29
- let a = decimal('5')
30
- a.negated() // -5
31
- ```
32
- * @returns {Decimal}
33
- */
34
- negated(): Decimal;
35
- /**
36
- * Returns a new `Decimal` that is the sum of this and `x`.
37
- * @param x
38
- * @example
39
- ```js
40
- let a = decimal('50000000000')
41
- let b = decimal('0.000000005')
42
- a.add(b) // 50000000000.000000005
43
- ```
44
- * @returns {Decimal}
45
- */
46
- add(x: any): Decimal;
47
- /**
48
- * Returns a new `Decimal` that is the difference between this and `x`.
49
- * @param x
50
- * @example
51
- ```js
52
- let a = decimal('1')
53
- let b = decimal('0.0000000000000000001')
54
- a.sub(b) // 0.9999999999999999999
55
- ```
56
- * @returns {Decimal}
57
- */
58
- sub(x: any): Decimal;
59
- /**
60
- * Returns a new `Decimal` that is the product of this and `x`.
61
- * @param x
62
- * @example
63
- ```js
64
- let a = decimal('0.0000000000000000000025')
65
- let b = decimal('400000000000000000000')
66
- a.mul(b) // 1
67
- ```
68
- * @returns {Decimal}
69
- */
70
- mul(x: any): Decimal;
71
- /**
72
- * Returns a new `Decimal` that is the quotient of this and `x`
73
- * @param x
74
- * @example
75
- ```js
76
- let a = decimal('1')
77
- let b = decimal('3')
78
- a.div(b) // 0.333333333333333333333333333333
79
- ```
80
- * @returns {Decimal}
81
- */
82
- div(x: Decimal | string | number): Decimal;
83
- /**
84
- * Returns the square root of this `Decimal`.
85
- * @returns {TDecimal}
86
- */
87
- sqrt(): Decimal;
88
- /** Returns a new copy of this Decimal value. */
89
- clone(): Decimal;
90
- /**
91
- * Remove zeroes in the least-significant digit of this `Decimal`
92
- */
93
- private _normalize;
94
- /**
95
- * Truncate this `Decimal` to the configured precision
96
- */
97
- private _truncate;
98
- /**
99
- * Returns a string representation of this decimal.
100
- */
101
- toString(): string;
102
- /**
103
- * Converts this decimal to the `Number` value
104
- * @returns {number}
105
- */
106
- toNumber(): number;
107
- static get precision(): number;
108
- static set precision(value: number);
109
- /**
110
- * Returns `true` if this value is equal to the value of `x`, otherwise `false`.
111
- * @param {*} x other
112
- * @returns {boolean}
113
- */
114
- eq(x: any): boolean;
115
- /**
116
- * Returns `true` if this value is less than the value of `x`, otherwise `false`.
117
- * @param {*} x
118
- * @returns {boolean}
119
- */
120
- lt(x: any): boolean;
121
- /**
122
- * Returns a comparison value representing the ordering of this value in respect to `x`.
123
- * * `-1` if this < x
124
- * * `1` if this > x
125
- * * `0` if this == x
126
- * @param {*} x
127
- * @returns {number}
128
- */
129
- compare(x: any): 0 | 1 | -1;
130
- /**
131
- * Returns `true` if this value is greater than the value of `x`, otherwise `false`.
132
- * @param {*} x
133
- * @returns {boolean}
134
- */
135
- gt(x: any): boolean;
136
- /**
137
- * Returns `true` if this value is less than or equal to the value of `x`, otherwise `false`.
138
- * @param {*} x
139
- * @returns {boolean}
140
- */
141
- lte(x: any): boolean;
142
- /**
143
- * Returns `true` if this value is greater than or equal to the value of `x`, otherwise `false`.
144
- * @param {*} x
145
- * @returns {boolean}
146
- */
147
- gte(x: any): boolean;
148
- }
12
+ export { Currency } from './currency.js';
13
+ export { Decimal } from './decimal.js';
14
+ export type { DecimalInput, DivideOptions, RoundingMode } from './decimal.js';
@@ -1,377 +1,13 @@
1
1
  /**
2
- * `Decimal` provides support for correct rounded floating point arithmetic.
2
+ * Exact decimal arithmetic shaped after the TC39 Decimal draft, with Currency.
3
3
  *
4
- * @module decimal
5
- */
6
- const NUMBER_REGEX = /^(-?)([0-9]+)(\.([0-9]+))?$/;
7
- /**
8
- * Casts the given value as a {@link Decimal}.
9
- */
10
- export function decimal(x) {
11
- return new Decimal(x);
12
- }
13
- /**
14
- * `Decimal` provides support for correct rounded floating point arithmetic, unlike the standard
15
- * `Number` type. It also supports user-defined precision (default is 20 decimal places), which
16
- * can be as large as needed.
4
+ * @example
5
+ * ```ts
6
+ * import { Decimal } from 'atomic-fns/decimal'
7
+ *
8
+ * new Decimal('0.1').add('0.2').toString() // '0.3'
9
+ * ```
10
+ * @module
17
11
  */
18
- export class Decimal {
19
- static PRECISION = 20;
20
- i = 0n;
21
- e = 0;
22
- /**
23
- * Returns a new `Decimal` instance from the value or `0`.
24
- * @param {*} value
25
- * @returns {Decimal}
26
- */
27
- constructor(value) {
28
- if (value == null) {
29
- return this;
30
- }
31
- // Check if value is a Decimal like
32
- if (typeof value?.i === 'bigint' && typeof value.e === 'number') {
33
- this.i = value.i;
34
- this.e = value.e;
35
- this._normalize();
36
- return this;
37
- }
38
- const result = NUMBER_REGEX.exec(value.toString());
39
- if (result) {
40
- // console.log(result);
41
- // Construct from string if possible
42
- // this.i = parseInt(result[1] + result[2] + result[4]);
43
- if (typeof result[2] === 'undefined')
44
- result[2] = '';
45
- if (typeof result[4] === 'undefined')
46
- result[4] = '';
47
- this.i = BigInt(result[1] + result[2] + result[4]);
48
- this.e = result[4] ? -result[4].length : 0;
49
- this._normalize();
50
- }
51
- else {
52
- // Conversion to string failed, so construct from number
53
- let m = 1n;
54
- if (value < 0) {
55
- value = -value;
56
- m = -1n;
57
- }
58
- if (value === 0) {
59
- this.i = 0n;
60
- this.e = 0;
61
- }
62
- else {
63
- // Multiply the number by 10 until it is an integer
64
- let e = -Math.floor(Math.log10(value));
65
- if (e > 0) {
66
- value *= Math.pow(10, e);
67
- }
68
- else {
69
- e = 0;
70
- }
71
- while (value !== Math.round(value)) {
72
- e++;
73
- value *= 10;
74
- }
75
- // Divide the number by 10 until the one's digit is non-zero
76
- while (value % 10 === 0 && value > 0) {
77
- e--;
78
- value /= 10;
79
- }
80
- value = BigInt(value);
81
- this.i = value * m;
82
- this.e = -e;
83
- }
84
- }
85
- }
86
- /**
87
- * Returns a new `Decimal` that has the opposite sign of this `Decimal`.
88
- * @example
89
- ```js
90
- let a = decimal('5')
91
- a.negated() // -5
92
- ```
93
- * @returns {Decimal}
94
- */
95
- negated() {
96
- const negated = new Decimal();
97
- negated.i = -this.i;
98
- negated.e = this.e;
99
- return negated;
100
- }
101
- /**
102
- * Returns a new `Decimal` that is the sum of this and `x`.
103
- * @param x
104
- * @example
105
- ```js
106
- let a = decimal('50000000000')
107
- let b = decimal('0.000000005')
108
- a.add(b) // 50000000000.000000005
109
- ```
110
- * @returns {Decimal}
111
- */
112
- add(x) {
113
- let a = new Decimal(this);
114
- let b = new Decimal(x);
115
- // a+b = a.i * 10^a.e + b.i * 10^b.e
116
- // a+b = ( a.i * 10^(a.e-b.e) + b.i ) * 10^b.e
117
- const result = new Decimal();
118
- // Order arguments so that a.e >= b.e
119
- if (a.e < b.e) {
120
- const c = a;
121
- a = b;
122
- b = c;
123
- }
124
- const intPart = exp(a.i, a.e - b.e);
125
- result.i = intPart + b.i;
126
- result.e = b.e;
127
- result._normalize();
128
- return result;
129
- }
130
- /**
131
- * Returns a new `Decimal` that is the difference between this and `x`.
132
- * @param x
133
- * @example
134
- ```js
135
- let a = decimal('1')
136
- let b = decimal('0.0000000000000000001')
137
- a.sub(b) // 0.9999999999999999999
138
- ```
139
- * @returns {Decimal}
140
- */
141
- sub(x) {
142
- return this.add(decimal(x).negated());
143
- }
144
- /**
145
- * Returns a new `Decimal` that is the product of this and `x`.
146
- * @param x
147
- * @example
148
- ```js
149
- let a = decimal('0.0000000000000000000025')
150
- let b = decimal('400000000000000000000')
151
- a.mul(b) // 1
152
- ```
153
- * @returns {Decimal}
154
- */
155
- mul(x) {
156
- const a = new Decimal(this);
157
- const b = new Decimal(x);
158
- // a * b = (a.i * b.i) * 10^(a.e + b.e)
159
- const result = new Decimal();
160
- result.i = a.i * b.i;
161
- result.e = a.e + b.e;
162
- result._normalize();
163
- return result;
164
- }
165
- /**
166
- * Returns a new `Decimal` that is the quotient of this and `x`
167
- * @param x
168
- * @example
169
- ```js
170
- let a = decimal('1')
171
- let b = decimal('3')
172
- a.div(b) // 0.333333333333333333333333333333
173
- ```
174
- * @returns {Decimal}
175
- */
176
- div(x) {
177
- const a = new Decimal(this);
178
- const b = new Decimal(x);
179
- // We need to increase the number of digits of a and b so that a / b will have the desired precision.
180
- // In order for the integer quotient to have the correct precision, a.e - b.e must be greater than that precision.
181
- // So we need only to increase a.e.
182
- // TODO: Revisit the value of increaseA. Is it correct in every circumstance?
183
- const increaseA = Math.max(Decimal.precision - (a.e + b.e), 0);
184
- const intIncrease = new Decimal();
185
- intIncrease.i = exp(a.i, increaseA);
186
- const quotient = new Decimal();
187
- quotient.i = intIncrease.i / b.i;
188
- quotient.e = a.e - b.e - increaseA;
189
- // No need to truncate since we already increased the number of digits before dividing
190
- quotient._normalize();
191
- return quotient;
192
- }
193
- /**
194
- * Returns the square root of this `Decimal`.
195
- * @returns {TDecimal}
196
- */
197
- sqrt() {
198
- Decimal.PRECISION++;
199
- // Let the guess value be 10^(e/2), where e is this.e
200
- let x = new Decimal();
201
- x.i = 1n;
202
- x.e = Math.floor(this.e / 2);
203
- const onehalf = new Decimal('0.5');
204
- let xt = x.clone();
205
- xt._truncate();
206
- xt._normalize();
207
- let lastxt;
208
- let i;
209
- for (i = 0; i < 100; i++) {
210
- lastxt = xt;
211
- x = this.div(x).add(x).mul(onehalf);
212
- xt = x.clone();
213
- xt._truncate();
214
- xt._normalize();
215
- if (xt.i === lastxt.i && xt.e === lastxt.e)
216
- break;
217
- }
218
- if (i >= 100) {
219
- console.log('Warning: sqrt exceeded maximum iterations. (Did it enter a cycle?)');
220
- }
221
- Decimal.PRECISION--;
222
- xt._truncate();
223
- xt._normalize();
224
- return xt;
225
- }
226
- /** Returns a new copy of this Decimal value. */
227
- clone() {
228
- const a = new Decimal();
229
- a.i = this.i;
230
- a.e = this.e;
231
- return a;
232
- }
233
- /**
234
- * Remove zeroes in the least-significant digit of this `Decimal`
235
- */
236
- _normalize() {
237
- while (this.i % 10n === 0n && this.i !== 0n) {
238
- this.i /= 10n;
239
- this.e++;
240
- }
241
- return this;
242
- }
243
- /**
244
- * Truncate this `Decimal` to the configured precision
245
- */
246
- _truncate() {
247
- // TODO: Make this better
248
- // TODO: Rounding
249
- const trunc = -this.e - Decimal.PRECISION;
250
- if (trunc > 0) {
251
- this.i /= 10n ** BigInt(trunc - 1);
252
- this.e += trunc;
253
- const nextDigit = this.i % 10n;
254
- if (nextDigit >= 5) {
255
- this.i += 10n;
256
- }
257
- this.i /= 10n;
258
- }
259
- // while(-this.e > Decimal.PRECISION) {
260
- // this.i /= 10n;
261
- // this.e++;
262
- // }
263
- return this;
264
- }
265
- /**
266
- * Returns a string representation of this decimal.
267
- */
268
- toString() {
269
- let s = this.i.toString();
270
- let m = '';
271
- if (s[0] === '-') {
272
- s = s.substring(1, s.length);
273
- m = '-';
274
- }
275
- if (this.e > 0) {
276
- return m + s + '0'.repeat(this.e);
277
- }
278
- else if (this.e < 0) {
279
- if (-this.e >= s.length) {
280
- return m + '0.' + '0'.repeat(-this.e - s.length) + s;
281
- }
282
- return m + s.slice(0, s.length + this.e) + '.' + s.slice(s.length + this.e, s.length);
283
- }
284
- else {
285
- return m + s;
286
- }
287
- }
288
- [Symbol.for('nodejs.util.inspect.custom')]() {
289
- return this.toString();
290
- }
291
- /**
292
- * Converts this decimal to the `Number` value
293
- * @returns {number}
294
- */
295
- toNumber() {
296
- return Number(Decimal.prototype.toString.call(this));
297
- }
298
- static get precision() {
299
- return Decimal.PRECISION;
300
- }
301
- static set precision(value) {
302
- Decimal.PRECISION = value;
303
- }
304
- /**
305
- * Returns `true` if this value is equal to the value of `x`, otherwise `false`.
306
- * @param {*} x other
307
- * @returns {boolean}
308
- */
309
- eq(x) {
310
- const other = decimal(x);
311
- return this.i === other.i && this.e === other.e;
312
- }
313
- /**
314
- * Returns `true` if this value is less than the value of `x`, otherwise `false`.
315
- * @param {*} x
316
- * @returns {boolean}
317
- */
318
- lt(x) {
319
- const other = decimal(x);
320
- if (this.e === other.e)
321
- return this.i < other.i;
322
- const e = this.e - other.e;
323
- if (e > 0) {
324
- return exp(this.i, e) < other.i;
325
- }
326
- return this.i < exp(other.i, -e);
327
- }
328
- /**
329
- * Returns a comparison value representing the ordering of this value in respect to `x`.
330
- * * `-1` if this < x
331
- * * `1` if this > x
332
- * * `0` if this == x
333
- * @param {*} x
334
- * @returns {number}
335
- */
336
- compare(x) {
337
- if (this.eq(x))
338
- return 0;
339
- if (this.lt(x))
340
- return -1;
341
- return 1;
342
- }
343
- /**
344
- * Returns `true` if this value is greater than the value of `x`, otherwise `false`.
345
- * @param {*} x
346
- * @returns {boolean}
347
- */
348
- gt(x) {
349
- return this.compare(x) > 0;
350
- }
351
- /**
352
- * Returns `true` if this value is less than or equal to the value of `x`, otherwise `false`.
353
- * @param {*} x
354
- * @returns {boolean}
355
- */
356
- lte(x) {
357
- return this.compare(x) <= 0;
358
- }
359
- /**
360
- * Returns `true` if this value is greater than or equal to the value of `x`, otherwise `false`.
361
- * @param {*} x
362
- * @returns {boolean}
363
- */
364
- gte(x) {
365
- return this.compare(x) >= 0;
366
- }
367
- }
368
- /** Returns a BigInt that is equal to `i` times `10^e` */
369
- function exp(i, e) {
370
- // TODO: Make this better
371
- // The implementation is faster than 10n ** BigInt(e). But could we make it faster still?
372
- let ii = i;
373
- for (let j = 0; j < e; j++) {
374
- ii *= 10n;
375
- }
376
- return ii;
377
- }
12
+ export { Currency } from './currency.js';
13
+ export { Decimal } from './decimal.js';
@@ -0,0 +1,2 @@
1
+ /** Bit length of a non-negative bigint (0 for 0n), read from its hex form, not its binary one. */
2
+ export declare function bitLength(n: bigint): number;
@@ -0,0 +1,7 @@
1
+ /** Bit length of a non-negative bigint (0 for 0n), read from its hex form, not its binary one. */
2
+ export function bitLength(n) {
3
+ if (n === 0n)
4
+ return 0;
5
+ const hex = n.toString(16);
6
+ return (hex.length - 1) * 4 + (32 - Math.clz32(parseInt(hex[0], 16)));
7
+ }
@@ -0,0 +1,4 @@
1
+ /** UTF-16 units of bad input an error message repeats. */
2
+ export declare const MAX_ECHO = 40;
3
+ /** `value` quoted and cut to {@link MAX_ECHO} units. */
4
+ export declare function quoteForError(value: string): string;
@@ -0,0 +1,6 @@
1
+ /** UTF-16 units of bad input an error message repeats. */
2
+ export const MAX_ECHO = 40;
3
+ /** `value` quoted and cut to {@link MAX_ECHO} units. */
4
+ export function quoteForError(value) {
5
+ return `${JSON.stringify(value.slice(0, MAX_ECHO))}${value.length > MAX_ECHO ? '...' : ''}`;
6
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Floor integer square root via precision doubling.
3
+ * Below 2^52 uses `Math.sqrt` with an exact fix-up; larger n recurse on
4
+ * `n >> 2k` then one Newton step and one +-1 correction.
5
+ */
6
+ export declare function isqrtRoot(n: bigint): {
7
+ root: bigint;
8
+ exact: boolean;
9
+ };
@@ -0,0 +1,34 @@
1
+ import { bitLength } from './bits.js';
2
+ /**
3
+ * Adjust `x` so `x*x <= n < (x+1)*(x+1)` (one +-1 step).
4
+ * Returns the root and whether `x*x === n` (avoids a second full square in callers).
5
+ */
6
+ function isqrtFix(x, n) {
7
+ let y = x;
8
+ let y2 = y * y;
9
+ // Newton / Math.sqrt estimate is within 1 of the true floor root.
10
+ if (y2 > n) {
11
+ y--;
12
+ y2 = y * y;
13
+ }
14
+ else if (y2 + ((y << 1n) + 1n) <= n) {
15
+ y++;
16
+ y2 = y * y;
17
+ }
18
+ return { root: y, exact: y2 === n };
19
+ }
20
+ /**
21
+ * Floor integer square root via precision doubling.
22
+ * Below 2^52 uses `Math.sqrt` with an exact fix-up; larger n recurse on
23
+ * `n >> 2k` then one Newton step and one +-1 correction.
24
+ */
25
+ export function isqrtRoot(n) {
26
+ if (n <= 1n)
27
+ return { root: n, exact: true };
28
+ if (n < 1n << 52n) {
29
+ return isqrtFix(BigInt(Math.floor(Math.sqrt(Number(n)))), n);
30
+ }
31
+ const k = Math.floor(bitLength(n) / 4);
32
+ const r = isqrtRoot(n >> BigInt(2 * k)).root << BigInt(k);
33
+ return isqrtFix((r + n / r) >> 1n, n);
34
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Canonical tags for `locales`; an empty list means the runtime's locale.
3
+ * @throws {TypeError} for an entry that is neither a string nor an `Intl.Locale`.
4
+ * @throws {RangeError} for a list longer than 255 characters, which the engine canonicalizes in
5
+ * quadratic time, or a malformed tag.
6
+ */
7
+ export declare function canonicalLocales(locales: Intl.LocalesArgument): string[];
@@ -0,0 +1,24 @@
1
+ /** Longest locale list, joined, that `Intl` is asked to canonicalize. */
2
+ const MAX_LOCALE_LENGTH = 255;
3
+ /**
4
+ * Canonical tags for `locales`; an empty list means the runtime's locale.
5
+ * @throws {TypeError} for an entry that is neither a string nor an `Intl.Locale`.
6
+ * @throws {RangeError} for a list longer than 255 characters, which the engine canonicalizes in
7
+ * quadratic time, or a malformed tag.
8
+ */
9
+ export function canonicalLocales(locales) {
10
+ if (locales === undefined)
11
+ return [];
12
+ const entries = Array.isArray(locales) ? locales : [locales];
13
+ const tags = entries.map((entry) => {
14
+ if (typeof entry === 'string')
15
+ return entry;
16
+ if (entry instanceof Intl.Locale)
17
+ return entry.toString();
18
+ throw new TypeError('locale must be a string or an Intl.Locale');
19
+ });
20
+ if (tags.join().length > MAX_LOCALE_LENGTH) {
21
+ throw new RangeError(`locale is longer than ${String(MAX_LOCALE_LENGTH)} characters`);
22
+ }
23
+ return Intl.getCanonicalLocales(tags);
24
+ }
@@ -0,0 +1,2 @@
1
+ /** The ISO 4217 minor-unit digits of an upper-case `code`, or undefined when it has none. */
2
+ export declare function isoMinorUnits(code: string): number | undefined;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Active ISO 4217 codes by minor-unit digits, from the ISO 4217 list one published 2026-09-17.
3
+ * Codes the list gives no minor unit (XAU, XDR, XXX and the other funds and metals) are absent.
4
+ * `Intl` cannot stand in: it reports CLDR display digits, which differ from ISO 4217 (0 for IQD
5
+ * and HUF) and change with the runtime's ICU version.
6
+ */
7
+ const CODES_BY_MINOR_UNITS = [
8
+ [0, 'BIF CLP DJF GNF ISK JPY KMF KRW PYG RWF UGX UYI VND VUV XAF XOF XPF'],
9
+ [
10
+ 2,
11
+ 'AED AFN ALL AMD AOA ARS AUD AWG AZN BAM BBD BDT BMD BND BOB BOV BRL BSD BTN BWP BYN ' +
12
+ 'BZD CAD CDF CHE CHF CHW CNY COP COU CRC CUP CVE CZK DKK DOP DZD EGP ERN ETB EUR FJD ' +
13
+ 'FKP GBP GEL GHS GIP GMD GTQ GYD HKD HNL HTG HUF IDR ILS INR IRR JMD KES KGS KHR KPW ' +
14
+ 'KYD KZT LAK LBP LKR LRD LSL MAD MDL MGA MKD MMK MNT MOP MRU MUR MVR MWK MXN MXV MYR ' +
15
+ 'MZN NAD NGN NIO NOK NPR NZD PAB PEN PGK PHP PKR PLN QAR RON RSD RUB SAR SBD SCR SDG ' +
16
+ 'SEK SGD SHP SLE SOS SRD SSP STN SVC SYP SZL THB TJS TMT TOP TRY TTD TWD TZS UAH USD ' +
17
+ 'USN UYU UZS VED VES WST XAD XCD XCG YER ZAR ZMW ZWG'
18
+ ],
19
+ [3, 'BHD IQD JOD KWD LYD OMR TND'],
20
+ [4, 'CLF UYW']
21
+ ];
22
+ let minorUnits;
23
+ /** The ISO 4217 minor-unit digits of an upper-case `code`, or undefined when it has none. */
24
+ export function isoMinorUnits(code) {
25
+ if (minorUnits === undefined) {
26
+ minorUnits = new Map();
27
+ for (const [digits, codes] of CODES_BY_MINOR_UNITS) {
28
+ for (const listed of codes.split(' '))
29
+ minorUnits.set(listed, digits);
30
+ }
31
+ }
32
+ return minorUnits.get(code);
33
+ }