@refactorlabs/recount 0.9.1 → 0.11.0
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/README.md +3 -1
- package/dist/index.cjs +1310 -40
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +343 -10
- package/dist/index.d.ts +343 -10
- package/dist/index.js +1304 -38
- package/dist/index.js.map +1 -1
- package/llms.md +108 -19
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -88,21 +88,21 @@ function findHeader(headers, name) {
|
|
|
88
88
|
}
|
|
89
89
|
return void 0;
|
|
90
90
|
}
|
|
91
|
-
function parseBodyText(
|
|
92
|
-
if (
|
|
91
|
+
function parseBodyText(text2) {
|
|
92
|
+
if (text2 === "") {
|
|
93
93
|
return null;
|
|
94
94
|
}
|
|
95
|
-
if (
|
|
96
|
-
return /^\s*[[{]/.test(
|
|
95
|
+
if (text2.length > MAX_BODY_LENGTH) {
|
|
96
|
+
return /^\s*[[{]/.test(text2) ? OMITTED_JSON : truncate(text2, MAX_BODY_LENGTH);
|
|
97
97
|
}
|
|
98
98
|
try {
|
|
99
|
-
const parsed = JSON.parse(
|
|
99
|
+
const parsed = JSON.parse(text2);
|
|
100
100
|
if (typeof parsed === "object" && parsed !== null) {
|
|
101
101
|
return parsed;
|
|
102
102
|
}
|
|
103
103
|
} catch {
|
|
104
104
|
}
|
|
105
|
-
return
|
|
105
|
+
return text2;
|
|
106
106
|
}
|
|
107
107
|
function parseRequestBody(body, contentType) {
|
|
108
108
|
if (body == null) {
|
|
@@ -138,20 +138,20 @@ async function readResponseBody(response, contentType) {
|
|
|
138
138
|
timer = setTimeout(() => resolve("timeout"), BODY_READ_TIMEOUT_MS);
|
|
139
139
|
});
|
|
140
140
|
const decoder = new TextDecoder();
|
|
141
|
-
let
|
|
142
|
-
while (
|
|
141
|
+
let text2 = "";
|
|
142
|
+
while (text2.length <= MAX_BODY_LENGTH) {
|
|
143
143
|
const chunk = await Promise.race([reader.read(), timedOut]);
|
|
144
144
|
if (chunk === "timeout") {
|
|
145
145
|
stopReading();
|
|
146
146
|
return STILL_STREAMING;
|
|
147
147
|
}
|
|
148
148
|
if (chunk.done) {
|
|
149
|
-
return parseBodyText(
|
|
149
|
+
return parseBodyText(text2 + decoder.decode());
|
|
150
150
|
}
|
|
151
|
-
|
|
151
|
+
text2 += decoder.decode(chunk.value, { stream: true });
|
|
152
152
|
}
|
|
153
153
|
stopReading();
|
|
154
|
-
return parseBodyText(
|
|
154
|
+
return parseBodyText(text2);
|
|
155
155
|
} catch {
|
|
156
156
|
return null;
|
|
157
157
|
} finally {
|
|
@@ -238,6 +238,71 @@ async function executeFetch(input, init2, recordSpan) {
|
|
|
238
238
|
return response;
|
|
239
239
|
}
|
|
240
240
|
|
|
241
|
+
// src/lib/decimal.ts
|
|
242
|
+
var DECIMAL = /^-?(\d+(\.\d+)?|\.\d+)$/;
|
|
243
|
+
function decimalText(n) {
|
|
244
|
+
if (!Number.isFinite(n)) return void 0;
|
|
245
|
+
if (Object.is(n, -0) || n === 0) return "0";
|
|
246
|
+
const text2 = String(n);
|
|
247
|
+
if (!/e/i.test(text2)) return text2;
|
|
248
|
+
const [mantissa, exp] = text2.toLowerCase().split("e");
|
|
249
|
+
const negative = mantissa.startsWith("-");
|
|
250
|
+
const [whole, fraction = ""] = mantissa.replace("-", "").split(".");
|
|
251
|
+
const digits = whole + fraction;
|
|
252
|
+
const point = whole.length + Number(exp);
|
|
253
|
+
let out;
|
|
254
|
+
if (point <= 0) {
|
|
255
|
+
out = `0.${"0".repeat(-point)}${digits}`;
|
|
256
|
+
} else if (point >= digits.length) {
|
|
257
|
+
out = digits + "0".repeat(point - digits.length);
|
|
258
|
+
} else {
|
|
259
|
+
out = `${digits.slice(0, point)}.${digits.slice(point)}`;
|
|
260
|
+
}
|
|
261
|
+
return negative ? `-${out}` : out;
|
|
262
|
+
}
|
|
263
|
+
function isDecimalText(text2) {
|
|
264
|
+
return DECIMAL.test(text2.trim());
|
|
265
|
+
}
|
|
266
|
+
function toScaled(text2, scale) {
|
|
267
|
+
const t = text2.trim();
|
|
268
|
+
const negative = t.startsWith("-");
|
|
269
|
+
const [whole, fraction = ""] = t.replace("-", "").split(".");
|
|
270
|
+
const value = BigInt((whole || "0") + fraction.padEnd(scale, "0"));
|
|
271
|
+
return negative ? -value : value;
|
|
272
|
+
}
|
|
273
|
+
function placesOf(text2) {
|
|
274
|
+
return (text2.split(".")[1] ?? "").length;
|
|
275
|
+
}
|
|
276
|
+
function fromScaled(value, scale) {
|
|
277
|
+
const negative = value < 0n;
|
|
278
|
+
const digits = (negative ? -value : value).toString().padStart(scale + 1, "0");
|
|
279
|
+
const whole = digits.slice(0, digits.length - scale);
|
|
280
|
+
const fraction = digits.slice(digits.length - scale).replace(/0+$/, "");
|
|
281
|
+
const out = fraction ? `${whole}.${fraction}` : whole;
|
|
282
|
+
return negative && out !== "0" ? `-${out}` : out;
|
|
283
|
+
}
|
|
284
|
+
function addDecimals(...values) {
|
|
285
|
+
const scale = Math.max(0, ...values.map(placesOf));
|
|
286
|
+
return fromScaled(
|
|
287
|
+
values.reduce((sum, v) => sum + toScaled(v, scale), 0n),
|
|
288
|
+
scale
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
function decimalObjectText(value) {
|
|
292
|
+
if (typeof value !== "object" || value === null) return void 0;
|
|
293
|
+
const proto = Object.getPrototypeOf(value);
|
|
294
|
+
if (proto === Object.prototype || proto === null) return void 0;
|
|
295
|
+
try {
|
|
296
|
+
const toFixed = value.toFixed;
|
|
297
|
+
const text2 = String(
|
|
298
|
+
typeof toFixed === "function" ? toFixed.call(value) : value
|
|
299
|
+
).trim();
|
|
300
|
+
return isDecimalText(text2) ? text2 : void 0;
|
|
301
|
+
} catch {
|
|
302
|
+
return void 0;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
241
306
|
// src/lib/redact.ts
|
|
242
307
|
var REDACTED = { __redacted: true };
|
|
243
308
|
var SENSITIVE_HEADERS = /* @__PURE__ */ new Set([
|
|
@@ -283,8 +348,1101 @@ function redactObject(obj, fields) {
|
|
|
283
348
|
return redactAt(obj, paths, []);
|
|
284
349
|
}
|
|
285
350
|
|
|
351
|
+
// src/lib/calculation.ts
|
|
352
|
+
var MAX_CALCULATIONS = 20;
|
|
353
|
+
var MAX_ROWS = 500;
|
|
354
|
+
var MAX_LINES = 200;
|
|
355
|
+
var MAX_INPUTS = 100;
|
|
356
|
+
var MAX_FORMULA = 512;
|
|
357
|
+
var MAX_KEY = 64;
|
|
358
|
+
var MAX_LABEL = 256;
|
|
359
|
+
var MAX_UNIT = 32;
|
|
360
|
+
var MAX_TEXT_VALUE = 256;
|
|
361
|
+
var MAX_TAGS = 10;
|
|
362
|
+
var MAX_TAG = 64;
|
|
363
|
+
var MAX_NOTES = 20;
|
|
364
|
+
var MAX_NOTE = 1e3;
|
|
365
|
+
var MAX_SUMMARY = 20;
|
|
366
|
+
var MAX_SUMMARY_TEXT = 512;
|
|
367
|
+
var MAX_DETAIL = 512;
|
|
368
|
+
var MAX_SOURCE = 256;
|
|
369
|
+
var MAX_SOURCE_SPAN = 500;
|
|
370
|
+
var MAX_DESCRIPTION = 1e3;
|
|
371
|
+
var FUNCTIONS = /* @__PURE__ */ new Set([
|
|
372
|
+
"min",
|
|
373
|
+
"max",
|
|
374
|
+
"clamp",
|
|
375
|
+
"abs",
|
|
376
|
+
"round",
|
|
377
|
+
"floor",
|
|
378
|
+
"ceil",
|
|
379
|
+
"mround",
|
|
380
|
+
"sum",
|
|
381
|
+
"sum_kind",
|
|
382
|
+
"sum_tagged",
|
|
383
|
+
"if"
|
|
384
|
+
]);
|
|
385
|
+
var WORDS = /* @__PURE__ */ new Set(["and", "or", "not"]);
|
|
386
|
+
var RESERVED = /* @__PURE__ */ new Set([...FUNCTIONS, ...WORDS]);
|
|
387
|
+
var KEY = /^[a-z][a-z0-9_]{0,63}$/;
|
|
388
|
+
var HIGHLIGHTS = /* @__PURE__ */ new Set(["danger", "warn", "success", "info", "muted"]);
|
|
389
|
+
var KINDS = /* @__PURE__ */ new Set([
|
|
390
|
+
"charge",
|
|
391
|
+
"step",
|
|
392
|
+
"working",
|
|
393
|
+
"subtotal",
|
|
394
|
+
"total",
|
|
395
|
+
"info"
|
|
396
|
+
]);
|
|
397
|
+
var COLUMN_LABELS = /* @__PURE__ */ new Set([
|
|
398
|
+
"charge",
|
|
399
|
+
"quantity",
|
|
400
|
+
"rate",
|
|
401
|
+
"amount",
|
|
402
|
+
"step",
|
|
403
|
+
"working",
|
|
404
|
+
"value"
|
|
405
|
+
]);
|
|
406
|
+
function isValidKey(key) {
|
|
407
|
+
return KEY.test(key) && !RESERVED.has(key);
|
|
408
|
+
}
|
|
409
|
+
var _key, _options, _ops, _built, _CalculationBuilder_instances, push_fn, kindLine_fn;
|
|
410
|
+
var CalculationBuilder = class {
|
|
411
|
+
constructor(key, options = {}) {
|
|
412
|
+
__privateAdd(this, _CalculationBuilder_instances);
|
|
413
|
+
__privateAdd(this, _key);
|
|
414
|
+
__privateAdd(this, _options);
|
|
415
|
+
__privateAdd(this, _ops, []);
|
|
416
|
+
__privateAdd(this, _built);
|
|
417
|
+
__privateSet(this, _key, key);
|
|
418
|
+
__privateSet(this, _options, options ?? {});
|
|
419
|
+
}
|
|
420
|
+
/** A value the calculation starts from: a usage, a rate, or a constant like 365.25 days. */
|
|
421
|
+
input(key, value, options = {}) {
|
|
422
|
+
return __privateMethod(this, _CalculationBuilder_instances, push_fn).call(this, { type: "input", key, value, options });
|
|
423
|
+
}
|
|
424
|
+
/** A charge: `quantity * rate` (`* times`), checked against `value`. */
|
|
425
|
+
charge(key, label, options) {
|
|
426
|
+
const { quantity, rate, times, ...rest } = options ?? {};
|
|
427
|
+
return __privateMethod(this, _CalculationBuilder_instances, push_fn).call(this, {
|
|
428
|
+
type: "line",
|
|
429
|
+
spec: { key, label, kind: "charge", quantity, rate, times, options: rest }
|
|
430
|
+
});
|
|
431
|
+
}
|
|
432
|
+
/** One step of a workings chain, with the formula it's derived by. */
|
|
433
|
+
step(key, label, formula, valueOrOptions, options) {
|
|
434
|
+
const rest = isOptions(valueOrOptions) ? valueOrOptions : { ...options, value: valueOrOptions };
|
|
435
|
+
return __privateMethod(this, _CalculationBuilder_instances, push_fn).call(this, {
|
|
436
|
+
type: "line",
|
|
437
|
+
spec: { key, label, kind: "step", formula, options: rest }
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
/** An intermediate quantity, shown muted and left out of `sum_kind()`. */
|
|
441
|
+
working(key, label, formula, options = {}) {
|
|
442
|
+
return __privateMethod(this, _CalculationBuilder_instances, push_fn).call(this, {
|
|
443
|
+
type: "line",
|
|
444
|
+
spec: { key, label, kind: "working", formula, options }
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
/** A value the engine gives with no formula. Its quantity and rate are shown, not checked. */
|
|
448
|
+
stated(key, label, options) {
|
|
449
|
+
const { quantity, rate, times, kind, ...rest } = options ?? {};
|
|
450
|
+
return __privateMethod(this, _CalculationBuilder_instances, push_fn).call(this, {
|
|
451
|
+
type: "line",
|
|
452
|
+
spec: {
|
|
453
|
+
key,
|
|
454
|
+
label,
|
|
455
|
+
kind,
|
|
456
|
+
formula: null,
|
|
457
|
+
quantity,
|
|
458
|
+
rate,
|
|
459
|
+
times,
|
|
460
|
+
options: rest
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
/** A subtotal, such as charges before discounts. `null` formula = stated. */
|
|
465
|
+
subtotal(key, label, formula, options = {}) {
|
|
466
|
+
return __privateMethod(this, _CalculationBuilder_instances, kindLine_fn).call(this, "subtotal", key, label, formula, options);
|
|
467
|
+
}
|
|
468
|
+
/** A total, such as `round(sum_kind('charge'), 2)`. `null` formula = stated. */
|
|
469
|
+
total(key, label, formula, options = {}) {
|
|
470
|
+
return __privateMethod(this, _CalculationBuilder_instances, kindLine_fn).call(this, "total", key, label, formula, options);
|
|
471
|
+
}
|
|
472
|
+
/** A muted line that isn't summed, such as tax included or a % difference. */
|
|
473
|
+
info(key, label, formula, options = {}) {
|
|
474
|
+
return __privateMethod(this, _CalculationBuilder_instances, kindLine_fn).call(this, "info", key, label, formula, options);
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* A tiered charge: a charge per band, then a subtotal keyed `key` that's
|
|
478
|
+
* checked against `value`. Band n's quantity is the formula
|
|
479
|
+
* `clamp(quantity - before_n, 0, size_n) * periods`.
|
|
480
|
+
*/
|
|
481
|
+
tiers(key, options) {
|
|
482
|
+
return __privateMethod(this, _CalculationBuilder_instances, push_fn).call(this, { type: "tiers", key, options });
|
|
483
|
+
}
|
|
484
|
+
/** A percentage of other lines, such as a discount: `rate * sum(of)`. */
|
|
485
|
+
percentOf(key, label, options) {
|
|
486
|
+
return __privateMethod(this, _CalculationBuilder_instances, push_fn).call(this, { type: "percentOf", key, label, options });
|
|
487
|
+
}
|
|
488
|
+
/** The tax a total includes: `sum(of) * rate / (1 + rate)`. */
|
|
489
|
+
taxIncluded(key, label, options) {
|
|
490
|
+
return __privateMethod(this, _CalculationBuilder_instances, push_fn).call(this, { type: "taxIncluded", key, label, options });
|
|
491
|
+
}
|
|
492
|
+
/** A line in the wire shape, for anything the helpers don't cover. */
|
|
493
|
+
line(line) {
|
|
494
|
+
return __privateMethod(this, _CalculationBuilder_instances, push_fn).call(this, { type: "raw", line });
|
|
495
|
+
}
|
|
496
|
+
/** A muted row under the lines. `{key}` placeholders are filled in. */
|
|
497
|
+
note(text2) {
|
|
498
|
+
__privateMethod(this, _CalculationBuilder_instances, push_fn).call(this, { type: "note", text: text2 });
|
|
499
|
+
return this;
|
|
500
|
+
}
|
|
501
|
+
/** A key, checked by TypeScript, for writing formulas: `` `${c.ref('days')} * 2` ``. */
|
|
502
|
+
ref(key) {
|
|
503
|
+
return key;
|
|
504
|
+
}
|
|
505
|
+
/** Every key given so far, for editor completion: `c.keys.days`. */
|
|
506
|
+
get keys() {
|
|
507
|
+
const keys = {};
|
|
508
|
+
for (const op of __privateGet(this, _ops)) {
|
|
509
|
+
const key = op.type === "input" ? op.key : op.type === "line" ? op.spec.key : op.type === "raw" ? op.line?.key : op.type === "note" ? void 0 : op.key;
|
|
510
|
+
if (typeof key === "string") keys[key] = key;
|
|
511
|
+
}
|
|
512
|
+
return keys;
|
|
513
|
+
}
|
|
514
|
+
/** What's wrong with the calculation, such as a formula naming an unknown key. */
|
|
515
|
+
get problems() {
|
|
516
|
+
return this.compile().problems;
|
|
517
|
+
}
|
|
518
|
+
/** The calculation as it's sent. Attach calls build it for you. */
|
|
519
|
+
build() {
|
|
520
|
+
const { calculation: calculation2 } = this.compile();
|
|
521
|
+
return calculation2 ? structuredClone(calculation2) : { version: 1, key: __privateGet(this, _key), lines: [] };
|
|
522
|
+
}
|
|
523
|
+
/** @internal Builds once, never throws. */
|
|
524
|
+
compile() {
|
|
525
|
+
if (!__privateGet(this, _built)) {
|
|
526
|
+
try {
|
|
527
|
+
__privateSet(this, _built, compile(__privateGet(this, _key), __privateGet(this, _options), __privateGet(this, _ops)));
|
|
528
|
+
} catch (err) {
|
|
529
|
+
__privateSet(this, _built, {
|
|
530
|
+
calculation: void 0,
|
|
531
|
+
problems: [
|
|
532
|
+
`${String(__privateGet(this, _key))}: couldn't be built (${err instanceof Error ? err.message : String(err)})`
|
|
533
|
+
]
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
return __privateGet(this, _built);
|
|
538
|
+
}
|
|
539
|
+
};
|
|
540
|
+
_key = new WeakMap();
|
|
541
|
+
_options = new WeakMap();
|
|
542
|
+
_ops = new WeakMap();
|
|
543
|
+
_built = new WeakMap();
|
|
544
|
+
_CalculationBuilder_instances = new WeakSet();
|
|
545
|
+
push_fn = function(op) {
|
|
546
|
+
__privateGet(this, _ops).push(op);
|
|
547
|
+
__privateSet(this, _built, void 0);
|
|
548
|
+
return this;
|
|
549
|
+
};
|
|
550
|
+
kindLine_fn = function(kind, key, label, formula, options) {
|
|
551
|
+
return __privateMethod(this, _CalculationBuilder_instances, push_fn).call(this, {
|
|
552
|
+
type: "line",
|
|
553
|
+
spec: { key, label, kind, formula: formula ?? null, options }
|
|
554
|
+
});
|
|
555
|
+
};
|
|
556
|
+
function calculation(key, options) {
|
|
557
|
+
return new CalculationBuilder(key, options);
|
|
558
|
+
}
|
|
559
|
+
function isPlainObject(value) {
|
|
560
|
+
if (typeof value !== "object" || value === null) return false;
|
|
561
|
+
const proto = Object.getPrototypeOf(value);
|
|
562
|
+
return proto === Object.prototype || proto === null;
|
|
563
|
+
}
|
|
564
|
+
function isOptions(value) {
|
|
565
|
+
return isPlainObject(value) && !("__redacted" in value);
|
|
566
|
+
}
|
|
567
|
+
function compile(rawKey, options, ops) {
|
|
568
|
+
const problems = [];
|
|
569
|
+
const key = repairKey(
|
|
570
|
+
rawKey,
|
|
571
|
+
"calculation",
|
|
572
|
+
/* @__PURE__ */ new Set(),
|
|
573
|
+
problems,
|
|
574
|
+
"Calculation"
|
|
575
|
+
);
|
|
576
|
+
const layout = options.layout === "steps" ? "steps" : "charges";
|
|
577
|
+
if (options.layout !== void 0 && options.layout !== layout) {
|
|
578
|
+
problems.push(
|
|
579
|
+
`${key}: unknown layout "${String(options.layout)}"; using charges`
|
|
580
|
+
);
|
|
581
|
+
}
|
|
582
|
+
const s = {
|
|
583
|
+
calcKey: key,
|
|
584
|
+
layout,
|
|
585
|
+
currency: void 0,
|
|
586
|
+
problems,
|
|
587
|
+
taken: /* @__PURE__ */ new Set(),
|
|
588
|
+
inputKeys: /* @__PURE__ */ new Set(),
|
|
589
|
+
inputs: [],
|
|
590
|
+
lines: [],
|
|
591
|
+
kinds: [],
|
|
592
|
+
notes: []
|
|
593
|
+
};
|
|
594
|
+
const where = (field) => `${key}: ${field}`;
|
|
595
|
+
const calc = { version: 1, key, lines: [] };
|
|
596
|
+
const title = text(options.title, MAX_LABEL, where("title"), problems);
|
|
597
|
+
if (title !== void 0) calc.title = title;
|
|
598
|
+
if (layout === "steps") calc.layout = "steps";
|
|
599
|
+
const currency = currencyCode(options.currency, where("currency"), problems);
|
|
600
|
+
if (currency !== void 0) {
|
|
601
|
+
calc.currency = currency;
|
|
602
|
+
s.currency = currency;
|
|
603
|
+
}
|
|
604
|
+
const places = placesOf2(options.places, where("places"), problems);
|
|
605
|
+
if (places !== void 0) calc.places = places;
|
|
606
|
+
const tolerance = toleranceOf(
|
|
607
|
+
options.tolerance,
|
|
608
|
+
where("tolerance"),
|
|
609
|
+
problems
|
|
610
|
+
);
|
|
611
|
+
if (tolerance !== void 0) calc.tolerance = tolerance;
|
|
612
|
+
const alerts = alertsOf(options.alerts, where("alerts"), problems);
|
|
613
|
+
if (alerts !== void 0) calc.alerts = alerts;
|
|
614
|
+
const columnLabels = columnLabelsOf(
|
|
615
|
+
options.columnLabels,
|
|
616
|
+
where("columnLabels"),
|
|
617
|
+
problems
|
|
618
|
+
);
|
|
619
|
+
if (columnLabels !== void 0) calc.columnLabels = columnLabels;
|
|
620
|
+
for (const op of ops) {
|
|
621
|
+
if (op.type === "input") addInput(s, op.key, op.value, op.options ?? {});
|
|
622
|
+
}
|
|
623
|
+
for (const op of ops) {
|
|
624
|
+
switch (op.type) {
|
|
625
|
+
case "line":
|
|
626
|
+
addLine(s, op.spec);
|
|
627
|
+
break;
|
|
628
|
+
case "raw":
|
|
629
|
+
addRaw(s, op.line);
|
|
630
|
+
break;
|
|
631
|
+
case "tiers":
|
|
632
|
+
addTiers(s, op.key, op.options ?? {});
|
|
633
|
+
break;
|
|
634
|
+
case "percentOf":
|
|
635
|
+
addPercentOf(
|
|
636
|
+
s,
|
|
637
|
+
op.key,
|
|
638
|
+
op.label,
|
|
639
|
+
op.options ?? {}
|
|
640
|
+
);
|
|
641
|
+
break;
|
|
642
|
+
case "taxIncluded":
|
|
643
|
+
addTaxIncluded(
|
|
644
|
+
s,
|
|
645
|
+
op.key,
|
|
646
|
+
op.label,
|
|
647
|
+
op.options ?? {}
|
|
648
|
+
);
|
|
649
|
+
break;
|
|
650
|
+
case "note": {
|
|
651
|
+
const note = text(op.text, MAX_NOTE, where("note"), problems);
|
|
652
|
+
if (note !== void 0) s.notes.push(note);
|
|
653
|
+
break;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
if (s.inputs.length > MAX_INPUTS) {
|
|
658
|
+
problems.push(
|
|
659
|
+
`${key}: ${s.inputs.length} inputs; only the first ${MAX_INPUTS} are sent`
|
|
660
|
+
);
|
|
661
|
+
s.inputs.length = MAX_INPUTS;
|
|
662
|
+
}
|
|
663
|
+
if (s.lines.length > MAX_LINES) {
|
|
664
|
+
problems.push(
|
|
665
|
+
`${key}: ${s.lines.length} lines; only the first ${MAX_LINES} are sent`
|
|
666
|
+
);
|
|
667
|
+
s.lines.length = MAX_LINES;
|
|
668
|
+
}
|
|
669
|
+
if (s.notes.length > MAX_NOTES) {
|
|
670
|
+
problems.push(
|
|
671
|
+
`${key}: ${s.notes.length} notes; only the first ${MAX_NOTES} are sent`
|
|
672
|
+
);
|
|
673
|
+
s.notes.length = MAX_NOTES;
|
|
674
|
+
}
|
|
675
|
+
checkFormulas(s);
|
|
676
|
+
if (s.inputs.length > 0) calc.inputs = s.inputs;
|
|
677
|
+
calc.lines = s.lines;
|
|
678
|
+
if (s.notes.length > 0) calc.notes = s.notes;
|
|
679
|
+
const summary = summaryOf(options.summary, where("summary"), problems);
|
|
680
|
+
if (summary !== void 0) calc.summary = summary;
|
|
681
|
+
if (s.lines.length === 0) {
|
|
682
|
+
problems.push(`${key}: has no lines, so it isn't sent`);
|
|
683
|
+
return { calculation: void 0, problems };
|
|
684
|
+
}
|
|
685
|
+
return { calculation: calc, problems };
|
|
686
|
+
}
|
|
687
|
+
function addInput(s, rawKey, value, options) {
|
|
688
|
+
const label = text(
|
|
689
|
+
options.label,
|
|
690
|
+
MAX_LABEL,
|
|
691
|
+
`${s.calcKey}: input label`,
|
|
692
|
+
s.problems
|
|
693
|
+
);
|
|
694
|
+
const key = repairKey(
|
|
695
|
+
rawKey,
|
|
696
|
+
label ?? "input",
|
|
697
|
+
s.taken,
|
|
698
|
+
s.problems,
|
|
699
|
+
`${s.calcKey}: input`
|
|
700
|
+
);
|
|
701
|
+
s.inputKeys.add(key);
|
|
702
|
+
const at = `${s.calcKey}.${key}`;
|
|
703
|
+
const input = {
|
|
704
|
+
key,
|
|
705
|
+
...label !== void 0 && { label },
|
|
706
|
+
value: options.redact ? REDACTED : cleanValue(value, at, s.problems) ?? null
|
|
707
|
+
};
|
|
708
|
+
if (value === void 0 && !options.redact) {
|
|
709
|
+
s.problems.push(`${at}: an input needs a value; sent as null`);
|
|
710
|
+
}
|
|
711
|
+
assignText(input, "unit", options.unit, MAX_UNIT, at, s.problems);
|
|
712
|
+
const places = placesOf2(options.places, `${at}: places`, s.problems);
|
|
713
|
+
if (places !== void 0) input.places = places;
|
|
714
|
+
assignText(input, "source", options.source, MAX_SOURCE, at, s.problems);
|
|
715
|
+
assignText(
|
|
716
|
+
input,
|
|
717
|
+
"sourceSpan",
|
|
718
|
+
options.sourceSpan,
|
|
719
|
+
MAX_SOURCE_SPAN,
|
|
720
|
+
at,
|
|
721
|
+
s.problems
|
|
722
|
+
);
|
|
723
|
+
assignText(
|
|
724
|
+
input,
|
|
725
|
+
"description",
|
|
726
|
+
options.description,
|
|
727
|
+
MAX_DESCRIPTION,
|
|
728
|
+
at,
|
|
729
|
+
s.problems
|
|
730
|
+
);
|
|
731
|
+
s.inputs.push(input);
|
|
732
|
+
}
|
|
733
|
+
function defaultKind(s) {
|
|
734
|
+
return s.layout === "steps" ? "step" : "charge";
|
|
735
|
+
}
|
|
736
|
+
function addLine(s, spec) {
|
|
737
|
+
const options = spec.options ?? {};
|
|
738
|
+
const label = text(spec.label, MAX_LABEL, `${s.calcKey}: label`, s.problems);
|
|
739
|
+
const key = repairKey(
|
|
740
|
+
spec.key,
|
|
741
|
+
label ?? "line",
|
|
742
|
+
s.taken,
|
|
743
|
+
s.problems,
|
|
744
|
+
`${s.calcKey}: line`
|
|
745
|
+
);
|
|
746
|
+
const at = `${s.calcKey}.${key}`;
|
|
747
|
+
const line = { key };
|
|
748
|
+
if (label !== void 0) line.label = label;
|
|
749
|
+
if (options.redact) {
|
|
750
|
+
line.value = REDACTED;
|
|
751
|
+
} else if (options.value !== void 0) {
|
|
752
|
+
const value = cleanValue(options.value, at, s.problems);
|
|
753
|
+
if (value !== void 0) line.value = value;
|
|
754
|
+
}
|
|
755
|
+
if (typeof spec.formula === "string") {
|
|
756
|
+
const formula = formulaText(spec.formula, at, s.problems);
|
|
757
|
+
line.formula = formula ?? null;
|
|
758
|
+
} else if (spec.formula === null && spec.quantity !== void 0 && spec.rate !== void 0) {
|
|
759
|
+
line.formula = null;
|
|
760
|
+
}
|
|
761
|
+
for (const field of ["quantity", "rate", "times"]) {
|
|
762
|
+
const operand = operandOf(spec[field], `${at}: ${field}`, s.problems);
|
|
763
|
+
if (operand !== void 0) line[field] = operand;
|
|
764
|
+
}
|
|
765
|
+
assignText(
|
|
766
|
+
line,
|
|
767
|
+
"quantityUnit",
|
|
768
|
+
options.quantityUnit,
|
|
769
|
+
MAX_UNIT,
|
|
770
|
+
at,
|
|
771
|
+
s.problems
|
|
772
|
+
);
|
|
773
|
+
assignText(line, "rateUnit", options.rateUnit, MAX_UNIT, at, s.problems);
|
|
774
|
+
assignText(line, "unit", options.unit, MAX_UNIT, at, s.problems);
|
|
775
|
+
const places = placesOf2(options.places, `${at}: places`, s.problems);
|
|
776
|
+
if (places !== void 0) line.places = places;
|
|
777
|
+
let kind = spec.kind ?? defaultKind(s);
|
|
778
|
+
if (!KINDS.has(kind)) {
|
|
779
|
+
s.problems.push(`${at}: unknown kind "${String(kind)}"`);
|
|
780
|
+
kind = defaultKind(s);
|
|
781
|
+
}
|
|
782
|
+
if (kind !== defaultKind(s)) line.kind = kind;
|
|
783
|
+
if (options.primary === true) line.primary = true;
|
|
784
|
+
const tags = tagsOf(options.tags, options.taxFree, at, s.problems);
|
|
785
|
+
if (tags !== void 0) line.tags = tags;
|
|
786
|
+
if (options.highlight !== void 0) {
|
|
787
|
+
if (HIGHLIGHTS.has(options.highlight)) line.highlight = options.highlight;
|
|
788
|
+
else
|
|
789
|
+
s.problems.push(
|
|
790
|
+
`${at}: unknown highlight "${String(options.highlight)}"`
|
|
791
|
+
);
|
|
792
|
+
}
|
|
793
|
+
assignText(line, "detail", options.detail, MAX_DETAIL, at, s.problems);
|
|
794
|
+
assignText(line, "explain", options.explain, MAX_DETAIL, at, s.problems);
|
|
795
|
+
assignText(line, "source", options.source, MAX_SOURCE, at, s.problems);
|
|
796
|
+
assignText(
|
|
797
|
+
line,
|
|
798
|
+
"sourceSpan",
|
|
799
|
+
options.sourceSpan,
|
|
800
|
+
MAX_SOURCE_SPAN,
|
|
801
|
+
at,
|
|
802
|
+
s.problems
|
|
803
|
+
);
|
|
804
|
+
const expected = expectedOf(options.expected, at, s.problems);
|
|
805
|
+
if (expected !== void 0) line.expected = expected;
|
|
806
|
+
const tolerance = toleranceOf(
|
|
807
|
+
options.tolerance,
|
|
808
|
+
`${at}: tolerance`,
|
|
809
|
+
s.problems
|
|
810
|
+
);
|
|
811
|
+
if (tolerance !== void 0) line.tolerance = tolerance;
|
|
812
|
+
const alerts = alertsOf(options.alerts, `${at}: alerts`, s.problems);
|
|
813
|
+
if (alerts !== void 0) line.alerts = alerts;
|
|
814
|
+
s.lines.push(line);
|
|
815
|
+
s.kinds.push(kind);
|
|
816
|
+
return key;
|
|
817
|
+
}
|
|
818
|
+
function addRaw(s, raw) {
|
|
819
|
+
if (typeof raw !== "object" || raw === null) {
|
|
820
|
+
s.problems.push(`${s.calcKey}: a line must be an object`);
|
|
821
|
+
return;
|
|
822
|
+
}
|
|
823
|
+
const { key, label, kind, formula, quantity, rate, times, ...options } = raw;
|
|
824
|
+
addLine(s, {
|
|
825
|
+
key,
|
|
826
|
+
label,
|
|
827
|
+
kind,
|
|
828
|
+
// Absent stays absent, so quantity × rate is still claimed.
|
|
829
|
+
...formula !== void 0 && { formula },
|
|
830
|
+
quantity,
|
|
831
|
+
rate,
|
|
832
|
+
times,
|
|
833
|
+
options
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
function addTiers(s, rawKey, options) {
|
|
837
|
+
const {
|
|
838
|
+
label: rawLabel,
|
|
839
|
+
quantity,
|
|
840
|
+
periods,
|
|
841
|
+
bands,
|
|
842
|
+
unit,
|
|
843
|
+
per,
|
|
844
|
+
overflow,
|
|
845
|
+
value,
|
|
846
|
+
tags,
|
|
847
|
+
taxFree,
|
|
848
|
+
...subtotalOptions
|
|
849
|
+
} = options;
|
|
850
|
+
const base = rawLabel ?? (rawKey !== void 0 ? titleCase(rawKey) : "Tiers");
|
|
851
|
+
const key = repairKey(
|
|
852
|
+
rawKey,
|
|
853
|
+
base,
|
|
854
|
+
s.taken,
|
|
855
|
+
s.problems,
|
|
856
|
+
`${s.calcKey}: tiers`
|
|
857
|
+
);
|
|
858
|
+
const at = `${s.calcKey}.${key}`;
|
|
859
|
+
if (!Array.isArray(bands) || bands.length === 0) {
|
|
860
|
+
s.problems.push(`${at}: tiers need at least one band`);
|
|
861
|
+
s.taken.delete(key);
|
|
862
|
+
return;
|
|
863
|
+
}
|
|
864
|
+
const q = operandOf(quantity, `${at}: quantity`, s.problems);
|
|
865
|
+
if (q === void 0 || q === null) {
|
|
866
|
+
s.problems.push(
|
|
867
|
+
q === null ? `${at}: the quantity wasn't calculated, so the tiers aren't sent` : `${at}: tiers need a quantity`
|
|
868
|
+
);
|
|
869
|
+
s.taken.delete(key);
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
const p = periods === void 0 ? void 0 : operandOf(periods, `${at}: periods`, s.problems) ?? void 0;
|
|
873
|
+
if (periods !== void 0 && p === void 0) {
|
|
874
|
+
s.problems.push(`${at}: periods wasn't calculated; the tiers use 1`);
|
|
875
|
+
}
|
|
876
|
+
const perText = per ? ` per ${per}` : "";
|
|
877
|
+
const unitText = unit ? ` ${unit}` : "";
|
|
878
|
+
let before = "0";
|
|
879
|
+
const charges = [];
|
|
880
|
+
bands.forEach((band, i) => {
|
|
881
|
+
const n = i + 1;
|
|
882
|
+
const last = i === bands.length - 1;
|
|
883
|
+
let size = band?.size === null || band?.size === void 0 ? null : sizeText(band.size, `${at}: band ${n} size`, s.problems);
|
|
884
|
+
if (last && overflow === "last") size = null;
|
|
885
|
+
if (size === null && !last) {
|
|
886
|
+
s.problems.push(`${at}: band ${n} has no size but isn't the last band`);
|
|
887
|
+
}
|
|
888
|
+
const remaining = before === "0" ? operandText(q) : `${operandText(q)} - ${before}`;
|
|
889
|
+
const amount = size === null ? `max(${remaining}, 0)` : `clamp(${remaining}, 0, ${size})`;
|
|
890
|
+
const formula = p === void 0 || p === 1 || p === "1" ? amount : `${amount} * ${operandText(p)}`;
|
|
891
|
+
const range = size === null ? before === "0" ? `all${unitText}${perText}` : `over ${before}${unitText}${perText}` : i === 0 ? `first ${size}${unitText}${perText}` : `next ${size}${unitText}${perText}`;
|
|
892
|
+
const bandLabel = band?.label ?? `${base} tier ${n} (${range})`;
|
|
893
|
+
const bandKey = `${key}_${n}`;
|
|
894
|
+
const tooLong = formula.length > MAX_FORMULA;
|
|
895
|
+
if (tooLong) {
|
|
896
|
+
s.problems.push(
|
|
897
|
+
`${s.calcKey}.${bandKey}: quantity: the formula is ${formula.length} characters, over ${MAX_FORMULA}; the band isn't calculated`
|
|
898
|
+
);
|
|
899
|
+
}
|
|
900
|
+
charges.push(
|
|
901
|
+
addLine(s, {
|
|
902
|
+
key: bandKey,
|
|
903
|
+
label: bandLabel,
|
|
904
|
+
kind: "charge",
|
|
905
|
+
quantity: tooLong ? null : formula,
|
|
906
|
+
rate: band?.rate,
|
|
907
|
+
options: {
|
|
908
|
+
...band?.value !== void 0 && { value: band.value },
|
|
909
|
+
...unit !== void 0 && { quantityUnit: unit },
|
|
910
|
+
...subtotalOptions.rateUnit !== void 0 && {
|
|
911
|
+
rateUnit: subtotalOptions.rateUnit
|
|
912
|
+
},
|
|
913
|
+
tags,
|
|
914
|
+
taxFree
|
|
915
|
+
}
|
|
916
|
+
})
|
|
917
|
+
);
|
|
918
|
+
if (size !== null) before = addDecimals(before, size);
|
|
919
|
+
});
|
|
920
|
+
s.taken.delete(key);
|
|
921
|
+
const { rateUnit: _rateUnit, ...rest } = subtotalOptions;
|
|
922
|
+
addLine(s, {
|
|
923
|
+
key,
|
|
924
|
+
label: base,
|
|
925
|
+
kind: "subtotal",
|
|
926
|
+
formula: `sum(${charges.join(", ")})`,
|
|
927
|
+
options: { ...rest, value }
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
function addPercentOf(s, key, label, options) {
|
|
931
|
+
const { of, rate, ...rest } = options;
|
|
932
|
+
const keys = (Array.isArray(of) ? of : [of]).filter(
|
|
933
|
+
(k) => typeof k === "string" && k !== ""
|
|
934
|
+
);
|
|
935
|
+
if (keys.length === 0) {
|
|
936
|
+
s.problems.push(
|
|
937
|
+
`${s.calcKey}: percentOf "${label}" needs the keys it's a percentage of`
|
|
938
|
+
);
|
|
939
|
+
}
|
|
940
|
+
addLine(s, {
|
|
941
|
+
key,
|
|
942
|
+
label,
|
|
943
|
+
kind: "charge",
|
|
944
|
+
// With nothing to take a percentage of, there's no quantity, so the
|
|
945
|
+
// line is stated.
|
|
946
|
+
...keys.length > 0 && {
|
|
947
|
+
quantity: keys.length === 1 ? keys[0] : `sum(${keys.join(", ")})`
|
|
948
|
+
},
|
|
949
|
+
rate,
|
|
950
|
+
options: {
|
|
951
|
+
rateUnit: "%",
|
|
952
|
+
...keys.length !== 1 && s.currency !== void 0 && { quantityUnit: s.currency },
|
|
953
|
+
...rest
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
}
|
|
957
|
+
function addTaxIncluded(s, key, label, options) {
|
|
958
|
+
const { of, tagged, rate, ...rest } = options;
|
|
959
|
+
const at = `${s.calcKey}: taxIncluded "${label}"`;
|
|
960
|
+
let base;
|
|
961
|
+
if (tagged !== void 0) {
|
|
962
|
+
base = `sum_tagged('${String(tagged).replace(/'/g, "")}')`;
|
|
963
|
+
} else {
|
|
964
|
+
const keys = of !== void 0 ? (Array.isArray(of) ? of : [of]).filter((k) => typeof k === "string") : s.lines.filter(
|
|
965
|
+
(line, i) => s.kinds[i] === "charge" && !(line.tags ?? []).includes("tax_free")
|
|
966
|
+
).map((line) => line.key);
|
|
967
|
+
if (keys.length === 0) {
|
|
968
|
+
s.problems.push(`${at}: no lines to take the tax from`);
|
|
969
|
+
} else {
|
|
970
|
+
base = keys.length === 1 ? keys[0] : `sum(${keys.join(", ")})`;
|
|
971
|
+
const onlyCharges = s.lines.every(
|
|
972
|
+
(line, i) => !(line.tags ?? []).includes("tax_free") || s.kinds[i] === "charge"
|
|
973
|
+
);
|
|
974
|
+
if (of === void 0 && base.length > MAX_FORMULA - 40 && onlyCharges) {
|
|
975
|
+
base = `(sum_kind('charge') - sum_tagged('tax_free'))`;
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
if (rate === void 0) {
|
|
980
|
+
s.problems.push(`${at}: needs a rate, such as 0.1`);
|
|
981
|
+
}
|
|
982
|
+
const r = operandOf(rate, `${at}: rate`, s.problems);
|
|
983
|
+
let formula;
|
|
984
|
+
if (base !== void 0 && r !== void 0 && r !== null) {
|
|
985
|
+
const rateText = typeof r === "number" ? decimalText(r) : r;
|
|
986
|
+
const onePlus = isDecimalText(rateText) ? addDecimals("1", rateText) : `(1 + ${operandText(r)})`;
|
|
987
|
+
formula = `${base} * ${operandText(r)} / ${onePlus}`;
|
|
988
|
+
}
|
|
989
|
+
addLine(s, {
|
|
990
|
+
key,
|
|
991
|
+
label,
|
|
992
|
+
kind: "info",
|
|
993
|
+
formula: formula ?? null,
|
|
994
|
+
// A rate the engine doesn't have: the tax wasn't calculated either.
|
|
995
|
+
options: r === null && rest.value === void 0 ? { ...rest, value: null } : rest
|
|
996
|
+
});
|
|
997
|
+
}
|
|
998
|
+
function checkFormulas(s) {
|
|
999
|
+
const lineIndex = new Map(s.lines.map((l, i) => [l.key, i]));
|
|
1000
|
+
s.lines.forEach((line, i) => {
|
|
1001
|
+
const at = `${s.calcKey}.${line.key}`;
|
|
1002
|
+
const formulas = [];
|
|
1003
|
+
if (typeof line.formula === "string")
|
|
1004
|
+
formulas.push(["formula", line.formula]);
|
|
1005
|
+
for (const field of ["quantity", "rate", "times"]) {
|
|
1006
|
+
const v = line[field];
|
|
1007
|
+
if (typeof v === "string") formulas.push([field, v]);
|
|
1008
|
+
}
|
|
1009
|
+
for (const [field, formula] of formulas) {
|
|
1010
|
+
for (const problem of formulaProblems(formula, (name) => {
|
|
1011
|
+
if (s.inputKeys.has(name)) return void 0;
|
|
1012
|
+
const j = lineIndex.get(name);
|
|
1013
|
+
if (j === void 0) return `names an unknown key "${name}"`;
|
|
1014
|
+
if (j === i) return `refers to its own line`;
|
|
1015
|
+
if (j > i) return `refers to "${name}", a later line`;
|
|
1016
|
+
return void 0;
|
|
1017
|
+
})) {
|
|
1018
|
+
s.problems.push(
|
|
1019
|
+
`${at}: ${field === "formula" ? "" : `${field} `}${problem}`
|
|
1020
|
+
);
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
});
|
|
1024
|
+
}
|
|
1025
|
+
function formulaProblems(formula, checkKey = () => void 0) {
|
|
1026
|
+
const problems = [];
|
|
1027
|
+
let depth = 0;
|
|
1028
|
+
let i = 0;
|
|
1029
|
+
const src = formula;
|
|
1030
|
+
while (i < src.length) {
|
|
1031
|
+
const ch = src[i];
|
|
1032
|
+
if (/\s/.test(ch)) {
|
|
1033
|
+
i++;
|
|
1034
|
+
continue;
|
|
1035
|
+
}
|
|
1036
|
+
if (ch === "'" || ch === '"') {
|
|
1037
|
+
const end = src.indexOf(ch, i + 1);
|
|
1038
|
+
if (end === -1) {
|
|
1039
|
+
problems.push("has an unclosed quote");
|
|
1040
|
+
break;
|
|
1041
|
+
}
|
|
1042
|
+
i = end + 1;
|
|
1043
|
+
continue;
|
|
1044
|
+
}
|
|
1045
|
+
const number = /^(\d+(\.\d+)?|\.\d+)/.exec(src.slice(i));
|
|
1046
|
+
if (number) {
|
|
1047
|
+
i += number[0].length;
|
|
1048
|
+
if (/[A-Za-z0-9_.%]/.test(src[i] ?? "")) {
|
|
1049
|
+
problems.push(
|
|
1050
|
+
`has "${src[i]}" after a number; numbers are plain decimals, like 0.1`
|
|
1051
|
+
);
|
|
1052
|
+
}
|
|
1053
|
+
continue;
|
|
1054
|
+
}
|
|
1055
|
+
const ident = /^[A-Za-z_][A-Za-z0-9_]*/.exec(src.slice(i));
|
|
1056
|
+
if (ident) {
|
|
1057
|
+
const name = ident[0];
|
|
1058
|
+
i += name.length;
|
|
1059
|
+
const rest = src.slice(i).trimStart();
|
|
1060
|
+
if (rest.startsWith("(")) {
|
|
1061
|
+
if (!FUNCTIONS.has(name) && !WORDS.has(name)) {
|
|
1062
|
+
problems.push(`uses an unknown function "${name}"`);
|
|
1063
|
+
}
|
|
1064
|
+
} else if (!WORDS.has(name)) {
|
|
1065
|
+
const problem = checkKey(name);
|
|
1066
|
+
if (problem) problems.push(problem);
|
|
1067
|
+
}
|
|
1068
|
+
continue;
|
|
1069
|
+
}
|
|
1070
|
+
if (ch === "(") depth++;
|
|
1071
|
+
else if (ch === ")") {
|
|
1072
|
+
depth--;
|
|
1073
|
+
if (depth < 0) {
|
|
1074
|
+
problems.push('has a ")" with no "(" before it');
|
|
1075
|
+
depth = 0;
|
|
1076
|
+
}
|
|
1077
|
+
} else if (!"+-*/^,<>=".includes(ch)) {
|
|
1078
|
+
problems.push(`has an unexpected "${ch}"`);
|
|
1079
|
+
}
|
|
1080
|
+
i++;
|
|
1081
|
+
}
|
|
1082
|
+
if (depth > 0) problems.push('has an unclosed "("');
|
|
1083
|
+
return [...new Set(problems)];
|
|
1084
|
+
}
|
|
1085
|
+
function cleanValue(value, at, problems) {
|
|
1086
|
+
if (value === void 0) return void 0;
|
|
1087
|
+
if (value === null) return null;
|
|
1088
|
+
if (typeof value === "number") {
|
|
1089
|
+
if (Number.isNaN(value)) return null;
|
|
1090
|
+
if (!Number.isFinite(value)) {
|
|
1091
|
+
problems.push(
|
|
1092
|
+
`${at}: ${value} can't be sent; sent as null. Send text such as 'Unlimited' instead`
|
|
1093
|
+
);
|
|
1094
|
+
return null;
|
|
1095
|
+
}
|
|
1096
|
+
return Object.is(value, -0) ? 0 : value;
|
|
1097
|
+
}
|
|
1098
|
+
if (typeof value === "string") {
|
|
1099
|
+
return text(value, MAX_TEXT_VALUE, at, problems);
|
|
1100
|
+
}
|
|
1101
|
+
if (typeof value === "boolean") return value;
|
|
1102
|
+
if (typeof value === "bigint") return value.toString();
|
|
1103
|
+
if (typeof value === "object" && value.__redacted === true) {
|
|
1104
|
+
return REDACTED;
|
|
1105
|
+
}
|
|
1106
|
+
const decimal = decimalObjectText(value);
|
|
1107
|
+
if (decimal !== void 0) return decimal;
|
|
1108
|
+
problems.push(`${at}: a ${typeof value} isn't a value; left out`);
|
|
1109
|
+
return void 0;
|
|
1110
|
+
}
|
|
1111
|
+
function operandOf(value, at, problems) {
|
|
1112
|
+
if (value === void 0) return void 0;
|
|
1113
|
+
if (value === null) return null;
|
|
1114
|
+
if (typeof value === "number") {
|
|
1115
|
+
if (Number.isNaN(value)) return null;
|
|
1116
|
+
if (!Number.isFinite(value)) {
|
|
1117
|
+
problems.push(`${at}: ${value} can't be sent; sent as not calculated`);
|
|
1118
|
+
return null;
|
|
1119
|
+
}
|
|
1120
|
+
return Object.is(value, -0) ? 0 : value;
|
|
1121
|
+
}
|
|
1122
|
+
if (typeof value === "string") return formulaText(value, at, problems);
|
|
1123
|
+
const decimal = decimalObjectText(value);
|
|
1124
|
+
if (decimal !== void 0) return decimal;
|
|
1125
|
+
problems.push(`${at}: should be a number or a formula`);
|
|
1126
|
+
return void 0;
|
|
1127
|
+
}
|
|
1128
|
+
function formulaText(formula, at, problems) {
|
|
1129
|
+
const trimmed = formula.trim();
|
|
1130
|
+
if (trimmed === "") {
|
|
1131
|
+
problems.push(`${at}: the formula is empty`);
|
|
1132
|
+
return void 0;
|
|
1133
|
+
}
|
|
1134
|
+
if (trimmed.length > MAX_FORMULA) {
|
|
1135
|
+
problems.push(
|
|
1136
|
+
`${at}: the formula is ${trimmed.length} characters, over ${MAX_FORMULA}; sent as stated`
|
|
1137
|
+
);
|
|
1138
|
+
return void 0;
|
|
1139
|
+
}
|
|
1140
|
+
return trimmed;
|
|
1141
|
+
}
|
|
1142
|
+
function sizeText(value, at, problems) {
|
|
1143
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0) {
|
|
1144
|
+
return decimalText(value);
|
|
1145
|
+
}
|
|
1146
|
+
const text2 = typeof value === "string" ? value.trim() : decimalObjectText(value);
|
|
1147
|
+
if (text2 !== void 0 && isDecimalText(text2) && !text2.startsWith("-")) {
|
|
1148
|
+
return text2;
|
|
1149
|
+
}
|
|
1150
|
+
problems.push(
|
|
1151
|
+
`${at}: a band size is a number of 0 or more; treated as the rest`
|
|
1152
|
+
);
|
|
1153
|
+
return null;
|
|
1154
|
+
}
|
|
1155
|
+
function operandText(value) {
|
|
1156
|
+
if (typeof value === "number") {
|
|
1157
|
+
const t = decimalText(value);
|
|
1158
|
+
return value < 0 ? `(${t})` : t;
|
|
1159
|
+
}
|
|
1160
|
+
return /^([a-z][a-z0-9_]*|\d+(\.\d+)?)$/.test(value) ? value : `(${value})`;
|
|
1161
|
+
}
|
|
1162
|
+
function text(value, max, at, problems) {
|
|
1163
|
+
if (value === void 0 || value === null) return void 0;
|
|
1164
|
+
const s = typeof value === "string" ? value : String(value);
|
|
1165
|
+
if (s.length > max) {
|
|
1166
|
+
problems.push(`${at}: longer than ${max} characters; cut short`);
|
|
1167
|
+
return s.slice(0, max - 1) + "\u2026";
|
|
1168
|
+
}
|
|
1169
|
+
return s;
|
|
1170
|
+
}
|
|
1171
|
+
function assignText(target, field, value, max, at, problems) {
|
|
1172
|
+
if (value === void 0 || value === null) return;
|
|
1173
|
+
const t = text(value, max, `${at}: ${field}`, problems);
|
|
1174
|
+
if (t === void 0 || t === "" && max === MAX_UNIT) return;
|
|
1175
|
+
target[field] = t;
|
|
1176
|
+
}
|
|
1177
|
+
function placesOf2(value, at, problems) {
|
|
1178
|
+
if (value === void 0) return void 0;
|
|
1179
|
+
if (typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= 6) {
|
|
1180
|
+
return value;
|
|
1181
|
+
}
|
|
1182
|
+
problems.push(`${at}: places is a whole number from 0 to 6; left out`);
|
|
1183
|
+
return void 0;
|
|
1184
|
+
}
|
|
1185
|
+
function toleranceOf(value, at, problems) {
|
|
1186
|
+
if (value === void 0) return void 0;
|
|
1187
|
+
if (typeof value === "number" && Number.isFinite(value) && value >= 0)
|
|
1188
|
+
return value;
|
|
1189
|
+
if (typeof value === "string" && /^\d+(\.\d+)?%$/.test(value)) {
|
|
1190
|
+
return value;
|
|
1191
|
+
}
|
|
1192
|
+
problems.push(
|
|
1193
|
+
`${at}: a tolerance is a number of 0 or more, or a percentage like "0.1%"; left out`
|
|
1194
|
+
);
|
|
1195
|
+
return void 0;
|
|
1196
|
+
}
|
|
1197
|
+
function alertsOf(value, at, problems) {
|
|
1198
|
+
if (value === void 0) return void 0;
|
|
1199
|
+
if (value === "none" || value === "warn" || value === "error") return value;
|
|
1200
|
+
problems.push(`${at}: alerts is 'none', 'warn' or 'error'; left out`);
|
|
1201
|
+
return void 0;
|
|
1202
|
+
}
|
|
1203
|
+
function currencyCode(value, at, problems) {
|
|
1204
|
+
if (value === void 0) return void 0;
|
|
1205
|
+
if (typeof value === "string" && /^[A-Za-z]{3}$/.test(value)) {
|
|
1206
|
+
return value.toUpperCase();
|
|
1207
|
+
}
|
|
1208
|
+
problems.push(`${at}: a currency is an ISO 4217 code, such as AUD; left out`);
|
|
1209
|
+
return void 0;
|
|
1210
|
+
}
|
|
1211
|
+
function tagsOf(tags, taxFree, at, problems) {
|
|
1212
|
+
const out = [];
|
|
1213
|
+
for (const tag of Array.isArray(tags) ? tags : []) {
|
|
1214
|
+
if (typeof tag !== "string" || tag === "") continue;
|
|
1215
|
+
const t = text(tag, MAX_TAG, `${at}: tag`, problems);
|
|
1216
|
+
if (!out.includes(t)) out.push(t);
|
|
1217
|
+
}
|
|
1218
|
+
if (taxFree === true && !out.includes("tax_free")) out.push("tax_free");
|
|
1219
|
+
if (out.length > MAX_TAGS) {
|
|
1220
|
+
problems.push(
|
|
1221
|
+
`${at}: ${out.length} tags; only the first ${MAX_TAGS} are sent`
|
|
1222
|
+
);
|
|
1223
|
+
out.length = MAX_TAGS;
|
|
1224
|
+
}
|
|
1225
|
+
return out.length > 0 ? out : void 0;
|
|
1226
|
+
}
|
|
1227
|
+
function expectedOf(value, at, problems) {
|
|
1228
|
+
if (value === void 0 || value === null) return void 0;
|
|
1229
|
+
if (typeof value !== "object") {
|
|
1230
|
+
problems.push(`${at}: expected should be an object; left out`);
|
|
1231
|
+
return void 0;
|
|
1232
|
+
}
|
|
1233
|
+
const v = cleanValue(value.value, `${at}: expected`, problems);
|
|
1234
|
+
if (v === void 0) {
|
|
1235
|
+
problems.push(`${at}: expected needs a value; left out`);
|
|
1236
|
+
return void 0;
|
|
1237
|
+
}
|
|
1238
|
+
const out = { value: v };
|
|
1239
|
+
assignText(
|
|
1240
|
+
out,
|
|
1241
|
+
"source",
|
|
1242
|
+
value.source,
|
|
1243
|
+
MAX_SOURCE,
|
|
1244
|
+
`${at}: expected`,
|
|
1245
|
+
problems
|
|
1246
|
+
);
|
|
1247
|
+
const tolerance = toleranceOf(
|
|
1248
|
+
value.tolerance,
|
|
1249
|
+
`${at}: expected tolerance`,
|
|
1250
|
+
problems
|
|
1251
|
+
);
|
|
1252
|
+
if (tolerance !== void 0) out.tolerance = tolerance;
|
|
1253
|
+
if (value.level === "warn" || value.level === "error")
|
|
1254
|
+
out.level = value.level;
|
|
1255
|
+
else if (value.level !== void 0) {
|
|
1256
|
+
problems.push(`${at}: expected level is 'warn' or 'error'; left out`);
|
|
1257
|
+
}
|
|
1258
|
+
assignText(
|
|
1259
|
+
out,
|
|
1260
|
+
"reason",
|
|
1261
|
+
value.reason,
|
|
1262
|
+
MAX_DETAIL,
|
|
1263
|
+
`${at}: expected`,
|
|
1264
|
+
problems
|
|
1265
|
+
);
|
|
1266
|
+
return out;
|
|
1267
|
+
}
|
|
1268
|
+
function columnLabelsOf(value, at, problems) {
|
|
1269
|
+
if (value === void 0 || value === null || typeof value !== "object")
|
|
1270
|
+
return void 0;
|
|
1271
|
+
const out = {};
|
|
1272
|
+
for (const [k, v] of Object.entries(value)) {
|
|
1273
|
+
if (!COLUMN_LABELS.has(k)) {
|
|
1274
|
+
problems.push(`${at}: unknown column "${k}"`);
|
|
1275
|
+
continue;
|
|
1276
|
+
}
|
|
1277
|
+
const t = text(v, MAX_LABEL, `${at}.${k}`, problems);
|
|
1278
|
+
if (t !== void 0) out[k] = t;
|
|
1279
|
+
}
|
|
1280
|
+
return Object.keys(out).length > 0 ? out : void 0;
|
|
1281
|
+
}
|
|
1282
|
+
function summaryOf(value, at, problems) {
|
|
1283
|
+
if (!Array.isArray(value) || value.length === 0) return void 0;
|
|
1284
|
+
const out = [];
|
|
1285
|
+
for (const stat of value.slice(0, MAX_SUMMARY)) {
|
|
1286
|
+
if (!stat || typeof stat !== "object") continue;
|
|
1287
|
+
const t = text(
|
|
1288
|
+
stat.text,
|
|
1289
|
+
MAX_SUMMARY_TEXT,
|
|
1290
|
+
at,
|
|
1291
|
+
problems
|
|
1292
|
+
);
|
|
1293
|
+
if (t === void 0) continue;
|
|
1294
|
+
const highlight = stat.highlight;
|
|
1295
|
+
out.push({
|
|
1296
|
+
text: t,
|
|
1297
|
+
...highlight !== void 0 && HIGHLIGHTS.has(highlight) && { highlight }
|
|
1298
|
+
});
|
|
1299
|
+
}
|
|
1300
|
+
if (value.length > MAX_SUMMARY) {
|
|
1301
|
+
problems.push(
|
|
1302
|
+
`${at}: ${value.length} stats; only the first ${MAX_SUMMARY} are sent`
|
|
1303
|
+
);
|
|
1304
|
+
}
|
|
1305
|
+
return out.length > 0 ? out : void 0;
|
|
1306
|
+
}
|
|
1307
|
+
function slugify(text2) {
|
|
1308
|
+
const slug = text2.normalize("NFKD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[^a-z0-9]+/g, "_").replace(/^_+|_+$/g, "").slice(0, MAX_KEY - 4).replace(/_+$/, "");
|
|
1309
|
+
if (slug === "") return "";
|
|
1310
|
+
return /^[a-z]/.test(slug) ? slug : `k_${slug}`.slice(0, MAX_KEY - 4);
|
|
1311
|
+
}
|
|
1312
|
+
function titleCase(key) {
|
|
1313
|
+
const words = String(key).replace(/_+/g, " ").trim();
|
|
1314
|
+
return words.charAt(0).toUpperCase() + words.slice(1);
|
|
1315
|
+
}
|
|
1316
|
+
function repairKey(given, label, taken, problems, at) {
|
|
1317
|
+
let base;
|
|
1318
|
+
if (typeof given === "string" && given !== "") {
|
|
1319
|
+
if (isValidKey(given)) {
|
|
1320
|
+
base = given;
|
|
1321
|
+
} else {
|
|
1322
|
+
base = slugify(given) || "line";
|
|
1323
|
+
if (RESERVED.has(base)) base = `${base}_1`;
|
|
1324
|
+
problems.push(
|
|
1325
|
+
`${at}: "${given}" isn't a valid key; sent as "${unique(base, taken)}"`
|
|
1326
|
+
);
|
|
1327
|
+
}
|
|
1328
|
+
if (taken.has(base)) {
|
|
1329
|
+
const key2 = unique(base, taken);
|
|
1330
|
+
if (isValidKey(given)) {
|
|
1331
|
+
problems.push(
|
|
1332
|
+
`${at}: the key "${given}" is used more than once; sent as "${key2}"`
|
|
1333
|
+
);
|
|
1334
|
+
}
|
|
1335
|
+
taken.add(key2);
|
|
1336
|
+
return key2;
|
|
1337
|
+
}
|
|
1338
|
+
} else {
|
|
1339
|
+
base = slugify(label) || "line";
|
|
1340
|
+
if (RESERVED.has(base)) base = `${base}_1`;
|
|
1341
|
+
}
|
|
1342
|
+
const key = unique(base, taken);
|
|
1343
|
+
taken.add(key);
|
|
1344
|
+
return key;
|
|
1345
|
+
}
|
|
1346
|
+
function unique(base, taken) {
|
|
1347
|
+
if (!taken.has(base)) return base;
|
|
1348
|
+
for (let n = 2; ; n++) {
|
|
1349
|
+
const key = `${base}_${n}`;
|
|
1350
|
+
if (!taken.has(key)) return key;
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
function buildCalculations(calcs) {
|
|
1354
|
+
const problems = [];
|
|
1355
|
+
const list = Array.isArray(calcs) ? calcs : [calcs];
|
|
1356
|
+
const calculations = [];
|
|
1357
|
+
const keys = /* @__PURE__ */ new Set();
|
|
1358
|
+
let rows = 0;
|
|
1359
|
+
for (const item of list) {
|
|
1360
|
+
let built;
|
|
1361
|
+
try {
|
|
1362
|
+
built = (isBuilder(item) ? item : fromPlain(item)).compile();
|
|
1363
|
+
} catch {
|
|
1364
|
+
problems.push("a calculation couldn't be built and was left out");
|
|
1365
|
+
continue;
|
|
1366
|
+
}
|
|
1367
|
+
problems.push(...built.problems);
|
|
1368
|
+
const calc = built.calculation;
|
|
1369
|
+
if (!calc) continue;
|
|
1370
|
+
if (calculations.length === MAX_CALCULATIONS) {
|
|
1371
|
+
problems.push(
|
|
1372
|
+
`${calc.key}: a table side holds ${MAX_CALCULATIONS} calculations; left out`
|
|
1373
|
+
);
|
|
1374
|
+
continue;
|
|
1375
|
+
}
|
|
1376
|
+
const drawn = calc.lines.length + (calc.notes?.length ?? 0);
|
|
1377
|
+
if (rows + drawn > MAX_ROWS) {
|
|
1378
|
+
problems.push(
|
|
1379
|
+
`${calc.key}: would draw more than ${MAX_ROWS} rows on one table side; left out`
|
|
1380
|
+
);
|
|
1381
|
+
continue;
|
|
1382
|
+
}
|
|
1383
|
+
if (keys.has(calc.key)) {
|
|
1384
|
+
const key = unique(calc.key, keys);
|
|
1385
|
+
problems.push(
|
|
1386
|
+
`${calc.key}: two calculations share this key; sent as "${key}"`
|
|
1387
|
+
);
|
|
1388
|
+
calculations.push({ ...calc, key });
|
|
1389
|
+
keys.add(key);
|
|
1390
|
+
} else {
|
|
1391
|
+
calculations.push(calc);
|
|
1392
|
+
keys.add(calc.key);
|
|
1393
|
+
}
|
|
1394
|
+
rows += drawn;
|
|
1395
|
+
}
|
|
1396
|
+
return { calculations, problems };
|
|
1397
|
+
}
|
|
1398
|
+
function isBuilder(item) {
|
|
1399
|
+
return typeof item === "object" && item !== null && typeof item.compile === "function";
|
|
1400
|
+
}
|
|
1401
|
+
function fromPlain(calc) {
|
|
1402
|
+
if (!calc || typeof calc !== "object") {
|
|
1403
|
+
return new CalculationBuilder("calculation");
|
|
1404
|
+
}
|
|
1405
|
+
const { key, inputs, lines, notes, version: _v, ...options } = calc;
|
|
1406
|
+
const b = new CalculationBuilder(key, options);
|
|
1407
|
+
for (const input of Array.isArray(inputs) ? inputs : []) {
|
|
1408
|
+
if (!input || typeof input !== "object") continue;
|
|
1409
|
+
const { key: k, value, ...rest } = input;
|
|
1410
|
+
b.input(k, value, rest);
|
|
1411
|
+
}
|
|
1412
|
+
for (const line of Array.isArray(lines) ? lines : []) {
|
|
1413
|
+
b.line(line);
|
|
1414
|
+
}
|
|
1415
|
+
for (const note of Array.isArray(notes) ? notes : []) {
|
|
1416
|
+
b.note(note);
|
|
1417
|
+
}
|
|
1418
|
+
return b;
|
|
1419
|
+
}
|
|
1420
|
+
var units = {
|
|
1421
|
+
/** A fraction shown as a percentage: `0.03` is `3%`. */
|
|
1422
|
+
percent: "%",
|
|
1423
|
+
/** Counts: singular at 1 ("1 day"), and per one in a rate ("$0.67/day"). */
|
|
1424
|
+
days: "days",
|
|
1425
|
+
months: "months",
|
|
1426
|
+
years: "years",
|
|
1427
|
+
hours: "hours",
|
|
1428
|
+
/** A rate per something: `units.per('AUD', 'kWh')` is `'AUD/kWh'`. */
|
|
1429
|
+
per: (unit, per) => `${unit}/${per}`
|
|
1430
|
+
};
|
|
1431
|
+
|
|
286
1432
|
// src/trace.ts
|
|
287
1433
|
var NOOP_RESULT = { traceId: "" };
|
|
1434
|
+
function calculationSpanOptions(calcs, options, log, name) {
|
|
1435
|
+
const { side, ...rest } = options ?? {};
|
|
1436
|
+
const { calculations, problems } = buildCalculations(calcs);
|
|
1437
|
+
for (const problem of problems) {
|
|
1438
|
+
log(`calculation span=${name} ${problem}`);
|
|
1439
|
+
}
|
|
1440
|
+
if (calculations.length === 0) {
|
|
1441
|
+
return rest;
|
|
1442
|
+
}
|
|
1443
|
+
const data = { calculations };
|
|
1444
|
+
return side === "input" ? { ...rest, input: data } : { ...rest, output: data };
|
|
1445
|
+
}
|
|
288
1446
|
function reservoirSample(items, k) {
|
|
289
1447
|
if (k >= items.length) return items.slice();
|
|
290
1448
|
const indexed = items.map((item, i) => ({ item, i, key: Math.random() }));
|
|
@@ -303,8 +1461,19 @@ var SpanGroup = class {
|
|
|
303
1461
|
__privateSet(this, _lastSpan, item);
|
|
304
1462
|
return this;
|
|
305
1463
|
}
|
|
306
|
-
addMessage(name,
|
|
307
|
-
return this.span("message", name, { text, ...opts });
|
|
1464
|
+
addMessage(name, text2, opts) {
|
|
1465
|
+
return this.span("message", name, { text: text2, ...opts });
|
|
1466
|
+
}
|
|
1467
|
+
/** A table span holding these calculations, as a child of the group. */
|
|
1468
|
+
calculation(name, calcs, options) {
|
|
1469
|
+
const item = {
|
|
1470
|
+
type: "table",
|
|
1471
|
+
name,
|
|
1472
|
+
calculations: (log) => calculationSpanOptions(calcs, options, log, name)
|
|
1473
|
+
};
|
|
1474
|
+
__privateGet(this, _buffer).push(item);
|
|
1475
|
+
__privateSet(this, _lastSpan, item);
|
|
1476
|
+
return this;
|
|
308
1477
|
}
|
|
309
1478
|
alert(label, level, options) {
|
|
310
1479
|
if (!__privateGet(this, _lastSpan)) return this;
|
|
@@ -320,10 +1489,11 @@ var SpanGroup = class {
|
|
|
320
1489
|
};
|
|
321
1490
|
return this;
|
|
322
1491
|
}
|
|
323
|
-
_flush(push, options) {
|
|
1492
|
+
_flush(push, options, log = () => {
|
|
1493
|
+
}) {
|
|
324
1494
|
const { limit, sample, sampleThreshold } = options ?? {};
|
|
325
1495
|
let items = __privateGet(this, _buffer).filter(
|
|
326
|
-
({ type, opts }) => type === "group" || type === "message" || opts?.input !== void 0 || opts?.output !== void 0
|
|
1496
|
+
({ type, opts, calculations }) => type === "group" || type === "message" || calculations !== void 0 || opts?.input !== void 0 || opts?.output !== void 0
|
|
327
1497
|
);
|
|
328
1498
|
if (sample !== void 0) {
|
|
329
1499
|
const threshold = sampleThreshold ?? sample;
|
|
@@ -333,21 +1503,33 @@ var SpanGroup = class {
|
|
|
333
1503
|
} else if (limit !== void 0) {
|
|
334
1504
|
items = items.slice(0, limit);
|
|
335
1505
|
}
|
|
336
|
-
for (const { type, name, opts } of items) {
|
|
337
|
-
|
|
1506
|
+
for (const { type, name, opts, calculations } of items) {
|
|
1507
|
+
if (calculations) {
|
|
1508
|
+
let built = {};
|
|
1509
|
+
try {
|
|
1510
|
+
built = calculations(log);
|
|
1511
|
+
} catch {
|
|
1512
|
+
}
|
|
1513
|
+
push(type, name, {
|
|
1514
|
+
...built,
|
|
1515
|
+
...opts?.alerts && { alerts: opts.alerts }
|
|
1516
|
+
});
|
|
1517
|
+
} else {
|
|
1518
|
+
push(type, name, opts);
|
|
1519
|
+
}
|
|
338
1520
|
}
|
|
339
1521
|
}
|
|
340
1522
|
};
|
|
341
1523
|
_buffer = new WeakMap();
|
|
342
1524
|
_lastSpan = new WeakMap();
|
|
343
|
-
var _client, _name,
|
|
1525
|
+
var _client, _name, _options2, _spans, _startTime, _disabled, _log, _metadata, _environment, _lastSpan2, _Trace_instances, pushSpan_fn, applyRedaction_fn;
|
|
344
1526
|
var Trace = class {
|
|
345
1527
|
constructor(client, name, options = {}, disabled = false, log = () => {
|
|
346
1528
|
}) {
|
|
347
1529
|
__privateAdd(this, _Trace_instances);
|
|
348
1530
|
__privateAdd(this, _client);
|
|
349
1531
|
__privateAdd(this, _name);
|
|
350
|
-
__privateAdd(this,
|
|
1532
|
+
__privateAdd(this, _options2);
|
|
351
1533
|
__privateAdd(this, _spans, []);
|
|
352
1534
|
__privateAdd(this, _startTime);
|
|
353
1535
|
__privateAdd(this, _disabled);
|
|
@@ -357,7 +1539,7 @@ var Trace = class {
|
|
|
357
1539
|
__privateAdd(this, _lastSpan2);
|
|
358
1540
|
__privateSet(this, _client, client);
|
|
359
1541
|
__privateSet(this, _name, name);
|
|
360
|
-
__privateSet(this,
|
|
1542
|
+
__privateSet(this, _options2, options);
|
|
361
1543
|
__privateSet(this, _startTime, Date.now());
|
|
362
1544
|
__privateSet(this, _disabled, disabled);
|
|
363
1545
|
__privateSet(this, _log, log);
|
|
@@ -390,7 +1572,8 @@ var Trace = class {
|
|
|
390
1572
|
const spanCountBeforeFlush = __privateGet(this, _spans).length;
|
|
391
1573
|
g._flush(
|
|
392
1574
|
(type, spanName, opts) => __privateMethod(this, _Trace_instances, pushSpan_fn).call(this, type, spanName, opts, groupOrder),
|
|
393
|
-
options
|
|
1575
|
+
options,
|
|
1576
|
+
__privateGet(this, _log)
|
|
394
1577
|
);
|
|
395
1578
|
if (__privateGet(this, _spans).length === spanCountBeforeFlush) {
|
|
396
1579
|
__privateGet(this, _spans).splice(groupOrder, 1);
|
|
@@ -413,8 +1596,19 @@ var Trace = class {
|
|
|
413
1596
|
__privateGet(this, _lastSpan2).alerts = [...__privateGet(this, _lastSpan2).alerts ?? [], entry];
|
|
414
1597
|
return this;
|
|
415
1598
|
}
|
|
416
|
-
addMessage(name,
|
|
417
|
-
return this.span("message", name, { text, ...opts });
|
|
1599
|
+
addMessage(name, text2, opts) {
|
|
1600
|
+
return this.span("message", name, { text: text2, ...opts });
|
|
1601
|
+
}
|
|
1602
|
+
/** A table span holding these calculations. Pass builders without `.build()`. */
|
|
1603
|
+
calculation(name, calcs, options) {
|
|
1604
|
+
if (__privateGet(this, _disabled)) return this;
|
|
1605
|
+
let opts;
|
|
1606
|
+
try {
|
|
1607
|
+
opts = calculationSpanOptions(calcs, options, __privateGet(this, _log), name);
|
|
1608
|
+
} catch {
|
|
1609
|
+
return this;
|
|
1610
|
+
}
|
|
1611
|
+
return this.span("table", name, opts);
|
|
418
1612
|
}
|
|
419
1613
|
addMetadata(data) {
|
|
420
1614
|
Object.assign(__privateGet(this, _metadata), data);
|
|
@@ -443,7 +1637,7 @@ var Trace = class {
|
|
|
443
1637
|
}
|
|
444
1638
|
try {
|
|
445
1639
|
return await __privateGet(this, _client).ingest({
|
|
446
|
-
...__privateGet(this,
|
|
1640
|
+
...__privateGet(this, _options2),
|
|
447
1641
|
name: __privateGet(this, _name),
|
|
448
1642
|
environment: __privateGet(this, _environment),
|
|
449
1643
|
durationMs: Date.now() - __privateGet(this, _startTime),
|
|
@@ -458,7 +1652,7 @@ var Trace = class {
|
|
|
458
1652
|
};
|
|
459
1653
|
_client = new WeakMap();
|
|
460
1654
|
_name = new WeakMap();
|
|
461
|
-
|
|
1655
|
+
_options2 = new WeakMap();
|
|
462
1656
|
_spans = new WeakMap();
|
|
463
1657
|
_startTime = new WeakMap();
|
|
464
1658
|
_disabled = new WeakMap();
|
|
@@ -660,8 +1854,8 @@ var Recount = class {
|
|
|
660
1854
|
continue;
|
|
661
1855
|
}
|
|
662
1856
|
if (!res.ok) {
|
|
663
|
-
const
|
|
664
|
-
__privateGet(this, _log2).call(this, `ingest error status=${res.status} message=${
|
|
1857
|
+
const text2 = await res.text().catch(() => res.statusText);
|
|
1858
|
+
__privateGet(this, _log2).call(this, `ingest error status=${res.status} message=${text2}`);
|
|
665
1859
|
return NOOP_RESULT2;
|
|
666
1860
|
}
|
|
667
1861
|
let json;
|
|
@@ -700,12 +1894,12 @@ _environment2 = new WeakMap();
|
|
|
700
1894
|
_disabled2 = new WeakMap();
|
|
701
1895
|
_timeoutMs = new WeakMap();
|
|
702
1896
|
_log2 = new WeakMap();
|
|
703
|
-
var
|
|
1897
|
+
var _key2 = /* @__PURE__ */ Symbol.for("@refactorlabs/recount.storage");
|
|
704
1898
|
var _nameKey = /* @__PURE__ */ Symbol.for("@refactorlabs/recount.spanName");
|
|
705
1899
|
var _g = globalThis;
|
|
706
|
-
if (!_g[
|
|
1900
|
+
if (!_g[_key2]) _g[_key2] = new async_hooks.AsyncLocalStorage();
|
|
707
1901
|
if (!_g[_nameKey]) _g[_nameKey] = new async_hooks.AsyncLocalStorage();
|
|
708
|
-
var storage = _g[
|
|
1902
|
+
var storage = _g[_key2];
|
|
709
1903
|
var nameStorage = _g[_nameKey];
|
|
710
1904
|
function getActiveTrace() {
|
|
711
1905
|
return storage.getStore();
|
|
@@ -721,12 +1915,41 @@ function runWithSpanName(name, fn) {
|
|
|
721
1915
|
function spanCell(value, span) {
|
|
722
1916
|
return { __cellSpan: true, span, value };
|
|
723
1917
|
}
|
|
1918
|
+
function cell(value, options = {}) {
|
|
1919
|
+
return {
|
|
1920
|
+
__cell: true,
|
|
1921
|
+
value: cleanCellValue(value),
|
|
1922
|
+
...definedOnly(options)
|
|
1923
|
+
};
|
|
1924
|
+
}
|
|
1925
|
+
function definedOnly(obj) {
|
|
1926
|
+
return Object.fromEntries(
|
|
1927
|
+
Object.entries(obj).filter(([, v]) => v !== void 0)
|
|
1928
|
+
);
|
|
1929
|
+
}
|
|
1930
|
+
function cleanCellValue(value) {
|
|
1931
|
+
if (typeof value === "number") {
|
|
1932
|
+
if (!Number.isFinite(value)) return null;
|
|
1933
|
+
if (Object.is(value, -0)) return 0;
|
|
1934
|
+
}
|
|
1935
|
+
return value;
|
|
1936
|
+
}
|
|
1937
|
+
function signOf(value, highlight) {
|
|
1938
|
+
const n = typeof value === "number" ? value : typeof value === "string" && value.trim() !== "" ? Number(value) : NaN;
|
|
1939
|
+
if (Number.isNaN(n)) return null;
|
|
1940
|
+
if (n > 0) return highlight.positive ?? null;
|
|
1941
|
+
if (n < 0) return highlight.negative ?? null;
|
|
1942
|
+
return highlight.zero ?? null;
|
|
1943
|
+
}
|
|
724
1944
|
function roundValue(value, decimals) {
|
|
725
1945
|
if (typeof value === "number") {
|
|
726
1946
|
return +value.toFixed(decimals);
|
|
727
1947
|
}
|
|
728
1948
|
return value;
|
|
729
1949
|
}
|
|
1950
|
+
function isCellValue(value) {
|
|
1951
|
+
return value === null || typeof value === "number" || typeof value === "string" || typeof value === "boolean";
|
|
1952
|
+
}
|
|
730
1953
|
var TableBuilder = class {
|
|
731
1954
|
constructor(title) {
|
|
732
1955
|
this._rawKeys = [];
|
|
@@ -738,6 +1961,8 @@ var TableBuilder = class {
|
|
|
738
1961
|
this._cellHighlightFns = {};
|
|
739
1962
|
this._extraRows = [];
|
|
740
1963
|
this._redactFields = /* @__PURE__ */ new Set();
|
|
1964
|
+
this._formats = {};
|
|
1965
|
+
this._signHighlights = {};
|
|
741
1966
|
this._title = title;
|
|
742
1967
|
}
|
|
743
1968
|
redact(fields) {
|
|
@@ -787,6 +2012,23 @@ var TableBuilder = class {
|
|
|
787
2012
|
Object.assign(this._labels, map);
|
|
788
2013
|
return this;
|
|
789
2014
|
}
|
|
2015
|
+
/**
|
|
2016
|
+
* Has Recount format these columns (records) or rows (objects), so send
|
|
2017
|
+
* the numbers as they are: `.format({ cost: { format: 'money', currency: 'AUD' } })`.
|
|
2018
|
+
* Formatted values aren't affected by `.round()`.
|
|
2019
|
+
*/
|
|
2020
|
+
format(map) {
|
|
2021
|
+
Object.assign(this._formats, map);
|
|
2022
|
+
return this;
|
|
2023
|
+
}
|
|
2024
|
+
/**
|
|
2025
|
+
* Colours a column's numbers (records) or a row (objects) by sign:
|
|
2026
|
+
* `.signHighlight('saving', { positive: 'success', negative: 'danger' })`.
|
|
2027
|
+
*/
|
|
2028
|
+
signHighlight(key, highlight) {
|
|
2029
|
+
this._signHighlights[key] = highlight;
|
|
2030
|
+
return this;
|
|
2031
|
+
}
|
|
790
2032
|
round(decimals) {
|
|
791
2033
|
this._round = decimals;
|
|
792
2034
|
return this;
|
|
@@ -859,15 +2101,26 @@ var TableBuilder = class {
|
|
|
859
2101
|
_resolveColumn(key, label) {
|
|
860
2102
|
const span = this._columnSpans[key];
|
|
861
2103
|
const highlight = this._columnHighlights[key];
|
|
862
|
-
|
|
2104
|
+
const format = this._formats[key];
|
|
2105
|
+
const signHighlight = this._signHighlights[key];
|
|
2106
|
+
if (span !== void 0 || highlight !== void 0 || format !== void 0 || signHighlight !== void 0) {
|
|
863
2107
|
return {
|
|
864
2108
|
label,
|
|
865
2109
|
...span !== void 0 && { span },
|
|
866
|
-
...highlight !== void 0 && { highlight }
|
|
2110
|
+
...highlight !== void 0 && { highlight },
|
|
2111
|
+
...format !== void 0 && definedOnly(format),
|
|
2112
|
+
...signHighlight !== void 0 && { signHighlight }
|
|
867
2113
|
};
|
|
868
2114
|
}
|
|
869
2115
|
return label;
|
|
870
2116
|
}
|
|
2117
|
+
// A value as sent: rounded, unless Recount formats it.
|
|
2118
|
+
_finalValue(key, value) {
|
|
2119
|
+
if (this._formats[key] !== void 0) {
|
|
2120
|
+
return typeof value === "number" ? cleanCellValue(value) : value;
|
|
2121
|
+
}
|
|
2122
|
+
return this._round !== void 0 ? roundValue(value, this._round) : value;
|
|
2123
|
+
}
|
|
871
2124
|
_mergeRowHighlights(highlights, rowCount) {
|
|
872
2125
|
if (!highlights && !this._extraRows.some((r) => r.highlight))
|
|
873
2126
|
return void 0;
|
|
@@ -901,13 +2154,19 @@ var TableBuilder = class {
|
|
|
901
2154
|
return [this._resolveLabel(key), REDACTED];
|
|
902
2155
|
}
|
|
903
2156
|
const transformed = this._valueTransforms[key] ? this._valueTransforms[key](v) : v;
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
this.
|
|
907
|
-
|
|
2157
|
+
const format = this._formats[key];
|
|
2158
|
+
if (format !== void 0 && isCellValue(transformed)) {
|
|
2159
|
+
return [this._resolveLabel(key), cell(transformed, format)];
|
|
2160
|
+
}
|
|
2161
|
+
return [this._resolveLabel(key), this._finalValue(key, transformed)];
|
|
908
2162
|
});
|
|
909
2163
|
const allRows = this._extraRows.length ? [...finalRows, ...this._extraRows.map((r) => r.cells)] : finalRows;
|
|
910
|
-
const
|
|
2164
|
+
const hasSigns = Object.keys(this._signHighlights).length > 0;
|
|
2165
|
+
const rowHighlights = this._rowHighlightFn || hasSigns ? rows.map(([k, v]) => {
|
|
2166
|
+
const fromFn = this._rowHighlightFn ? this._rowHighlightFn({ [k]: v }) : null;
|
|
2167
|
+
const sign = this._signHighlights[k];
|
|
2168
|
+
return fromFn ?? (sign && !this._shouldRedact(k) ? signOf(v, sign) : null);
|
|
2169
|
+
}) : void 0;
|
|
911
2170
|
const allRowHighlights = this._mergeRowHighlights(
|
|
912
2171
|
rowHighlights,
|
|
913
2172
|
finalRows.length
|
|
@@ -931,7 +2190,7 @@ var TableBuilder = class {
|
|
|
931
2190
|
if (this._shouldRedact(k)) return REDACTED;
|
|
932
2191
|
const raw = row[keyToIndex.get(k)];
|
|
933
2192
|
const value = this._valueTransforms[k] ? this._valueTransforms[k](raw) : raw;
|
|
934
|
-
return this.
|
|
2193
|
+
return this._finalValue(k, value);
|
|
935
2194
|
})
|
|
936
2195
|
);
|
|
937
2196
|
const rowHighlights = this._buildRowHighlights(activeKeys);
|
|
@@ -1126,8 +2385,11 @@ async function runSpanCallback(type, name, opts, fn) {
|
|
|
1126
2385
|
function addGroup(name, builder, options) {
|
|
1127
2386
|
getActiveTrace()?.group(name, builder, options);
|
|
1128
2387
|
}
|
|
1129
|
-
function addMessage(name,
|
|
1130
|
-
getActiveTrace()?.addMessage(name,
|
|
2388
|
+
function addMessage(name, text2, opts) {
|
|
2389
|
+
getActiveTrace()?.addMessage(name, text2, opts);
|
|
2390
|
+
}
|
|
2391
|
+
function addCalculation(name, calcs, opts) {
|
|
2392
|
+
getActiveTrace()?.calculation(name, calcs, opts);
|
|
1131
2393
|
}
|
|
1132
2394
|
function addAlert(label, level, opts) {
|
|
1133
2395
|
getActiveTrace()?.alert(label, level, opts);
|
|
@@ -1158,6 +2420,7 @@ var recount = {
|
|
|
1158
2420
|
addSpan,
|
|
1159
2421
|
addGroup,
|
|
1160
2422
|
addMessage,
|
|
2423
|
+
addCalculation,
|
|
1161
2424
|
addAlert,
|
|
1162
2425
|
addMetadata,
|
|
1163
2426
|
fetch,
|
|
@@ -1167,9 +2430,12 @@ var recount = {
|
|
|
1167
2430
|
tableFromRecords,
|
|
1168
2431
|
tableSections,
|
|
1169
2432
|
spanCell,
|
|
2433
|
+
cell,
|
|
2434
|
+
calculation,
|
|
1170
2435
|
chart,
|
|
1171
2436
|
chartFromRecords,
|
|
1172
2437
|
// Constants & sentinels
|
|
2438
|
+
units,
|
|
1173
2439
|
REDACTED
|
|
1174
2440
|
};
|
|
1175
2441
|
var index_default = recount;
|
|
@@ -1179,10 +2445,13 @@ exports.Recount = Recount;
|
|
|
1179
2445
|
exports.RecountError = RecountError;
|
|
1180
2446
|
exports.SpanGroup = SpanGroup;
|
|
1181
2447
|
exports.addAlert = addAlert;
|
|
2448
|
+
exports.addCalculation = addCalculation;
|
|
1182
2449
|
exports.addGroup = addGroup;
|
|
1183
2450
|
exports.addMessage = addMessage;
|
|
1184
2451
|
exports.addMetadata = addMetadata;
|
|
1185
2452
|
exports.addSpan = addSpan;
|
|
2453
|
+
exports.calculation = calculation;
|
|
2454
|
+
exports.cell = cell;
|
|
1186
2455
|
exports.chart = chart;
|
|
1187
2456
|
exports.chartFromRecords = chartFromRecords;
|
|
1188
2457
|
exports.default = index_default;
|
|
@@ -1195,5 +2464,6 @@ exports.table = table;
|
|
|
1195
2464
|
exports.tableFromObject = tableFromObject;
|
|
1196
2465
|
exports.tableFromRecords = tableFromRecords;
|
|
1197
2466
|
exports.tableSections = tableSections;
|
|
2467
|
+
exports.units = units;
|
|
1198
2468
|
//# sourceMappingURL=index.cjs.map
|
|
1199
2469
|
//# sourceMappingURL=index.cjs.map
|