All files / tr8-script/domain/utils math-low-level.ts

100% Statements 55/55
100% Branches 5/5
100% Functions 15/15
100% Lines 55/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179                                  14x 14x           14x         14x         14x     764x 764x 764x 764x       6x               11861x                       6564x 6564x             510x 510x 156x 156x   510x       545x 545x       60x 60x       34x 34x       3664x 3664x       122x   122x 122x 122x 122x       100x 100x 100x 100x       1896x 1896x 1896x       60x 60x 60x       34x 34x 34x       17x 17x   17x 17x   17x 17x   17x   17x 17x 17x   17x                                                        
import {
  add,
  createDinero,
  Currency,
  Dinero,
  multiply,
  subtract,
  toDecimal,
  toSnapshot,
  trimScale,
} from "dinero.js";
 
import { calculator } from "@dinero.js/calculator-bigint";
 
import { Amount, Fraction } from "../value.js";
import { minus, negate, normalize, remainder, split, times } from "./math.js";
 
const dineroBigint = createDinero({ calculator });
const TR8UNIT: Currency<bigint> = {
  code: "TR8UNIT",
  base: 10n,
  exponent: 2n,
};
 
const unity: Amount = {
  amount: 1n,
  scale: 0n,
};
 
const zero: Amount = {
  amount: 0n,
  scale: 0n,
};
 
const zeroFraction: Fraction = [zero, unity];
 
function decimalStringToAmount(decimalString: string): Amount {
  const [integral, fractional = ""] = decimalString.split(".");
  const amount = BigInt(integral + fractional);
  const scale = BigInt(fractional.length);
  return { amount, scale };
}
 
function amountToDecimalString(amount: Amount): string {
  return toDecimal(trimScale(atd(amount)));
}
 
/**
 * Amount to Dinero, Converts TR8Script Amount to Dinero
 * @param amount Tr8Script Amount
 */
function atd(amount: Amount): Dinero<bigint> {
  return dineroBigint({
    amount: amount.amount,
    currency: TR8UNIT,
    scale: amount.scale,
  });
}
 
/**
 * Dinero to Amount, Converts Dinero to TR8Script Amount
 * @param dinero Dinero Object
 */
function dta(dinero: Dinero<bigint>): Amount {
  const ss = toSnapshot(dinero);
  return {
    amount: ss.amount,
    scale: ss.scale,
  };
}
 
function polarityFix(a: Fraction): Fraction {
  let [n, d] = a;
  if (d.amount < 0n) {
    n = negate(n);
    d = negate(d);
  }
  return [n, d];
}
 
function simplePlus(a: Amount, b: Amount): Amount {
  const ans = dta(add(atd(a), atd(b)));
  return ans;
}
 
function mixPlus(a: Amount, [bn, bd]: Fraction): Fraction {
  const an = times(a, bd);
  return polarityFix([simplePlus(an, bn), bd]);
}
 
function crossPlus(a: Fraction, b: Fraction): Fraction {
  const [[an, ad], [bn]] = normalize([a, b]);
  return polarityFix([simplePlus(an, bn), ad]);
}
 
function simpleMultiply(a: Amount, b: Amount): Amount {
  const ans = multiply(atd(a), b);
  return dta(ans);
}
 
function mixMultiply(a: Amount, b: Fraction): Amount {
  const [bn, bd] = polarityFix(b);
 
  const [count, mod] = remainder(bn, bd);
  const [n] = split(a, [mod, minus(bd, mod)]);
  const val = simplePlus(n, simpleMultiply(a, { amount: count, scale: 0n }));
  return bn.amount < 0n ? negate(val) : val;
}
 
function crossMultiply(a: Fraction, b: Fraction): Fraction {
  const [an, ad] = a;
  const [bn, bd] = b;
  const ret = [times(an, bn), times(ad, bd)] as Fraction;
  return polarityFix(ret);
}
 
function simpleMinus(a: Amount, b: Amount): Amount {
  const [aa, bb] = [atd(a), atd(b)];
  const ans = subtract(aa, bb);
  return dta(ans);
}
 
function mixMinus(a: Amount, [bn, bd]: Fraction): Fraction {
  const an = simpleMultiply(a, bd);
  const ret = [simpleMinus(an, bn), bd] as Fraction;
  return polarityFix(ret);
}
 
function crossMinus(a: Fraction, b: Fraction): Fraction {
  const [[an, ad], [bn]] = normalize([a, b]);
  const ret = [simpleMinus(an, bn), ad] as Fraction;
  return polarityFix(ret);
}
 
function simpleDivide(a: Amount, b: Amount): Amount {
  const dinA = atd(a);
  const dinB = atd(b);
 
  const unitToDecimalAa = toDecimal(dinA);
  const unitToDecimalBb = toDecimal(dinB);
 
  const decimalAa = parseFloat(unitToDecimalAa);
  const decimalBb = parseFloat(unitToDecimalBb);
 
  const dividedAmount = decimalAa! / decimalBb!;
 
  const amount = decimalStringToAmount(dividedAmount.toString());
  const resultAmount = amount.amount;
  const resultScale = amount.scale;
 
  return {
    amount: resultAmount,
    scale: resultScale,
  };
}
 
export {
  decimalStringToAmount,
  amountToDecimalString,
  atd,
  dta,
  unity,
  zero,
  zeroFraction,
  TR8UNIT,
  dineroBigint,
  polarityFix,
  simpleMinus,
  mixMinus,
  crossMinus,
  simplePlus,
  mixPlus,
  crossPlus,
  simpleMultiply,
  mixMultiply,
  crossMultiply,
  simpleDivide,
};