utils.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Arr = exports.BigNumOps = exports.chainPromises = void 0;
  4. const crypto = require("crypto");
  5. const chainPromises = (lazyPromises, reducer = (lp1, lp2) => (p) => lp1(p).then(lp2), initialValue) => lazyPromises.reduce(reducer, p => Promise.resolve(p))(initialValue);
  6. exports.chainPromises = chainPromises;
  7. const shuffleArray = (arr) => {
  8. const res = arr.filter(e => e);
  9. for (let i = res.length - 1; i > 0; i--) {
  10. const j = crypto.randomInt(0, i + 1);
  11. [res[i], res[j]] = [res[j], res[i]];
  12. }
  13. return res;
  14. };
  15. const chunkArray = (arr, size) => {
  16. const noOfChunks = Math.ceil(size && arr.length / size);
  17. const res = Array(noOfChunks);
  18. for (let [i, j] = [0, 0]; i < noOfChunks; i++) {
  19. res[i] = arr.slice(j, j += size);
  20. }
  21. return res;
  22. };
  23. const splitBigNumAt = (num, at) => num.replace(RegExp(String.raw `^([+-]?)(\d+)(\d{${at}})$`), '$1$2,$1$3')
  24. .replace(/^([^,]*)$/, '0,$1').split(',')
  25. .map(Number);
  26. const bigNumPlus = (num1, num2) => {
  27. let [high, low] = [splitBigNumAt(num1, 15), splitBigNumAt(num2, 15)]
  28. .reduce((a, b) => [a[0] + b[0], a[1] + b[1]]);
  29. const [highSign, lowSign] = [high, low].map(Math.sign);
  30. if (highSign === 0)
  31. return low.toString();
  32. if (highSign + lowSign === 0) {
  33. [high, low] = [high - highSign, low - lowSign * Math.pow(10, 15)];
  34. }
  35. else {
  36. [high, low] = [high + Math.trunc(low / Math.pow(10, 15)), low % Math.pow(10, 15)];
  37. }
  38. if (high === 0)
  39. return low.toString();
  40. return `${high}${Math.abs(low).toString().padStart(15, '0')}`;
  41. };
  42. const bigNumCompare = (num1, num2) => Math.sign(Number(bigNumPlus(num1, num2.replace(/^([+-]?)(\d+)/, (_, $1, $2) => `${$1 === '-' ? '' : '-'}${$2}`))));
  43. const bigNumMin = (...nums) => {
  44. if (!nums || !nums.length)
  45. return undefined;
  46. let min = nums[0];
  47. for (let i = 1; i < nums.length; i++) {
  48. if (bigNumCompare(nums[i], min) < 0)
  49. min = nums[i];
  50. }
  51. return min;
  52. };
  53. const bigNumLShift = (num, by) => {
  54. if (by < 0)
  55. throw Error('cannot perform right shift');
  56. const at = Math.trunc((52 - by) / 10) * 3;
  57. const [high, low] = splitBigNumAt(num, at).map(n => n * Math.pow(2, by));
  58. return bigNumPlus(`${high}${'0'.repeat(at)}`, low.toString());
  59. };
  60. const parseBigNum = (str) => (/^-?\d+$/.exec(str) || [''])[0].replace(/^(-)?0*/, '$1');
  61. exports.BigNumOps = {
  62. splitAt: splitBigNumAt,
  63. plus: bigNumPlus,
  64. compare: bigNumCompare,
  65. min: bigNumMin,
  66. lShift: bigNumLShift,
  67. parse: parseBigNum,
  68. };
  69. exports.Arr = { chunk: chunkArray, shuffle: shuffleArray };