big.esm 1.3.0 → 1.3.2

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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [1.3.2](https://github.com/dschewchenko/big.esm/compare/v1.3.1...v1.3.2) (2025-10-26)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * migrate to bun and fix dependencies ([302f12b](https://github.com/dschewchenko/big.esm/commit/302f12bcf300a0f14f2af970c34b0251e1bbda81))
7
+
8
+ ## [1.3.1](https://github.com/dschewchenko/big.esm/compare/v1.3.0...v1.3.1) (2023-06-25)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * docs update ([cac1876](https://github.com/dschewchenko/big.esm/commit/cac1876a8455ff044dc9bab66524e4bba2359cf1))
14
+
1
15
  # [1.3.0](https://github.com/dschewchenko/big.esm/compare/v1.2.2...v1.3.0) (2023-06-24)
2
16
 
3
17
 
package/README.md CHANGED
@@ -13,17 +13,29 @@ It provides advanced functionality for performing arithmetic operations with pre
13
13
  - Fully written in TypeScript, so it provides type definitions out of the box.
14
14
  - Tree-shaking is supported.
15
15
 
16
- Full bundle size(ESM) — 4.16 kB minified and 1.75 kB gzipped.
17
- `sqrtBig` operation is heavy(more than half of bundle), because it reuses most of the operations from `big.esm`.
16
+ Full bundle size (ESM) — 4.2 kB minified and 1.8 kB gzipped.
17
+ `sqrtBig` operation is heavy (more than half of bundle), because it reuses most of the operations from `big.esm`.
18
18
 
19
19
  ## Installation
20
20
 
21
- Install `big.esm` using npm:
22
-
21
+ Install `big.esm` using Bun:
22
+
23
+ ```bash
24
+ bun add big.esm
25
+ ```
26
+
27
+ or npm:
28
+
23
29
  ```bash
24
30
  npm install big.esm
25
31
  ```
26
32
 
33
+ or yarn:
34
+
35
+ ```bash
36
+ yarn add big.esm
37
+ ```
38
+
27
39
  ## Usage
28
40
 
29
41
  ```js
@@ -37,12 +49,12 @@ console.log(result.toString()); // 22222222121.1111111101
37
49
 
38
50
  ## Compatibility
39
51
 
40
- `big.esm` is compatible with all modern browsers and Node.js 10+. It uses `BigInt` internally, so it is not compatible with older browsers and Node.js versions. On info from [caniuse.com](https://caniuse.com/#feat=bigint) `BigInt` is supported by 95.25% of all browsers(as of 2023-05-12).
52
+ `big.esm` is compatible with all modern browsers, Bun 1.0+, and Node.js 16+. It uses `BigInt` internally, so it is not compatible with older runtimes. On info from [caniuse.com](https://caniuse.com/#feat=bigint) `BigInt` is supported by 96.47% of all browsers(as of 2023-06-25).
41
53
 
42
54
  ## Notes
43
55
 
44
56
  - `big.esm` is not a drop-in replacement for `big.js`. It does not support the same API and does not have the same functionality. It is a completely different library.
45
- - `powBig()` and `sqrtBig()` are heavy and slow operations. They are implemented using the Newton's method and exponentiation by squaring algorithms. Use them with caution, especially with large numbers. In most cases, there will be no problems.
57
+ - `sqrtBig()` is heavy and slow operation. It's implemented using the Newton's method with nth root calculation. In most cases, there will be no problems.
46
58
 
47
59
  ## API
48
60
 
@@ -76,6 +88,8 @@ Checks if the value is a valid numeric value for creating a `Big` instance.
76
88
 
77
89
  In math operations `mutable` option is used to specify whether to mutate the first argument or create a new instance. By default, a new instance is created.
78
90
 
91
+ **Note:** In high-load applications, it is recommended to use the `mutable` option to reduce memory usage and improve performance.
92
+
79
93
  #### `addBig(a: Big, b: Big, mutable = false): Big`
80
94
 
81
95
  Adds two `Big` instances.
@@ -112,9 +126,17 @@ Returns the absolute value of `a`.
112
126
 
113
127
  Compares two `Big` instances. Returns `-1` if `a < b`, `0` if `a == b` and `1` if `a > b`.
114
128
 
129
+ #### `minBig(a: Big, b: Big, mutable = false): Big`
130
+
131
+ Returns the minimum of two `Big` instances. If the numbers are equal, returns the first number.
132
+
133
+ #### `maxBig(a: Big, b: Big, mutable = false): Big`
134
+
135
+ Returns the maximum of two `Big` instances. If the numbers are equal, returns the first number.
136
+
115
137
  ## Benchmark
116
138
 
117
- Benchmark results are available [here](https://dschewchenko.github.io/big.esm/benchmark.html).
139
+ Benchmark results are available [here](https://github.com/dschewchenko/big.esm/blob/master/BENCHMARK.md).
118
140
 
119
141
  ## Roadmap
120
142
 
package/dist/big.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { BigObject, PossibleNumber } from "./types";
1
+ import { BigObject, PossibleNumber } from './types';
2
2
  /**
3
3
  * The Big class for working with large numbers and fractions using BigInt.
4
4
  *
package/dist/big.esm.js CHANGED
@@ -1,37 +1,40 @@
1
- var x = Object.defineProperty;
2
- var A = (e, t, n) => t in e ? x(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var I = (e, t, n) => (A(e, typeof t != "symbol" ? t + "" : t, n), n);
4
- const M = (e) => typeof e == "bigint" || typeof e == "number" || typeof e == "string" && !isNaN(Number(e)), _ = /0+$/, R = (e) => e.replace(_, "");
5
1
  const m = BigInt(0), y = BigInt(1), p = BigInt(10);
6
2
  function o(e, t) {
7
3
  return e instanceof w ? e : new w(e, t);
8
4
  }
9
- function j(e, t = !1, n = !0) {
5
+ function x(e, t = !1, n = !0) {
10
6
  const r = e.toString();
11
- if (!Number.isFinite(Number(r)))
12
- throw new Error("Invalid number");
13
- if (Number(r) === 0)
14
- return n ? o(m) : { value: m, scale: 0 };
15
- let [s, i] = r.split("e");
16
- const g = s.startsWith("-") ? "-" : "";
17
- s = g ? s.slice(1) : s;
18
- let [u, l = ""] = s.split(".");
7
+ if (!Number.isFinite(Number(r))) throw new Error("Invalid number");
8
+ if (Number(r) === 0) return n ? o(m) : { value: m, scale: 0 };
9
+ let [s, a] = r.split("e");
10
+ const g = a ?? "0", h = s.startsWith("-") ? "-" : "";
11
+ s = h ? s.slice(1) : s;
12
+ let [i, l = ""] = s.split(".");
19
13
  l = t ? "" : l;
20
- const a = Number.parseInt(i);
21
- if (a > 0) {
22
- const f = Math.min(a, l.length);
23
- u += l.slice(0, f), l = l.slice(f), f < a && (u += "0".repeat(a - f));
24
- } else if (a < 0) {
25
- const f = Math.min(-a, u.length);
26
- l = u.slice(-f) + l, u = u.slice(0, u.length - f), f < -a && (l = "0".repeat(-a - f) + l);
14
+ const u = Number.parseInt(g, 10);
15
+ if (u > 0) {
16
+ const c = Math.min(u, l.length);
17
+ i += l.slice(0, c), l = l.slice(c), c < u && (i += "0".repeat(u - c));
18
+ } else if (u < 0) {
19
+ const c = Math.min(-u, i.length);
20
+ l = i.slice(-c) + l, i = i.slice(0, i.length - c), c < -u && (l = "0".repeat(-u - c) + l);
27
21
  }
28
- const h = BigInt(g + u + l), c = l.length;
29
- return n ? o(h, c) : { value: h, scale: c };
22
+ const f = BigInt(h + i + l), v = l.length;
23
+ return n ? o(f, v) : { value: f, scale: v };
30
24
  }
31
- function P(e) {
25
+ function D(e) {
32
26
  return typeof e != "object" || e === null ? !1 : "value" in e && "scale" in e && typeof e.value == "bigint" && typeof e.scale == "number";
33
27
  }
28
+ const A = (e) => typeof e == "bigint" || typeof e == "number" || typeof e == "string" && !Number.isNaN(Number(e)), M = /0+$/, _ = (e) => e.replace(M, "");
34
29
  class w {
30
+ /**
31
+ * The value of the Big instance.
32
+ */
33
+ value;
34
+ /**
35
+ * The scale of the Big instance, representing the number of decimal places.
36
+ */
37
+ scale;
35
38
  /**
36
39
  * Implements the overloaded constructors for the Big class.
37
40
  * @param {BigValue} value - The value to create the Big instance from. Can be a number, string, BigInt, or another Big instance.
@@ -40,23 +43,15 @@ class w {
40
43
  * @return {Big} - A new Big instance.
41
44
  */
42
45
  constructor(t, n) {
43
- /**
44
- * The value of the Big instance.
45
- */
46
- I(this, "value");
47
- /**
48
- * The scale of the Big instance, representing the number of decimal places.
49
- */
50
- I(this, "scale");
51
- if (P(t))
46
+ if (D(t))
52
47
  this.value = t.value, this.scale = t.scale;
53
- else if (M(t)) {
48
+ else if (A(t)) {
54
49
  let r = n !== void 0;
55
- if (r && (typeof n != "number" && (n = Number(n)), r = !isNaN(n), n = r ? Math.floor(n) : 0), typeof t == "bigint")
50
+ if (r && (typeof n != "number" && (n = Number(n)), r = !Number.isNaN(n), n = r ? Math.floor(n) : 0), typeof t == "bigint")
56
51
  this.value = t, this.scale = r ? n : 0;
57
52
  else {
58
- const { value: s, scale: i } = j(t, r, !1);
59
- this.value = s, this.scale = r ? n : i;
53
+ const { value: s, scale: a } = x(t, r, !1);
54
+ this.value = s, this.scale = r ? n : a;
60
55
  }
61
56
  } else
62
57
  throw new TypeError("Invalid type provided to Big constructor.");
@@ -81,8 +76,8 @@ class w {
81
76
  r && (n = n.substring(1));
82
77
  let s = "";
83
78
  if (this.scale > m) {
84
- const i = n.length > this.scale ? n.length - Number(this.scale) : 0;
85
- s = n.slice(i).padStart(Number(this.scale), "0"), t && (s = R(s)), s.length > 0 && (s = `.${s}`), n = n.slice(0, i);
79
+ const a = n.length > this.scale ? n.length - Number(this.scale) : 0;
80
+ s = n.slice(a).padStart(Number(this.scale), "0"), t && (s = _(s)), s.length > 0 && (s = `.${s}`), n = n.slice(0, a);
86
81
  }
87
82
  return `${r}${n || "0"}${s}`;
88
83
  }
@@ -95,6 +90,10 @@ class w {
95
90
  return this.toString();
96
91
  }
97
92
  }
93
+ function R(e, t = !1) {
94
+ const n = e.value < m ? -e.value : e.value;
95
+ return t ? (e.value = n, e) : o(n, e.scale);
96
+ }
98
97
  function S(e, t, n = !1) {
99
98
  const r = e.scale - t.scale;
100
99
  if (r === 0)
@@ -102,106 +101,98 @@ function S(e, t, n = !1) {
102
101
  const s = p ** BigInt(Math.abs(r));
103
102
  if (n)
104
103
  return r > 0 ? (t.value *= s, t.scale = e.scale) : (e.value *= s, e.scale = t.scale), [e, t];
105
- const i = r > 0 ? e : o(e.value * s, t.scale), g = r < 0 ? t : o(t.value * s, e.scale);
106
- return [i, g];
104
+ const a = r > 0 ? e : o(e.value * s, t.scale), g = r < 0 ? t : o(t.value * s, e.scale);
105
+ return [a, g];
107
106
  }
108
107
  function O(e, t, n = !1) {
109
- const [r, s] = S(e, t), i = r.value + s.value;
110
- return n ? (e.value = i, e.scale = r.scale, e) : o(i, r.scale);
108
+ const [r, s] = S(e, t), a = r.value + s.value;
109
+ return n ? (e.value = a, e.scale = r.scale, e) : o(a, r.scale);
111
110
  }
112
- function N(e, t, n = !1) {
113
- const [r, s] = S(e, t, n), i = r.value - s.value;
114
- return n ? (e.value = i, e) : o(i, r.scale);
115
- }
116
- function E(e, t, n = !1) {
117
- const r = e.value * t.value, s = e.scale + t.scale;
118
- return n ? (e.value = r, e.scale = s, e) : o(r, s);
119
- }
120
- const d = (e) => e.value === m;
111
+ const d = (e, t) => {
112
+ const [n, r] = S(e, t);
113
+ return n.value > r.value ? 1 : n.value < r.value ? -1 : 0;
114
+ }, T = (e) => e.value === m;
121
115
  function B(e, t, n = 20, r = "half-up", s = !1) {
122
- if (d(t))
116
+ if (T(t))
123
117
  throw new Error("Division by zero is not allowed.");
124
- const i = Math.max(e.scale, t.scale), g = Math.max(0, n), u = e.value * p ** BigInt(i + g - e.scale), l = t.value * p ** BigInt(i - t.scale), a = u / l, h = u % l;
125
- let c;
118
+ const a = Math.max(e.scale, t.scale), g = Math.max(0, n), h = e.value * p ** BigInt(a + g - e.scale), i = t.value * p ** BigInt(a - t.scale), l = h / i, u = h % i;
119
+ let f = l;
126
120
  switch (r) {
127
121
  case "down":
128
- c = a;
129
122
  break;
130
123
  case "up":
131
- c = h !== m ? a + y : a + m;
124
+ f = u !== m ? l + y : l + m;
132
125
  break;
133
- case "half-up":
134
126
  default:
135
- c = h * BigInt(2) >= l ? a + y : a;
127
+ f = u * BigInt(2) >= i ? l + y : l;
136
128
  break;
137
129
  }
138
- return s ? (e.value = c, e.scale = g, e) : o(c, g);
130
+ return s ? (e.value = f, e.scale = g, e) : o(f, g);
131
+ }
132
+ const I = (e) => new w(e.value, e.scale), P = (e, t, n = !1) => d(e, t) >= 0 ? n ? e : I(e) : n ? t : I(t), $ = (e, t, n = !1) => d(e, t) > 0 ? n ? t : I(t) : n ? e : I(e);
133
+ function N(e, t, n = !1) {
134
+ const r = e.value * t.value, s = e.scale + t.scale;
135
+ return n ? (e.value = r, e.scale = s, e) : o(r, s);
136
+ }
137
+ function E(e, t, n = !1) {
138
+ const [r, s] = S(e, t, n), a = r.value - s.value;
139
+ return n ? (e.value = a, e) : o(a, r.scale);
139
140
  }
140
- function V(e, t, n = !1) {
141
- if (d(t))
141
+ function F(e, t, n = !1) {
142
+ if (T(t))
142
143
  throw new Error("Division by zero is not allowed.");
143
144
  const r = B(e, t, 0, "down");
144
145
  if (n)
145
- return E(r, t, !0), N(e, r, !0);
146
- const s = E(r, t);
147
- return N(e, s);
146
+ return N(r, t, !0), E(e, r, !0);
147
+ const s = N(r, t);
148
+ return E(e, s);
148
149
  }
149
- function $(e, t = 2, n = !1) {
150
+ function j(e, t = 2, n = !1) {
150
151
  if (!Number.isInteger(t) || t < 0)
151
152
  throw new Error("The exponent must be a non-negative integer.");
152
153
  const r = e.value ** BigInt(t), s = e.scale * t;
153
154
  return n ? (e.value = r, e.scale = s, e) : o(r, s);
154
155
  }
155
- const T = (e, t) => {
156
- const [n, r] = S(e, t);
157
- return n.value > r.value ? 1 : n.value < r.value ? -1 : 0;
158
- };
159
- function F(e, t = !1) {
160
- const n = e.value < m ? -e.value : e.value;
161
- return t ? (e.value = n, e) : o(n, e.scale);
162
- }
163
- function Z(e, t = 2, n = 20, r = !1) {
164
- if (d(e))
165
- return o(0);
166
- if (e.valueOf() === BigInt(1))
167
- return o(1);
156
+ function G(e, t = 2, n = 20, r = !1) {
157
+ if (T(e)) return o(0);
158
+ if (e.valueOf() === BigInt(1)) return o(1);
168
159
  if (e.value < m && t % 2 === 0)
169
160
  throw new Error("The root must be an odd integer when the number is negative.");
170
- if (!Number.isInteger(t) || t < 2)
171
- throw new Error("The root must be an integer and greater than 1");
172
- if (n < 0)
173
- throw new Error("The precision must be a non-negative integer.");
174
- const s = o(n === 0 ? "1" : "0." + "0".repeat(n - 1) + "1"), i = o(t), g = o(t - 1), u = o(2);
175
- let l = B(O(e, B(e, u, n)), u, n), a;
161
+ if (!Number.isInteger(t) || t < 2) throw new Error("The root must be an integer and greater than 1");
162
+ if (n < 0) throw new Error("The precision must be a non-negative integer.");
163
+ const s = o(n === 0 ? "1" : `0.${"0".repeat(n - 1)}1`), a = o(t), g = o(t - 1), h = o(2);
164
+ let i = B(O(e, B(e, h, n)), h, n), l;
176
165
  do {
177
- a = l;
178
- const D = O(E(g, l), B(e, $(l, t - 1), n));
179
- l = B(D, i, n);
180
- } while (T(F(N(a, l)), s) === 1);
181
- const h = l.toString();
166
+ l = i;
167
+ const c = O(
168
+ N(g, i),
169
+ B(e, j(i, t - 1), n)
170
+ );
171
+ i = B(c, a, n);
172
+ } while (d(R(E(l, i)), s) === 1);
173
+ const u = i.toString();
182
174
  if (r)
183
- return e.value = l.value, e.scale = n, e;
184
- const c = h.indexOf("."), f = c !== -1 ? h.slice(0, c + n + 1) : h;
185
- return o(f);
175
+ return e.value = i.value, e.scale = n, e;
176
+ const f = u.indexOf("."), v = f !== -1 ? u.slice(0, f + n + 1) : u;
177
+ return o(v);
186
178
  }
187
- const v = (e) => new w(e.value, e.scale), k = (e, t, n = !1) => T(e, t) > 0 ? n ? t : v(t) : n ? e : v(e), q = (e, t, n = !1) => T(e, t) >= 0 ? n ? e : v(e) : n ? t : v(t);
188
179
  export {
189
180
  w as Big,
190
- F as absBig,
181
+ R as absBig,
191
182
  O as addBig,
192
183
  S as alignScale,
193
- v as cloneBig,
194
- T as compareBig,
184
+ I as cloneBig,
185
+ d as compareBig,
195
186
  o as createBig,
196
187
  B as divBig,
197
- j as fromString,
198
- P as isBigObject,
199
- M as isNumericValue,
200
- q as maxBig,
201
- k as minBig,
202
- V as modBig,
203
- E as mulBig,
204
- $ as powBig,
205
- Z as sqrtBig,
206
- N as subBig
188
+ x as fromString,
189
+ D as isBigObject,
190
+ A as isNumericValue,
191
+ P as maxBig,
192
+ $ as minBig,
193
+ F as modBig,
194
+ N as mulBig,
195
+ j as powBig,
196
+ G as sqrtBig,
197
+ E as subBig
207
198
  };
@@ -1 +1 @@
1
- (function(r,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(r=typeof globalThis<"u"?globalThis:r||self,o((r.big=r.big||{},r.big.esm={})))})(this,function(r){"use strict";var Z=Object.defineProperty;var k=(r,o,d)=>o in r?Z(r,o,{enumerable:!0,configurable:!0,writable:!0,value:d}):r[o]=d;var D=(r,o,d)=>(k(r,typeof o!="symbol"?o+"":o,d),d);const o=e=>typeof e=="bigint"||typeof e=="number"||typeof e=="string"&&!isNaN(Number(e)),d=/0+$/,P=e=>e.replace(d,""),v=BigInt(0),M=BigInt(1),T=BigInt(10);function u(e,t){return e instanceof N?e:new N(e,t)}function A(e,t=!1,n=!0){const i=e.toString();if(!Number.isFinite(Number(i)))throw new Error("Invalid number");if(Number(i)===0)return n?u(v):{value:v,scale:0};let[s,a]=i.split("e");const h=s.startsWith("-")?"-":"";s=h?s.slice(1):s;let[f,l=""]=s.split(".");l=t?"":l;const c=Number.parseInt(a);if(c>0){const B=Math.min(c,l.length);f+=l.slice(0,B),l=l.slice(B),B<c&&(f+="0".repeat(c-B))}else if(c<0){const B=Math.min(-c,f.length);l=f.slice(-B)+l,f=f.slice(0,f.length-B),B<-c&&(l="0".repeat(-c-B)+l)}const m=BigInt(h+f+l),g=l.length;return n?u(m,g):{value:m,scale:g}}function _(e){return typeof e!="object"||e===null?!1:"value"in e&&"scale"in e&&typeof e.value=="bigint"&&typeof e.scale=="number"}class N{constructor(t,n){D(this,"value");D(this,"scale");if(_(t))this.value=t.value,this.scale=t.scale;else if(o(t)){let i=n!==void 0;if(i&&(typeof n!="number"&&(n=Number(n)),i=!isNaN(n),n=i?Math.floor(n):0),typeof t=="bigint")this.value=t,this.scale=i?n:0;else{const{value:s,scale:a}=A(t,i,!1);this.value=s,this.scale=i?n:a}}else throw new TypeError("Invalid type provided to Big constructor.")}valueOf(){return this.value}toString(t=!0){let n=this.value.toString();const i=n.startsWith("-")?"-":"";i&&(n=n.substring(1));let s="";if(this.scale>v){const a=n.length>this.scale?n.length-Number(this.scale):0;s=n.slice(a).padStart(Number(this.scale),"0"),t&&(s=P(s)),s.length>0&&(s=`.${s}`),n=n.slice(0,a)}return`${i}${n||"0"}${s}`}toJSON(){return this.toString()}}function S(e,t,n=!1){const i=e.scale-t.scale;if(i===0)return[e,t];const s=T**BigInt(Math.abs(i));if(n)return i>0?(t.value*=s,t.scale=e.scale):(e.value*=s,e.scale=t.scale),[e,t];const a=i>0?e:u(e.value*s,t.scale),h=i<0?t:u(t.value*s,e.scale);return[a,h]}function O(e,t,n=!1){const[i,s]=S(e,t),a=i.value+s.value;return n?(e.value=a,e.scale=i.scale,e):u(a,i.scale)}function E(e,t,n=!1){const[i,s]=S(e,t,n),a=i.value-s.value;return n?(e.value=a,e):u(a,i.scale)}function p(e,t,n=!1){const i=e.value*t.value,s=e.scale+t.scale;return n?(e.value=i,e.scale=s,e):u(i,s)}const j=e=>e.value===v;function I(e,t,n=20,i="half-up",s=!1){if(j(t))throw new Error("Division by zero is not allowed.");const a=Math.max(e.scale,t.scale),h=Math.max(0,n),f=e.value*T**BigInt(a+h-e.scale),l=t.value*T**BigInt(a-t.scale),c=f/l,m=f%l;let g;switch(i){case"down":g=c;break;case"up":g=m!==v?c+M:c+v;break;case"half-up":default:g=m*BigInt(2)>=l?c+M:c;break}return s?(e.value=g,e.scale=h,e):u(g,h)}function V(e,t,n=!1){if(j(t))throw new Error("Division by zero is not allowed.");const i=I(e,t,0,"down");if(n)return p(i,t,!0),E(e,i,!0);const s=p(i,t);return E(e,s)}function b(e,t=2,n=!1){if(!Number.isInteger(t)||t<0)throw new Error("The exponent must be a non-negative integer.");const i=e.value**BigInt(t),s=e.scale*t;return n?(e.value=i,e.scale=s,e):u(i,s)}const y=(e,t)=>{const[n,i]=S(e,t);return n.value>i.value?1:n.value<i.value?-1:0};function R(e,t=!1){const n=e.value<v?-e.value:e.value;return t?(e.value=n,e):u(n,e.scale)}function $(e,t=2,n=20,i=!1){if(j(e))return u(0);if(e.valueOf()===BigInt(1))return u(1);if(e.value<v&&t%2===0)throw new Error("The root must be an odd integer when the number is negative.");if(!Number.isInteger(t)||t<2)throw new Error("The root must be an integer and greater than 1");if(n<0)throw new Error("The precision must be a non-negative integer.");const s=u(n===0?"1":"0."+"0".repeat(n-1)+"1"),a=u(t),h=u(t-1),f=u(2);let l=I(O(e,I(e,f,n)),f,n),c;do{c=l;const G=O(p(h,l),I(e,b(l,t-1),n));l=I(G,a,n)}while(y(R(E(c,l)),s)===1);const m=l.toString();if(i)return e.value=l.value,e.scale=n,e;const g=m.indexOf("."),B=g!==-1?m.slice(0,g+n+1):m;return u(B)}const w=e=>new N(e.value,e.scale),q=(e,t,n=!1)=>y(e,t)>0?n?t:w(t):n?e:w(e),F=(e,t,n=!1)=>y(e,t)>=0?n?e:w(e):n?t:w(t);r.Big=N,r.absBig=R,r.addBig=O,r.alignScale=S,r.cloneBig=w,r.compareBig=y,r.createBig=u,r.divBig=I,r.fromString=A,r.isBigObject=_,r.isNumericValue=o,r.maxBig=F,r.minBig=q,r.modBig=V,r.mulBig=p,r.powBig=b,r.sqrtBig=$,r.subBig=E,Object.defineProperty(r,Symbol.toStringTag,{value:"Module"})});
1
+ (function(s,I){typeof exports=="object"&&typeof module<"u"?I(exports):typeof define=="function"&&define.amd?define(["exports"],I):(s=typeof globalThis<"u"?globalThis:s||self,I((s.big=s.big||{},s.big.esm={})))})(this,(function(s){"use strict";const m=BigInt(0),j=BigInt(1),T=BigInt(10);function c(e,t){return e instanceof w?e:new w(e,t)}function D(e,t=!1,n=!0){const i=e.toString();if(!Number.isFinite(Number(i)))throw new Error("Invalid number");if(Number(i)===0)return n?c(m):{value:m,scale:0};let[r,a]=i.split("e");const B=a??"0",h=r.startsWith("-")?"-":"";r=h?r.slice(1):r;let[u,l=""]=r.split(".");l=t?"":l;const o=Number.parseInt(B,10);if(o>0){const f=Math.min(o,l.length);u+=l.slice(0,f),l=l.slice(f),f<o&&(u+="0".repeat(o-f))}else if(o<0){const f=Math.min(-o,u.length);l=u.slice(-f)+l,u=u.slice(0,u.length-f),f<-o&&(l="0".repeat(-o-f)+l)}const g=BigInt(h+u+l),y=l.length;return n?c(g,y):{value:g,scale:y}}function M(e){return typeof e!="object"||e===null?!1:"value"in e&&"scale"in e&&typeof e.value=="bigint"&&typeof e.scale=="number"}const A=e=>typeof e=="bigint"||typeof e=="number"||typeof e=="string"&&!Number.isNaN(Number(e)),R=/0+$/,$=e=>e.replace(R,"");class w{value;scale;constructor(t,n){if(M(t))this.value=t.value,this.scale=t.scale;else if(A(t)){let i=n!==void 0;if(i&&(typeof n!="number"&&(n=Number(n)),i=!Number.isNaN(n),n=i?Math.floor(n):0),typeof t=="bigint")this.value=t,this.scale=i?n:0;else{const{value:r,scale:a}=D(t,i,!1);this.value=r,this.scale=i?n:a}}else throw new TypeError("Invalid type provided to Big constructor.")}valueOf(){return this.value}toString(t=!0){let n=this.value.toString();const i=n.startsWith("-")?"-":"";i&&(n=n.substring(1));let r="";if(this.scale>m){const a=n.length>this.scale?n.length-Number(this.scale):0;r=n.slice(a).padStart(Number(this.scale),"0"),t&&(r=$(r)),r.length>0&&(r=`.${r}`),n=n.slice(0,a)}return`${i}${n||"0"}${r}`}toJSON(){return this.toString()}}function _(e,t=!1){const n=e.value<m?-e.value:e.value;return t?(e.value=n,e):c(n,e.scale)}function N(e,t,n=!1){const i=e.scale-t.scale;if(i===0)return[e,t];const r=T**BigInt(Math.abs(i));if(n)return i>0?(t.value*=r,t.scale=e.scale):(e.value*=r,e.scale=t.scale),[e,t];const a=i>0?e:c(e.value*r,t.scale),B=i<0?t:c(t.value*r,e.scale);return[a,B]}function O(e,t,n=!1){const[i,r]=N(e,t),a=i.value+r.value;return n?(e.value=a,e.scale=i.scale,e):c(a,i.scale)}const S=(e,t)=>{const[n,i]=N(e,t);return n.value>i.value?1:n.value<i.value?-1:0},b=e=>e.value===m;function v(e,t,n=20,i="half-up",r=!1){if(b(t))throw new Error("Division by zero is not allowed.");const a=Math.max(e.scale,t.scale),B=Math.max(0,n),h=e.value*T**BigInt(a+B-e.scale),u=t.value*T**BigInt(a-t.scale),l=h/u,o=h%u;let g=l;switch(i){case"down":break;case"up":g=o!==m?l+j:l+m;break;default:g=o*BigInt(2)>=u?l+j:l;break}return r?(e.value=g,e.scale=B,e):c(g,B)}const d=e=>new w(e.value,e.scale),V=(e,t,n=!1)=>S(e,t)>=0?n?e:d(e):n?t:d(t),q=(e,t,n=!1)=>S(e,t)>0?n?t:d(t):n?e:d(e);function E(e,t,n=!1){const i=e.value*t.value,r=e.scale+t.scale;return n?(e.value=i,e.scale=r,e):c(i,r)}function p(e,t,n=!1){const[i,r]=N(e,t,n),a=i.value-r.value;return n?(e.value=a,e):c(a,i.scale)}function F(e,t,n=!1){if(b(t))throw new Error("Division by zero is not allowed.");const i=v(e,t,0,"down");if(n)return E(i,t,!0),p(e,i,!0);const r=E(i,t);return p(e,r)}function P(e,t=2,n=!1){if(!Number.isInteger(t)||t<0)throw new Error("The exponent must be a non-negative integer.");const i=e.value**BigInt(t),r=e.scale*t;return n?(e.value=i,e.scale=r,e):c(i,r)}function G(e,t=2,n=20,i=!1){if(b(e))return c(0);if(e.valueOf()===BigInt(1))return c(1);if(e.value<m&&t%2===0)throw new Error("The root must be an odd integer when the number is negative.");if(!Number.isInteger(t)||t<2)throw new Error("The root must be an integer and greater than 1");if(n<0)throw new Error("The precision must be a non-negative integer.");const r=c(n===0?"1":`0.${"0".repeat(n-1)}1`),a=c(t),B=c(t-1),h=c(2);let u=v(O(e,v(e,h,n)),h,n),l;do{l=u;const f=O(E(B,u),v(e,P(u,t-1),n));u=v(f,a,n)}while(S(_(p(l,u)),r)===1);const o=u.toString();if(i)return e.value=u.value,e.scale=n,e;const g=o.indexOf("."),y=g!==-1?o.slice(0,g+n+1):o;return c(y)}s.Big=w,s.absBig=_,s.addBig=O,s.alignScale=N,s.cloneBig=d,s.compareBig=S,s.createBig=c,s.divBig=v,s.fromString=D,s.isBigObject=M,s.isNumericValue=A,s.maxBig=V,s.minBig=q,s.modBig=F,s.mulBig=E,s.powBig=P,s.sqrtBig=G,s.subBig=p,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));
package/dist/index.d.ts CHANGED
@@ -1,19 +1,19 @@
1
- export { Big } from "./big";
2
- export type { BigValue, PossibleNumber, RoundingMode, CompareResult } from "./types";
3
- export { addBig } from "./operations/add";
4
- export { subBig } from "./operations/sub";
5
- export { mulBig } from "./operations/mul";
6
- export { divBig } from "./operations/div";
7
- export { modBig } from "./operations/mod";
8
- export { powBig } from "./operations/pow";
9
- export { sqrtBig } from "./operations/sqrt";
10
- export { absBig } from "./operations/abs";
11
- export { compareBig } from "./operations/compare";
12
- export { minBig } from "./operations/min";
13
- export { maxBig } from "./operations/max";
14
- export { isBigObject } from "./utils/is-big-object";
15
- export { isNumericValue } from "./utils/numeric";
16
- export { alignScale } from "./utils/align-scale";
17
- export { createBig } from "./utils/create";
18
- export { cloneBig } from "./utils/clone";
19
- export { fromString } from "./utils/from-string";
1
+ export { Big } from './big';
2
+ export { absBig } from './operations/abs';
3
+ export { addBig } from './operations/add';
4
+ export { compareBig } from './operations/compare';
5
+ export { divBig } from './operations/div';
6
+ export { maxBig } from './operations/max';
7
+ export { minBig } from './operations/min';
8
+ export { modBig } from './operations/mod';
9
+ export { mulBig } from './operations/mul';
10
+ export { powBig } from './operations/pow';
11
+ export { sqrtBig } from './operations/sqrt';
12
+ export { subBig } from './operations/sub';
13
+ export type { BigValue, CompareResult, PossibleNumber, RoundingMode } from './types';
14
+ export { alignScale } from './utils/align-scale';
15
+ export { cloneBig } from './utils/clone';
16
+ export { createBig } from './utils/create';
17
+ export { fromString } from './utils/from-string';
18
+ export { isBigObject } from './utils/is-big-object';
19
+ export { isNumericValue } from './utils/numeric';
@@ -1,4 +1,4 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
3
  * Returns the absolute value of a Big instance.
4
4
  *
@@ -1,4 +1,4 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
3
  * Adds two big values.
4
4
  *
@@ -1,5 +1,5 @@
1
- import type { Big } from "../big";
2
- import type { CompareResult } from "../types";
1
+ import { Big } from '../big';
2
+ import { CompareResult } from '../types';
3
3
  /**
4
4
  * Compares two Big numbers and returns a CompareResult indicating the relation between them.
5
5
  *
@@ -1,5 +1,5 @@
1
- import type { RoundingMode } from "../types";
2
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
+ import { RoundingMode } from '../types';
3
3
  /**
4
4
  * Divides two Big instances and returns the result as a new Big instance.
5
5
  *
@@ -1,4 +1,4 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
3
  * Returns the maximum of two Big numbers.
4
4
  *
@@ -1,4 +1,4 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
3
  * Returns the minimum of two Big numbers.
4
4
  *
@@ -1,4 +1,4 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
3
  * Performs the modulo operation (remainder of division) for two Big instances.
4
4
  *
@@ -1,4 +1,4 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
3
  * Multiplies two Big instances and returns the result as a new Big instance.
4
4
  *
@@ -1,4 +1,4 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
3
  * Raises a Big number to the power of another Big number.
4
4
  *
@@ -1,4 +1,4 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
3
  * Calculates the nth root of a Big instance with custom precision and rounding mode.
4
4
  *
@@ -1,4 +1,4 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
3
  * Subtracts two big values and returns the result as a new Big instance.
4
4
  *
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Big } from "./big";
1
+ import { Big } from './big';
2
2
  /**
3
3
  * The object type for the Big class.
4
4
  *
@@ -1,4 +1,4 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
3
  * Aligns the scale(decimal places) of two Big instances before performing arithmetic operations.
4
4
  * It takes two Big instances as input and returns a tuple (array) of two new Big instances with the same scale.
@@ -1,4 +1,4 @@
1
- import { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
3
  * Clone a Big instance
4
4
  * @param {Big} big - The Big instance to clone.
@@ -1,5 +1,5 @@
1
- import { Big } from "../big";
2
- import type { BigObject, PossibleNumber } from "../types";
1
+ import { Big } from '../big';
2
+ import { BigObject, PossibleNumber } from '../types';
3
3
  /**
4
4
  * Creates a new Big instance from the given value.
5
5
  * If the value is already a Big instance, it returns the value itself.
@@ -1,5 +1,5 @@
1
- import { Big } from "../big";
2
- import { BigObject } from "../types";
1
+ import { Big } from '../big';
2
+ import { BigObject } from '../types';
3
3
  /**
4
4
  * Convert a number or string to normal string number. Supports scientific notation.
5
5
  *
@@ -1,5 +1,5 @@
1
- import type { BigObject } from "../types";
2
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
+ import { BigObject } from '../types';
3
3
  /**
4
4
  * Determines if the value is a BigObject. A BigObject is an object with a value property of type bigint and a scale property of type number. It usually represents a Big instance.
5
5
  *
@@ -1,4 +1,4 @@
1
- import type { Big } from "../big";
1
+ import { Big } from '../big';
2
2
  /**
3
3
  * Checks if a Big instance is zero.
4
4
  *
package/package.json CHANGED
@@ -1,22 +1,22 @@
1
1
  {
2
2
  "name": "big.esm",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "A small, fast JavaScript library for arbitrary-precision decimal arithmetic.",
5
5
  "type": "module",
6
6
  "scripts": {
7
- "dev": "vite",
8
- "build": "tsc && vite build",
9
- "preview": "vite preview",
10
- "test": "vitest",
11
- "test:ui": "vitest --ui",
12
- "test:run": "vitest run",
13
- "coverage": "vitest run --coverage",
14
- "docs": "typedoc --out docs",
15
- "lint": "eslint --ext .ts,.js,.json ./src",
16
- "lint:fix": "eslint --fix --ext .ts,.js,.json ./src",
17
- "benchmark": "node --expose-gc ./benchmark.js",
18
- "semantic-release": "semantic-release",
19
- "prepare": "is-ci || husky install"
7
+ "dev": "bunx vite",
8
+ "build": "bunx tsc && bunx vite build",
9
+ "preview": "bunx vite preview",
10
+ "test": "bunx vitest",
11
+ "test:ui": "bunx vitest --ui",
12
+ "test:run": "bunx vitest run",
13
+ "coverage": "bunx vitest run --coverage",
14
+ "docs": "bunx typedoc --out docs",
15
+ "lint": "bunx biome check",
16
+ "lint:fix": "bun run lint -- --write",
17
+ "benchmark": "bun benchmark.js",
18
+ "semantic-release": "bunx semantic-release",
19
+ "prepare": "bunx is-ci || bunx husky"
20
20
  },
21
21
  "main": "dist/big.esm.umd.cjs",
22
22
  "module": "dist/big.esm.js",
@@ -46,35 +46,32 @@
46
46
  "url": "https://github.com/dschewchenko/big.esm/issues"
47
47
  },
48
48
  "homepage": "https://github.com/dschewchenko/big.esm#readme",
49
+ "packageManager": "bun@1.3.1",
49
50
  "engines": {
51
+ "bun": ">=1.0.0",
50
52
  "node": ">=16"
51
53
  },
52
54
  "devDependencies": {
53
- "@commitlint/cli": "^17.6.3",
54
- "@commitlint/config-conventional": "^17.6.3",
55
+ "@biomejs/biome": "2.3.0",
56
+ "@commitlint/cli": "^20.1.0",
57
+ "@commitlint/config-conventional": "^20.0.0",
55
58
  "@semantic-release/changelog": "^6.0.3",
56
59
  "@semantic-release/git": "^10.0.1",
57
- "@types/jest": "^29.5.1",
58
- "@types/node": "^18.15.3",
59
- "@typescript-eslint/eslint-plugin": "^5.59.5",
60
- "@typescript-eslint/parser": "^5.59.5",
61
- "@vitest/coverage-c8": "^0.31.0",
62
- "@vitest/ui": "^0.31.0",
63
- "big.js": "^6.2.1",
64
- "eslint": "^8.40.0",
65
- "eslint-config-prettier": "^8.8.0",
66
- "eslint-plugin-prettier": "^4.2.1",
67
- "husky": "^8.0.3",
68
- "is-ci": "^3.0.1",
69
- "prettier": "^2.8.8",
70
- "rollup-plugin-visualizer": "^5.9.0",
71
- "semantic-release": "^21.0.2",
72
- "ts-jest": "^29.1.0",
73
- "typedoc": "^0.24.7",
74
- "typedoc-plugin-mdn-links": "^3.0.3",
75
- "typescript": "^5.0.4",
76
- "vite": "^4.3.5",
77
- "vite-plugin-dts": "^2.3.0",
78
- "vitest": "^0.31.0"
60
+ "@types/node": "^24.9.1",
61
+ "@vitest/ui": "^4.0.3",
62
+ "@vitest/coverage-v8": "^4.0.3",
63
+ "big.js": "^7.0.1",
64
+ "husky": "^9.1.7",
65
+ "is-ci": "^4.1.0",
66
+ "rollup-plugin-visualizer": "^6.0.5",
67
+ "semantic-release": "^25.0.1",
68
+ "typedoc": "^0.28.14",
69
+ "typedoc-plugin-mdn-links": "^5.0.10",
70
+ "typedoc-plugin-merge-modules": "^7.0.0",
71
+ "typedoc-plugin-pages": "^1.1.0",
72
+ "typescript": "^5.9.3",
73
+ "vite": "^7.1.12",
74
+ "vite-plugin-dts": "^4.5.4",
75
+ "vitest": "^4.0.3"
79
76
  }
80
77
  }