1 export default function Big() {
2 "use strict";
3
4 function intToDigits(n) {
5 return n
6 .split("")
7 .reverse()
8 .map((d) => +d);
9 }
10
11 function doubleToDidits(n) {
12 return [n % 10, (n / 10) >> 0];
13 }
14
15 function isBigInt(n) {
16 return typeof n === "string" && !n.includes(".");
17 }
18
19 function add(n0, n) {
20 if (!isBigInt(n0) || !isBigInt(n)) {
21 throw Error("Please enter a int");
22 }
23
24 n0 = intToDigits(n0);
25 n = intToDigits(n);
26 let max = Math.max(n0.length, n.length),
27 _high = 0,
28 digits = [];
29 for (let i = 0; i < max; i++) {
30 const [low, high] = doubleToDidits((n0[i] || 0) + (n[i] || 0) + _high);
31 digits.push(low);
32 _high = high;
33 }
34 _high && digits.push(_high);
35
36 return digits.reverse().join("");
37 }
38
39 function mul(n0, n) {
40 if (!isBigInt(n0) || !isBigInt(n)) {
41 throw Error("Please enter a int");
42 }
43
44 n0 = intToDigits(n0);
45 n = intToDigits(n);
46 const D1 = n0.reduce(function (D1, d1, i) {
47 let _high = 0;
48 let D2 = n.reduce(function (D2, d2) {
49 const [low, high] = doubleToDidits(d1 * d2 + _high);
50 D2.push(low);
51 _high = high;
52 return D2;
53 }, new Array(i).fill(0));
54 _high && D2.push(_high);
55 return add(D1, D2.reverse().join(""));
56 }, "");
57
58 return D1;
59 }
60
61 this.add = add;
62 this.mul = mul;
63 }