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