@refactorlabs/recount 0.10.0 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -240,6 +240,11 @@ async function executeFetch(input, init2, recordSpan) {
240
240
 
241
241
  // src/lib/decimal.ts
242
242
  var DECIMAL = /^-?(\d+(\.\d+)?|\.\d+)$/;
243
+ var SIGNIFICANT_DIGITS = 15;
244
+ function withoutFloatNoise(n) {
245
+ if (!Number.isFinite(n) || Number.isInteger(n)) return n === 0 ? 0 : n;
246
+ return Number(n.toPrecision(SIGNIFICANT_DIGITS));
247
+ }
243
248
  function decimalText(n) {
244
249
  if (!Number.isFinite(n)) return void 0;
245
250
  if (Object.is(n, -0) || n === 0) return "0";
@@ -474,9 +479,9 @@ var CalculationBuilder = class {
474
479
  return __privateMethod(this, _CalculationBuilder_instances, kindLine_fn).call(this, "info", key, label, formula, options);
475
480
  }
476
481
  /**
477
- * A tiered charge: per band, a working line for its quantity and a
478
- * charge, then a subtotal keyed `key` that's checked against `value`.
479
- * Band n's quantity is `clamp(quantity - before_n, 0, size_n) * periods`.
482
+ * A tiered charge: a charge per band, then a subtotal keyed `key` that's
483
+ * checked against `value`. Band n's quantity is the formula
484
+ * `clamp(quantity - before_n, 0, size_n) * periods`.
480
485
  */
481
486
  tiers(key, options) {
482
487
  return __privateMethod(this, _CalculationBuilder_instances, push_fn).call(this, { type: "tiers", key, options });
@@ -890,19 +895,19 @@ function addTiers(s, rawKey, options) {
890
895
  const formula = p === void 0 || p === 1 || p === "1" ? amount : `${amount} * ${operandText(p)}`;
891
896
  const range = size === null ? before === "0" ? `all${unitText}${perText}` : `over ${before}${unitText}${perText}` : i === 0 ? `first ${size}${unitText}${perText}` : `next ${size}${unitText}${perText}`;
892
897
  const bandLabel = band?.label ?? `${base} tier ${n} (${range})`;
893
- const qtyKey = addLine(s, {
894
- key: `${key}_${n}_qty`,
895
- label: `${base} tier ${n} quantity`,
896
- kind: "working",
897
- formula,
898
- options: { ...unit !== void 0 && { unit } }
899
- });
898
+ const bandKey = `${key}_${n}`;
899
+ const tooLong = formula.length > MAX_FORMULA;
900
+ if (tooLong) {
901
+ s.problems.push(
902
+ `${s.calcKey}.${bandKey}: quantity: the formula is ${formula.length} characters, over ${MAX_FORMULA}; the band isn't calculated`
903
+ );
904
+ }
900
905
  charges.push(
901
906
  addLine(s, {
902
- key: `${key}_${n}`,
907
+ key: bandKey,
903
908
  label: bandLabel,
904
909
  kind: "charge",
905
- quantity: qtyKey,
910
+ quantity: tooLong ? null : formula,
906
911
  rate: band?.rate,
907
912
  options: {
908
913
  ...band?.value !== void 0 && { value: band.value },
@@ -1093,7 +1098,7 @@ function cleanValue(value, at, problems) {
1093
1098
  );
1094
1099
  return null;
1095
1100
  }
1096
- return Object.is(value, -0) ? 0 : value;
1101
+ return withoutFloatNoise(value);
1097
1102
  }
1098
1103
  if (typeof value === "string") {
1099
1104
  return text(value, MAX_TEXT_VALUE, at, problems);
@@ -1117,7 +1122,7 @@ function operandOf(value, at, problems) {
1117
1122
  problems.push(`${at}: ${value} can't be sent; sent as not calculated`);
1118
1123
  return null;
1119
1124
  }
1120
- return Object.is(value, -0) ? 0 : value;
1125
+ return withoutFloatNoise(value);
1121
1126
  }
1122
1127
  if (typeof value === "string") return formulaText(value, at, problems);
1123
1128
  const decimal = decimalObjectText(value);
@@ -1141,7 +1146,7 @@ function formulaText(formula, at, problems) {
1141
1146
  }
1142
1147
  function sizeText(value, at, problems) {
1143
1148
  if (typeof value === "number" && Number.isFinite(value) && value >= 0) {
1144
- return decimalText(value);
1149
+ return decimalText(withoutFloatNoise(value));
1145
1150
  }
1146
1151
  const text2 = typeof value === "string" ? value.trim() : decimalObjectText(value);
1147
1152
  if (text2 !== void 0 && isDecimalText(text2) && !text2.startsWith("-")) {
@@ -1418,24 +1423,15 @@ function fromPlain(calc) {
1418
1423
  return b;
1419
1424
  }
1420
1425
  var units = {
1426
+ /** A fraction shown as a percentage: `0.03` is `3%`. */
1421
1427
  percent: "%",
1428
+ /** Counts: singular at 1 ("1 day"), and per one in a rate ("$0.67/day"). */
1422
1429
  days: "days",
1423
1430
  months: "months",
1424
1431
  years: "years",
1425
1432
  hours: "hours",
1426
- kWh: "kWh",
1427
- MWh: "MWh",
1428
- MJ: "MJ",
1429
- GJ: "GJ",
1430
- kW: "kW",
1431
- kVA: "kVA",
1432
- kWhPerYear: "kWh/yr",
1433
- m2: "m\xB2",
1434
- panels: "panels",
1435
1433
  /** A rate per something: `units.per('AUD', 'kWh')` is `'AUD/kWh'`. */
1436
- per: (unit, per) => `${unit}/${per}`,
1437
- /** `units.perDay('AUD')` is `'AUD/day'`. */
1438
- perDay: (unit) => `${unit}/day`
1434
+ per: (unit, per) => `${unit}/${per}`
1439
1435
  };
1440
1436
 
1441
1437
  // src/trace.ts