"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Arr = exports.BigNumOps = exports.rawRegExp = exports.customError = exports.CustomError = exports.neverResolves = exports.chainPromises = void 0; const CallableInstance = require("callable-instance"); const chainPromises = (lazyPromises, reducer = (lp1, lp2) => (p) => lp1(p).then(lp2), initialValue) => lazyPromises.reduce(reducer, p => Promise.resolve(p))(initialValue); exports.chainPromises = chainPromises; const neverResolves = () => new Promise(() => undefined); exports.neverResolves = neverResolves; class CustomErrorConstructor extends CallableInstance { constructor(name) { super('getError'); Object.defineProperty(this.constructor, 'name', { value: name }); } getError(message) { return new CustomError(this.constructor.name, message); } } class CustomError extends Error { constructor(name, message) { super(message); this.name = name; Object.defineProperty(this.constructor, 'name', { value: name }); } } exports.CustomError = CustomError; const customError = (name) => new CustomErrorConstructor(name); exports.customError = customError; const chunkArray = (arr, size) => { const noOfChunks = Math.ceil(size && arr.length / size); const res = Array(noOfChunks); for (let [i, j] = [0, 0]; i < noOfChunks; i++) { res[i] = arr.slice(j, j += size); } return res; }; const rawRegExp = (...args) => RegExp(String.raw(...args)); exports.rawRegExp = rawRegExp; const splitBigNumAt = (num, at) => num.replace(exports.rawRegExp `^([+-]?)(\d+)(\d{${at}})$`, '$1$2,$1$3') .replace(/^([^,]*)$/, '0,$1').split(',') .map(Number); const bigNumPlus = (num1, num2) => { let [high, low] = [splitBigNumAt(num1, 15), splitBigNumAt(num2, 15)] .reduce((a, b) => [a[0] + b[0], a[1] + b[1]]); const [highSign, lowSign] = [high, low].map(Math.sign); if (highSign === 0) return low.toString(); if (highSign + lowSign === 0) { [high, low] = [high - highSign, low - lowSign * Math.pow(10, 15)]; } else { [high, low] = [high + Math.trunc(low / Math.pow(10, 15)), low % Math.pow(10, 15)]; } if (high === 0) return low.toString(); return `${high}${Math.abs(low).toString().padStart(15, '0')}`; }; const bigNumCompare = (num1, num2) => Math.sign(Number(bigNumPlus(num1, num2.replace(/^([+-]?)(\d+)/, (_, $1, $2) => `${$1 === '-' ? '' : '-'}${$2}`)))); const bigNumMin = (...nums) => { if (!nums || !nums.length) return undefined; let min = nums[0]; for (let i = 1; i < nums.length; i++) { if (bigNumCompare(nums[i], min) < 0) min = nums[i]; } return min; }; const bigNumLShift = (num, by) => { if (by < 0) throw Error('cannot perform right shift'); const at = Math.trunc((52 - by) / 10) * 3; const [high, low] = splitBigNumAt(num, at).map(n => n * Math.pow(2, by)); return bigNumPlus(`${high}${'0'.repeat(at)}`, low.toString()); }; const parseBigNum = (str) => (/^-?\d+$/.exec(str) || [''])[0].replace(/^(-)?0*/, '$1'); exports.BigNumOps = { splitAt: splitBigNumAt, plus: bigNumPlus, compare: bigNumCompare, min: bigNumMin, lShift: bigNumLShift, parse: parseBigNum, }; exports.Arr = { chunk: chunkArray };