BigInt Extras
Did some research on BigInt tonight and some of the short comings it has as far as transportation and arithmetic go.
Here is some code from my testing that might be useful for a helper.
/* global BigInt */
function bigPow (a, b) {
let c = a = BigInt(a)
for (let i = 1; i < b; i++) c = c * a
return c
}
class Mapper {
static walk (obj, cb) {
for (let key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key)) continue
if (typeof obj[key] === 'object') obj[key] = Mapper.walk(obj[key], cb)
else obj[key] = cb(obj[key], key)
}
return obj
}
}
console.log(bigPow(process.argv[2], process.argv[3]))
function encodeBigInt (obj) {
return Mapper.walk(obj, a => typeof a === 'bigint' ? `BigInt(${a})` : a)
}
function decodeBigInt (obj) {
return Mapper.walk(obj, a => {
const matches = a.match(/^BigInt\((\d+)\)$/)
if (matches[1]) return BigInt(matches[1])
return a
})
}
const str = JSON.stringify(encodeBigInt({ foo: 12n }))
console.log(str)
console.log(decodeBigInt(JSON.parse(str)))