@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.d.cts CHANGED
@@ -502,9 +502,9 @@ declare class CalculationBuilder<K extends string = never> {
502
502
  /** A muted line that isn't summed, such as tax included or a % difference. */
503
503
  info<NK extends string | undefined>(key: NK, label: string, formula: string | null, options?: LineOptions): CalculationBuilder<K | Defined<NK>>;
504
504
  /**
505
- * A tiered charge: per band, a working line for its quantity and a
506
- * charge, then a subtotal keyed `key` that's checked against `value`.
507
- * Band n's quantity is `clamp(quantity - before_n, 0, size_n) * periods`.
505
+ * A tiered charge: a charge per band, then a subtotal keyed `key` that's
506
+ * checked against `value`. Band n's quantity is the formula
507
+ * `clamp(quantity - before_n, 0, size_n) * periods`.
508
508
  */
509
509
  tiers<NK extends string | undefined>(key: NK, options: TiersOptions): CalculationBuilder<K | Defined<NK>>;
510
510
  /** A percentage of other lines, such as a discount: `rate * sum(of)`. */
@@ -533,26 +533,21 @@ declare class CalculationBuilder<K extends string = never> {
533
533
  /** Starts a calculation: inputs, then lines in display order. */
534
534
  declare function calculation(key: string, options?: CalculationOptions): CalculationBuilder;
535
535
  type CalculationLike = CalculationBuilder<string> | Calculation;
536
- /** Common units, so they're spelled the way Recount formats them. Units are free text. */
536
+ /**
537
+ * The units Recount formats specially. Any other unit is free text, such as
538
+ * `'kWh'` or `'MJ'`, and is shown as given. A currency code (`'AUD'`) is
539
+ * formatted as money, and `'AUD/kWh'` as a money rate.
540
+ */
537
541
  declare const units: {
542
+ /** A fraction shown as a percentage: `0.03` is `3%`. */
538
543
  readonly percent: "%";
544
+ /** Counts: singular at 1 ("1 day"), and per one in a rate ("$0.67/day"). */
539
545
  readonly days: "days";
540
546
  readonly months: "months";
541
547
  readonly years: "years";
542
548
  readonly hours: "hours";
543
- readonly kWh: "kWh";
544
- readonly MWh: "MWh";
545
- readonly MJ: "MJ";
546
- readonly GJ: "GJ";
547
- readonly kW: "kW";
548
- readonly kVA: "kVA";
549
- readonly kWhPerYear: "kWh/yr";
550
- readonly m2: "m²";
551
- readonly panels: "panels";
552
549
  /** A rate per something: `units.per('AUD', 'kWh')` is `'AUD/kWh'`. */
553
550
  readonly per: (unit: string, per: string) => string;
554
- /** `units.perDay('AUD')` is `'AUD/day'`. */
555
- readonly perDay: (unit: string) => string;
556
551
  };
557
552
 
558
553
  declare class SpanGroup {
@@ -783,17 +778,7 @@ declare const recount: {
783
778
  readonly months: "months";
784
779
  readonly years: "years";
785
780
  readonly hours: "hours";
786
- readonly kWh: "kWh";
787
- readonly MWh: "MWh";
788
- readonly MJ: "MJ";
789
- readonly GJ: "GJ";
790
- readonly kW: "kW";
791
- readonly kVA: "kVA";
792
- readonly kWhPerYear: "kWh/yr";
793
- readonly m2: "m²";
794
- readonly panels: "panels";
795
781
  readonly per: (unit: string, per: string) => string;
796
- readonly perDay: (unit: string) => string;
797
782
  };
798
783
  REDACTED: {
799
784
  readonly __redacted: true;
package/dist/index.d.ts CHANGED
@@ -502,9 +502,9 @@ declare class CalculationBuilder<K extends string = never> {
502
502
  /** A muted line that isn't summed, such as tax included or a % difference. */
503
503
  info<NK extends string | undefined>(key: NK, label: string, formula: string | null, options?: LineOptions): CalculationBuilder<K | Defined<NK>>;
504
504
  /**
505
- * A tiered charge: per band, a working line for its quantity and a
506
- * charge, then a subtotal keyed `key` that's checked against `value`.
507
- * Band n's quantity is `clamp(quantity - before_n, 0, size_n) * periods`.
505
+ * A tiered charge: a charge per band, then a subtotal keyed `key` that's
506
+ * checked against `value`. Band n's quantity is the formula
507
+ * `clamp(quantity - before_n, 0, size_n) * periods`.
508
508
  */
509
509
  tiers<NK extends string | undefined>(key: NK, options: TiersOptions): CalculationBuilder<K | Defined<NK>>;
510
510
  /** A percentage of other lines, such as a discount: `rate * sum(of)`. */
@@ -533,26 +533,21 @@ declare class CalculationBuilder<K extends string = never> {
533
533
  /** Starts a calculation: inputs, then lines in display order. */
534
534
  declare function calculation(key: string, options?: CalculationOptions): CalculationBuilder;
535
535
  type CalculationLike = CalculationBuilder<string> | Calculation;
536
- /** Common units, so they're spelled the way Recount formats them. Units are free text. */
536
+ /**
537
+ * The units Recount formats specially. Any other unit is free text, such as
538
+ * `'kWh'` or `'MJ'`, and is shown as given. A currency code (`'AUD'`) is
539
+ * formatted as money, and `'AUD/kWh'` as a money rate.
540
+ */
537
541
  declare const units: {
542
+ /** A fraction shown as a percentage: `0.03` is `3%`. */
538
543
  readonly percent: "%";
544
+ /** Counts: singular at 1 ("1 day"), and per one in a rate ("$0.67/day"). */
539
545
  readonly days: "days";
540
546
  readonly months: "months";
541
547
  readonly years: "years";
542
548
  readonly hours: "hours";
543
- readonly kWh: "kWh";
544
- readonly MWh: "MWh";
545
- readonly MJ: "MJ";
546
- readonly GJ: "GJ";
547
- readonly kW: "kW";
548
- readonly kVA: "kVA";
549
- readonly kWhPerYear: "kWh/yr";
550
- readonly m2: "m²";
551
- readonly panels: "panels";
552
549
  /** A rate per something: `units.per('AUD', 'kWh')` is `'AUD/kWh'`. */
553
550
  readonly per: (unit: string, per: string) => string;
554
- /** `units.perDay('AUD')` is `'AUD/day'`. */
555
- readonly perDay: (unit: string) => string;
556
551
  };
557
552
 
558
553
  declare class SpanGroup {
@@ -783,17 +778,7 @@ declare const recount: {
783
778
  readonly months: "months";
784
779
  readonly years: "years";
785
780
  readonly hours: "hours";
786
- readonly kWh: "kWh";
787
- readonly MWh: "MWh";
788
- readonly MJ: "MJ";
789
- readonly GJ: "GJ";
790
- readonly kW: "kW";
791
- readonly kVA: "kVA";
792
- readonly kWhPerYear: "kWh/yr";
793
- readonly m2: "m²";
794
- readonly panels: "panels";
795
781
  readonly per: (unit: string, per: string) => string;
796
- readonly perDay: (unit: string) => string;
797
782
  };
798
783
  REDACTED: {
799
784
  readonly __redacted: true;
package/dist/index.js CHANGED
@@ -227,6 +227,11 @@ async function executeFetch(input, init2, recordSpan) {
227
227
 
228
228
  // src/lib/decimal.ts
229
229
  var DECIMAL = /^-?(\d+(\.\d+)?|\.\d+)$/;
230
+ var SIGNIFICANT_DIGITS = 15;
231
+ function withoutFloatNoise(n) {
232
+ if (!Number.isFinite(n) || Number.isInteger(n)) return n === 0 ? 0 : n;
233
+ return Number(n.toPrecision(SIGNIFICANT_DIGITS));
234
+ }
230
235
  function decimalText(n) {
231
236
  if (!Number.isFinite(n)) return void 0;
232
237
  if (Object.is(n, -0) || n === 0) return "0";
@@ -461,9 +466,9 @@ var CalculationBuilder = class {
461
466
  return __privateMethod(this, _CalculationBuilder_instances, kindLine_fn).call(this, "info", key, label, formula, options);
462
467
  }
463
468
  /**
464
- * A tiered charge: per band, a working line for its quantity and a
465
- * charge, then a subtotal keyed `key` that's checked against `value`.
466
- * Band n's quantity is `clamp(quantity - before_n, 0, size_n) * periods`.
469
+ * A tiered charge: a charge per band, then a subtotal keyed `key` that's
470
+ * checked against `value`. Band n's quantity is the formula
471
+ * `clamp(quantity - before_n, 0, size_n) * periods`.
467
472
  */
468
473
  tiers(key, options) {
469
474
  return __privateMethod(this, _CalculationBuilder_instances, push_fn).call(this, { type: "tiers", key, options });
@@ -877,19 +882,19 @@ function addTiers(s, rawKey, options) {
877
882
  const formula = p === void 0 || p === 1 || p === "1" ? amount : `${amount} * ${operandText(p)}`;
878
883
  const range = size === null ? before === "0" ? `all${unitText}${perText}` : `over ${before}${unitText}${perText}` : i === 0 ? `first ${size}${unitText}${perText}` : `next ${size}${unitText}${perText}`;
879
884
  const bandLabel = band?.label ?? `${base} tier ${n} (${range})`;
880
- const qtyKey = addLine(s, {
881
- key: `${key}_${n}_qty`,
882
- label: `${base} tier ${n} quantity`,
883
- kind: "working",
884
- formula,
885
- options: { ...unit !== void 0 && { unit } }
886
- });
885
+ const bandKey = `${key}_${n}`;
886
+ const tooLong = formula.length > MAX_FORMULA;
887
+ if (tooLong) {
888
+ s.problems.push(
889
+ `${s.calcKey}.${bandKey}: quantity: the formula is ${formula.length} characters, over ${MAX_FORMULA}; the band isn't calculated`
890
+ );
891
+ }
887
892
  charges.push(
888
893
  addLine(s, {
889
- key: `${key}_${n}`,
894
+ key: bandKey,
890
895
  label: bandLabel,
891
896
  kind: "charge",
892
- quantity: qtyKey,
897
+ quantity: tooLong ? null : formula,
893
898
  rate: band?.rate,
894
899
  options: {
895
900
  ...band?.value !== void 0 && { value: band.value },
@@ -1080,7 +1085,7 @@ function cleanValue(value, at, problems) {
1080
1085
  );
1081
1086
  return null;
1082
1087
  }
1083
- return Object.is(value, -0) ? 0 : value;
1088
+ return withoutFloatNoise(value);
1084
1089
  }
1085
1090
  if (typeof value === "string") {
1086
1091
  return text(value, MAX_TEXT_VALUE, at, problems);
@@ -1104,7 +1109,7 @@ function operandOf(value, at, problems) {
1104
1109
  problems.push(`${at}: ${value} can't be sent; sent as not calculated`);
1105
1110
  return null;
1106
1111
  }
1107
- return Object.is(value, -0) ? 0 : value;
1112
+ return withoutFloatNoise(value);
1108
1113
  }
1109
1114
  if (typeof value === "string") return formulaText(value, at, problems);
1110
1115
  const decimal = decimalObjectText(value);
@@ -1128,7 +1133,7 @@ function formulaText(formula, at, problems) {
1128
1133
  }
1129
1134
  function sizeText(value, at, problems) {
1130
1135
  if (typeof value === "number" && Number.isFinite(value) && value >= 0) {
1131
- return decimalText(value);
1136
+ return decimalText(withoutFloatNoise(value));
1132
1137
  }
1133
1138
  const text2 = typeof value === "string" ? value.trim() : decimalObjectText(value);
1134
1139
  if (text2 !== void 0 && isDecimalText(text2) && !text2.startsWith("-")) {
@@ -1405,24 +1410,15 @@ function fromPlain(calc) {
1405
1410
  return b;
1406
1411
  }
1407
1412
  var units = {
1413
+ /** A fraction shown as a percentage: `0.03` is `3%`. */
1408
1414
  percent: "%",
1415
+ /** Counts: singular at 1 ("1 day"), and per one in a rate ("$0.67/day"). */
1409
1416
  days: "days",
1410
1417
  months: "months",
1411
1418
  years: "years",
1412
1419
  hours: "hours",
1413
- kWh: "kWh",
1414
- MWh: "MWh",
1415
- MJ: "MJ",
1416
- GJ: "GJ",
1417
- kW: "kW",
1418
- kVA: "kVA",
1419
- kWhPerYear: "kWh/yr",
1420
- m2: "m\xB2",
1421
- panels: "panels",
1422
1420
  /** A rate per something: `units.per('AUD', 'kWh')` is `'AUD/kWh'`. */
1423
- per: (unit, per) => `${unit}/${per}`,
1424
- /** `units.perDay('AUD')` is `'AUD/day'`. */
1425
- perDay: (unit) => `${unit}/day`
1421
+ per: (unit, per) => `${unit}/${per}`
1426
1422
  };
1427
1423
 
1428
1424
  // src/trace.ts