|
1 | | -// parcel required for this import to work like that |
| 1 | +// parcel required for these import to work like that |
2 | 2 | import seedrandom from 'seedrandom'; |
| 3 | +import bigInt from 'big-integer'; |
3 | 4 |
|
4 | 5 | const SALT = 'ds-glasses-app-2019'; |
5 | 6 | const ALPHABET = 'ABCDEFGHIJKLMNPQRSTUVWXYZ1234567890'; // no big O |
@@ -39,57 +40,12 @@ function base(b) { |
39 | 40 | })(); |
40 | 41 |
|
41 | 42 |
|
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 | | - |
53 | 43 | // convert number system |
54 | 44 | 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; |
88 | 46 | } |
89 | 47 |
|
90 | | -// cns(100, 36, [1, 2, 3, 4, 5,]); |
91 | | - |
92 | | - |
| 48 | +// console.log( cns(100, 36, [1, 2, 3, 4, 5,]) ); |
93 | 49 |
|
94 | 50 |
|
95 | 51 | // make groups of 4 or 3, whichever makes longer last section |
|
0 commit comments