Skip to content

Commit f360796

Browse files
committed
Removed dependency on native BigInt
1 parent 78da3a7 commit f360796

3 files changed

Lines changed: 12 additions & 376 deletions

File tree

main.js

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
// parcel required for this import to work like that
1+
// parcel required for these import to work like that
22
import seedrandom from 'seedrandom';
3+
import bigInt from 'big-integer';
34

45
const SALT = 'ds-glasses-app-2019';
56
const ALPHABET = 'ABCDEFGHIJKLMNPQRSTUVWXYZ1234567890'; // no big O
@@ -39,57 +40,12 @@ function base(b) {
3940
})();
4041

4142

42-
// Power function for BigInt (to replace ** operator for <ES2016)
43-
function pow(base, exponent) {
44-
if (exponent == BigInt(0)) return BigInt(1);
45-
if (exponent < BigInt(0)) throw 'Exponent needs to be >= 0!';
46-
let result = BigInt(1);
47-
for (let i=BigInt(0); i<exponent; i++) {
48-
result *= base;
49-
}
50-
return result;
51-
}
52-
5343
// convert number system
5444
function cns(fromBase, toBase, inputArray) {
55-
56-
// var hash = "",
57-
// alphabet = "0123456789abcdef",
58-
// alphabetLength = alphabet.length;
59-
//
60-
// do {
61-
// hash = alphabet[input % alphabetLength] + hash;
62-
// input = parseInt(input / alphabetLength, 10);
63-
// } while (input);
64-
//
65-
// convert input to a proper js integer (ie. decimal)
66-
let input = BigInt(0);
67-
// let exp = 1;
68-
// console.log(inputArray);
69-
for (let [i, el] of inputArray.entries()) {
70-
// input += ( BigInt(fromBase) ** BigInt(inputArray.length - i - 1) ) * BigInt(el);
71-
input += pow( BigInt(fromBase), BigInt(inputArray.length - i - 1) ) * BigInt(el);
72-
}
73-
// console.log(input);
74-
75-
let outputArray = [];
76-
toBase = BigInt(toBase);
77-
78-
do {
79-
outputArray.unshift( Number(input % toBase) ); // push from the left
80-
81-
// console.log(input % toBase, input / toBase)
82-
// console.log(input / toBase)
83-
// input = parseInt(input / toBase, 10);
84-
input = input / toBase
85-
} while (input);
86-
// console.log(outputArray);
87-
return outputArray;
45+
return bigInt.fromArray(inputArray, fromBase).toArray(toBase).value;
8846
}
8947

90-
// cns(100, 36, [1, 2, 3, 4, 5,]);
91-
92-
48+
// console.log( cns(100, 36, [1, 2, 3, 4, 5,]) );
9349

9450

9551
// make groups of 4 or 3, whichever makes longer last section

0 commit comments

Comments
 (0)