@orkestrel/rater 0.0.20 → 0.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/core/index.cjs +271 -35
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +84 -12
- package/dist/src/core/index.d.ts +84 -12
- package/dist/src/core/index.js +261 -38
- package/dist/src/core/index.js.map +1 -1
- package/package.json +12 -12
package/dist/src/core/index.js
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
import { INTRINSICS, arrayOf, holds, isArray, isBoolean, isFunction, isJSONValue, isNumber, isObject, isRecord, isString, literalOf, objectOf, recordOf } from "@orkestrel/contract";
|
|
2
|
-
import {
|
|
1
|
+
import { INTRINSICS, arrayOf, arrayShape, auditFaultShape, booleanShape, createContract, describeFaults, holds, isArray, isAuditFault, isBoolean, isFiniteNumber, isFunction, isJSONValue, isNumber, isObject, isRecord, isString, jsonShape, literalOf, literalShape, nullableShape, numberShape, objectOf, objectShape, optionalShape, recordOf, shapeShape, stringShape } from "@orkestrel/contract";
|
|
2
|
+
import { AGGREGATIONS, CHECK_STATES, COMPARISONS, createQuantitativeReasoner, createReason, createSubjectDoor, fieldPathShape, filterUniqueFaults, filterUniqueFields, formatField, isAggregation, isCheckState, isComparison, isContractShape, isFieldPath, isQuantitativeDefinition, isQuantitativeResult, isReasonError, projectValue, quantitativeDefinitionShape, scanShape, scanShapeReads } from "@orkestrel/reason";
|
|
3
3
|
import { Emitter } from "@orkestrel/emitter";
|
|
4
|
+
// src/core/constants.ts
|
|
5
|
+
/** Lists the stages a worksheet derivation step can belong to. */
|
|
6
|
+
var WORKSHEET_STAGES = [
|
|
7
|
+
"factor",
|
|
8
|
+
"group",
|
|
9
|
+
"total"
|
|
10
|
+
];
|
|
4
11
|
// src/core/errors.ts
|
|
5
12
|
/**
|
|
6
13
|
* Represents a coded programmer error thrown by the rating layer.
|
|
@@ -132,15 +139,20 @@ function isRatingDefinition(value) {
|
|
|
132
139
|
name: isString,
|
|
133
140
|
description: isString,
|
|
134
141
|
lines: arrayOf(isLineDefinition),
|
|
135
|
-
metadata: isJSONValue
|
|
136
|
-
|
|
142
|
+
metadata: isJSONValue,
|
|
143
|
+
shape: isContractShape
|
|
144
|
+
}, [
|
|
145
|
+
"description",
|
|
146
|
+
"metadata",
|
|
147
|
+
"shape"
|
|
148
|
+
])(value);
|
|
137
149
|
}
|
|
138
150
|
/**
|
|
139
151
|
* Determines whether a value is an open result-side {@link Evidence} object.
|
|
140
152
|
*
|
|
141
153
|
* @remarks
|
|
142
154
|
* Optional `field` is checked as a `FieldPath`, `label` as a string,
|
|
143
|
-
* `comparison` as a `Comparison`, and `
|
|
155
|
+
* `comparison` as a `Comparison`, and `state` as a `CheckState`, each when defined.
|
|
144
156
|
* Unknown members, prototypes, and class instances are admitted. Arrays are
|
|
145
157
|
* refused. `expected` and `actual` are `unknown`, so this guard does not read or
|
|
146
158
|
* check them and a borrowed result is never narrowed on them; either member may
|
|
@@ -154,7 +166,7 @@ function isRatingDefinition(value) {
|
|
|
154
166
|
* ```ts
|
|
155
167
|
* import { isEvidence } from '@orkestrel/rater'
|
|
156
168
|
*
|
|
157
|
-
* isEvidence({ field: 'age', comparison: 'above', expected: 18, actual: 25,
|
|
169
|
+
* isEvidence({ field: 'age', comparison: 'above', expected: 18, actual: 25, state: 'met' }) // true
|
|
158
170
|
* ```
|
|
159
171
|
*/
|
|
160
172
|
function isEvidence(value) {
|
|
@@ -162,12 +174,12 @@ function isEvidence(value) {
|
|
|
162
174
|
field: isFieldPath,
|
|
163
175
|
label: isString,
|
|
164
176
|
comparison: isComparison,
|
|
165
|
-
|
|
177
|
+
state: isCheckState
|
|
166
178
|
}, [
|
|
167
179
|
"field",
|
|
168
180
|
"label",
|
|
169
181
|
"comparison",
|
|
170
|
-
"
|
|
182
|
+
"state"
|
|
171
183
|
])(value);
|
|
172
184
|
}
|
|
173
185
|
/**
|
|
@@ -285,7 +297,7 @@ function isStep(value) {
|
|
|
285
297
|
* ```ts
|
|
286
298
|
* import { isWorksheet } from '@orkestrel/rater'
|
|
287
299
|
*
|
|
288
|
-
* isWorksheet({ id: 'quote', name: 'Quote', aggregation: 'sum', value: 0, groups: [], steps: [], trace: [], errors: [], success: true }) // true
|
|
300
|
+
* isWorksheet({ id: 'quote', name: 'Quote', aggregation: 'sum', value: 0, groups: [], steps: [], trace: [], errors: [], success: true, faults: [], repairs: [], defaulted: [] }) // true
|
|
289
301
|
* ```
|
|
290
302
|
*/
|
|
291
303
|
function isWorksheet(value) {
|
|
@@ -299,7 +311,10 @@ function isWorksheet(value) {
|
|
|
299
311
|
steps: arrayOf(isStep),
|
|
300
312
|
trace: arrayOf(isString),
|
|
301
313
|
errors: arrayOf(isString),
|
|
302
|
-
success: isBoolean
|
|
314
|
+
success: isBoolean,
|
|
315
|
+
faults: arrayOf(isAuditFault),
|
|
316
|
+
repairs: arrayOf(isAuditFault),
|
|
317
|
+
defaulted: arrayOf(isFieldPath)
|
|
303
318
|
}, ["precision"])(value);
|
|
304
319
|
}
|
|
305
320
|
/**
|
|
@@ -352,14 +367,18 @@ function isLineResult(value) {
|
|
|
352
367
|
* ```ts
|
|
353
368
|
* import { isRatingResult } from '@orkestrel/rater'
|
|
354
369
|
*
|
|
355
|
-
* isRatingResult({ lines: [], success: true }) // true
|
|
370
|
+
* isRatingResult({ lines: [], success: true, errors: [], faults: [], repairs: [], defaulted: [] }) // true
|
|
356
371
|
* ```
|
|
357
372
|
*/
|
|
358
373
|
function isRatingResult(value) {
|
|
359
374
|
return objectOf({
|
|
360
375
|
lines: arrayOf(isLineResult),
|
|
361
376
|
total: isNumber,
|
|
362
|
-
success: isBoolean
|
|
377
|
+
success: isBoolean,
|
|
378
|
+
errors: arrayOf(isString),
|
|
379
|
+
faults: arrayOf(isAuditFault),
|
|
380
|
+
repairs: arrayOf(isAuditFault),
|
|
381
|
+
defaulted: arrayOf(isFieldPath)
|
|
363
382
|
}, ["total"])(value);
|
|
364
383
|
}
|
|
365
384
|
// src/core/helpers.ts
|
|
@@ -423,8 +442,7 @@ function buildRatingDefinition(id, name, lines, overrides) {
|
|
|
423
442
|
*
|
|
424
443
|
* @param check - The authored check
|
|
425
444
|
* @param actual - The resolved subject value
|
|
426
|
-
* @param
|
|
427
|
-
* was not yet evaluated
|
|
445
|
+
* @param state - The check's state; absent when the check was not evaluated
|
|
428
446
|
* @param labels - Optional field-to-label overrides, keyed by dot-joined field
|
|
429
447
|
* @returns A fresh evidence row
|
|
430
448
|
*
|
|
@@ -434,18 +452,19 @@ function buildRatingDefinition(id, name, lines, overrides) {
|
|
|
434
452
|
* import { buildEvidence } from '@orkestrel/rater'
|
|
435
453
|
*
|
|
436
454
|
* const entry = createCheck('age', 'above', 18)
|
|
437
|
-
* buildEvidence(entry, 25,
|
|
455
|
+
* buildEvidence(entry, 25, 'met') // { field: 'age', comparison: 'above', expected: 18, actual: 25, state: 'met' }
|
|
438
456
|
* ```
|
|
439
457
|
*/
|
|
440
|
-
function buildEvidence(check, actual,
|
|
458
|
+
function buildEvidence(check, actual, state, labels) {
|
|
441
459
|
const field = formatField(check.field);
|
|
460
|
+
const label = labels !== void 0 && Object.hasOwn(labels, field) ? labels[field] : void 0;
|
|
442
461
|
return {
|
|
443
462
|
field: check.field,
|
|
444
|
-
...
|
|
463
|
+
...label === void 0 ? {} : { label },
|
|
445
464
|
comparison: check.operator,
|
|
446
465
|
expected: check.value,
|
|
447
466
|
actual,
|
|
448
|
-
...
|
|
467
|
+
...state === void 0 ? {} : { state }
|
|
449
468
|
};
|
|
450
469
|
}
|
|
451
470
|
/**
|
|
@@ -463,13 +482,13 @@ function buildEvidence(check, actual, met, labels) {
|
|
|
463
482
|
* import { buildEvidenceRows } from '@orkestrel/rater'
|
|
464
483
|
*
|
|
465
484
|
* const checks = [createCheck('age', 'above', 18)]
|
|
466
|
-
* buildEvidenceRows(checks, [{ field: 'age',
|
|
485
|
+
* buildEvidenceRows(checks, [{ field: 'age', state: 'met', actual: 25 }])
|
|
467
486
|
* ```
|
|
468
487
|
*/
|
|
469
488
|
function buildEvidenceRows(checks, results, labels) {
|
|
470
489
|
return (checks ?? []).map((check, index) => {
|
|
471
490
|
const result = results?.[index];
|
|
472
|
-
return buildEvidence(check, result?.actual, result?.
|
|
491
|
+
return buildEvidence(check, result?.actual, result?.state, labels);
|
|
473
492
|
});
|
|
474
493
|
}
|
|
475
494
|
/**
|
|
@@ -615,7 +634,10 @@ function buildWorksheet(definition, result, labels) {
|
|
|
615
634
|
steps: buildWorksheetSteps(definition, result, groups),
|
|
616
635
|
trace: result.trace,
|
|
617
636
|
errors: result.errors,
|
|
618
|
-
success: result.success
|
|
637
|
+
success: result.success,
|
|
638
|
+
faults: result.faults,
|
|
639
|
+
repairs: result.repairs,
|
|
640
|
+
defaulted: result.defaulted
|
|
619
641
|
};
|
|
620
642
|
}
|
|
621
643
|
/**
|
|
@@ -649,7 +671,7 @@ function buildLineResult(definition, result, labels) {
|
|
|
649
671
|
* Sums defined line amounts.
|
|
650
672
|
*
|
|
651
673
|
* @param lines - The rated lines
|
|
652
|
-
* @returns The sum
|
|
674
|
+
* @returns The sum, or `undefined` when no line has an amount or any line refused
|
|
653
675
|
*
|
|
654
676
|
* @example
|
|
655
677
|
* ```ts
|
|
@@ -663,8 +685,145 @@ function buildLineResult(definition, result, labels) {
|
|
|
663
685
|
* ```
|
|
664
686
|
*/
|
|
665
687
|
function sumAmounts(lines) {
|
|
688
|
+
if (!lines.every((rated) => rated.worksheet.success)) return void 0;
|
|
666
689
|
return Iterator.from(lines).reduce((total, rated) => rated.amount === void 0 ? total : (total ?? 0) + rated.amount, void 0);
|
|
667
690
|
}
|
|
691
|
+
/**
|
|
692
|
+
* Projects a result to the JSON its contract reads.
|
|
693
|
+
*
|
|
694
|
+
* @remarks
|
|
695
|
+
* An evidence row's `actual` holds whatever the subject held and a worksheet's numbers may be
|
|
696
|
+
* `NaN`, so a result crosses a boundary as this projection, each `actual` on its own budget.
|
|
697
|
+
*
|
|
698
|
+
* @param result - The rating result
|
|
699
|
+
* @returns Its JSON projection, which `ratingResultContract` admits
|
|
700
|
+
*
|
|
701
|
+
* @example
|
|
702
|
+
* ```ts
|
|
703
|
+
* import { createRater, projectRatingResult, ratingResultContract } from '@orkestrel/rater'
|
|
704
|
+
*
|
|
705
|
+
* const rater = createRater()
|
|
706
|
+
* ratingResultContract.is(projectRatingResult(rater.rate([], {}))) // true
|
|
707
|
+
* rater.destroy()
|
|
708
|
+
* ```
|
|
709
|
+
*/
|
|
710
|
+
function projectRatingResult(result) {
|
|
711
|
+
const lines = result.lines.map((line) => ({
|
|
712
|
+
...line,
|
|
713
|
+
worksheet: {
|
|
714
|
+
...line.worksheet,
|
|
715
|
+
groups: line.worksheet.groups.map((group) => ({
|
|
716
|
+
...group,
|
|
717
|
+
factors: group.factors.map((factor) => ({
|
|
718
|
+
...factor,
|
|
719
|
+
evidence: factor.evidence.map((row) => ({
|
|
720
|
+
...row,
|
|
721
|
+
actual: projectValue(row.actual)
|
|
722
|
+
}))
|
|
723
|
+
}))
|
|
724
|
+
}))
|
|
725
|
+
}
|
|
726
|
+
}));
|
|
727
|
+
return projectValue({
|
|
728
|
+
...result,
|
|
729
|
+
lines
|
|
730
|
+
}, /* @__PURE__ */ new Set(), { left: Number.POSITIVE_INFINITY });
|
|
731
|
+
}
|
|
732
|
+
// src/core/shapers.ts
|
|
733
|
+
/** Describes a `LineDefinition`. */
|
|
734
|
+
var lineDefinitionShape = objectShape({
|
|
735
|
+
id: stringShape(),
|
|
736
|
+
name: stringShape(),
|
|
737
|
+
description: optionalShape(stringShape()),
|
|
738
|
+
rate: quantitativeDefinitionShape,
|
|
739
|
+
metadata: optionalShape(jsonShape())
|
|
740
|
+
});
|
|
741
|
+
/**
|
|
742
|
+
* Describes a `RatingDefinition`.
|
|
743
|
+
*/
|
|
744
|
+
var ratingDefinitionShape = objectShape({
|
|
745
|
+
id: stringShape(),
|
|
746
|
+
name: stringShape(),
|
|
747
|
+
description: optionalShape(stringShape()),
|
|
748
|
+
lines: arrayShape(lineDefinitionShape),
|
|
749
|
+
metadata: optionalShape(jsonShape()),
|
|
750
|
+
shape: optionalShape(shapeShape())
|
|
751
|
+
});
|
|
752
|
+
/** Describes an `Evidence` row in its JSON projection. */
|
|
753
|
+
var evidenceShape = objectShape({
|
|
754
|
+
field: optionalShape(fieldPathShape),
|
|
755
|
+
label: optionalShape(stringShape()),
|
|
756
|
+
comparison: optionalShape(literalShape(COMPARISONS)),
|
|
757
|
+
expected: optionalShape(jsonShape()),
|
|
758
|
+
actual: optionalShape(jsonShape()),
|
|
759
|
+
state: optionalShape(literalShape(CHECK_STATES))
|
|
760
|
+
});
|
|
761
|
+
/** Describes a `WorksheetFactor` in its JSON projection. */
|
|
762
|
+
var worksheetFactorShape = objectShape({
|
|
763
|
+
id: stringShape(),
|
|
764
|
+
name: optionalShape(stringShape()),
|
|
765
|
+
description: optionalShape(stringShape()),
|
|
766
|
+
applied: booleanShape(),
|
|
767
|
+
value: optionalShape(nullableShape(numberShape())),
|
|
768
|
+
evidence: arrayShape(evidenceShape)
|
|
769
|
+
});
|
|
770
|
+
/** Describes a `WorksheetGroup` in its JSON projection. */
|
|
771
|
+
var worksheetGroupShape = objectShape({
|
|
772
|
+
id: stringShape(),
|
|
773
|
+
name: optionalShape(stringShape()),
|
|
774
|
+
description: optionalShape(stringShape()),
|
|
775
|
+
applied: booleanShape(),
|
|
776
|
+
value: nullableShape(numberShape()),
|
|
777
|
+
factors: arrayShape(worksheetFactorShape)
|
|
778
|
+
});
|
|
779
|
+
/** Describes a `Step` in its JSON projection. */
|
|
780
|
+
var worksheetStepShape = objectShape({
|
|
781
|
+
stage: literalShape(WORKSHEET_STAGES),
|
|
782
|
+
id: optionalShape(stringShape()),
|
|
783
|
+
name: optionalShape(stringShape()),
|
|
784
|
+
value: nullableShape(numberShape()),
|
|
785
|
+
expression: optionalShape(stringShape())
|
|
786
|
+
});
|
|
787
|
+
/** Describes a `Worksheet` in its JSON projection. */
|
|
788
|
+
var worksheetShape = objectShape({
|
|
789
|
+
id: stringShape(),
|
|
790
|
+
name: stringShape(),
|
|
791
|
+
aggregation: literalShape(AGGREGATIONS),
|
|
792
|
+
precision: optionalShape(numberShape()),
|
|
793
|
+
value: nullableShape(numberShape()),
|
|
794
|
+
groups: arrayShape(worksheetGroupShape),
|
|
795
|
+
steps: arrayShape(worksheetStepShape),
|
|
796
|
+
trace: arrayShape(stringShape()),
|
|
797
|
+
errors: arrayShape(stringShape()),
|
|
798
|
+
success: booleanShape(),
|
|
799
|
+
faults: arrayShape(auditFaultShape()),
|
|
800
|
+
repairs: arrayShape(auditFaultShape()),
|
|
801
|
+
defaulted: arrayShape(fieldPathShape)
|
|
802
|
+
});
|
|
803
|
+
/** Describes a `LineResult` in its JSON projection. */
|
|
804
|
+
var lineResultShape = objectShape({
|
|
805
|
+
id: stringShape(),
|
|
806
|
+
name: stringShape(),
|
|
807
|
+
amount: optionalShape(nullableShape(numberShape())),
|
|
808
|
+
worksheet: worksheetShape
|
|
809
|
+
});
|
|
810
|
+
/** Describes a `RatingResult` in its JSON projection. */
|
|
811
|
+
var ratingResultShape = objectShape({
|
|
812
|
+
lines: arrayShape(lineResultShape),
|
|
813
|
+
total: optionalShape(nullableShape(numberShape())),
|
|
814
|
+
success: booleanShape(),
|
|
815
|
+
errors: arrayShape(stringShape()),
|
|
816
|
+
faults: arrayShape(auditFaultShape()),
|
|
817
|
+
repairs: arrayShape(auditFaultShape()),
|
|
818
|
+
defaulted: arrayShape(fieldPathShape)
|
|
819
|
+
});
|
|
820
|
+
// src/core/contracts.ts
|
|
821
|
+
/**
|
|
822
|
+
* Publishes a `RatingDefinition` as a contract. Cross it on `is`, never `parse`.
|
|
823
|
+
*/
|
|
824
|
+
var ratingDefinitionContract = createContract(ratingDefinitionShape);
|
|
825
|
+
/** Publishes a `RatingResult`'s JSON projection as a contract, read through `projectRatingResult`. */
|
|
826
|
+
var ratingResultContract = createContract(ratingResultShape);
|
|
668
827
|
// src/core/Rater.ts
|
|
669
828
|
/**
|
|
670
829
|
* Orchestrates rating — owns (or receives) the shared quantitative
|
|
@@ -684,7 +843,7 @@ function sumAmounts(lines) {
|
|
|
684
843
|
* import { Rater } from '@orkestrel/rater'
|
|
685
844
|
*
|
|
686
845
|
* const rater = new Rater()
|
|
687
|
-
* rater.rate([], { id: 'subject-1' }) //
|
|
846
|
+
* rater.rate([], { id: 'subject-1' }).success // true
|
|
688
847
|
* rater.destroy()
|
|
689
848
|
* ```
|
|
690
849
|
*/
|
|
@@ -694,6 +853,8 @@ var Rater = class {
|
|
|
694
853
|
#owned;
|
|
695
854
|
#total;
|
|
696
855
|
#labels;
|
|
856
|
+
#door = createSubjectDoor();
|
|
857
|
+
#valid = /* @__PURE__ */ new WeakSet();
|
|
697
858
|
#destroyed = false;
|
|
698
859
|
constructor(options) {
|
|
699
860
|
this.#emitter = new Emitter({
|
|
@@ -720,15 +881,49 @@ var Rater = class {
|
|
|
720
881
|
this.#ensureAlive();
|
|
721
882
|
const lines = this.#normalize(input);
|
|
722
883
|
if (!isRecord(subject)) throw new RaterError("MISMATCH", "Subject must be a record");
|
|
723
|
-
const
|
|
724
|
-
const
|
|
725
|
-
|
|
884
|
+
const admission = this.#door.admit(subject, this.#readShape(input));
|
|
885
|
+
const admitted = admission.subject;
|
|
886
|
+
if (admitted === void 0) {
|
|
887
|
+
const refused = {
|
|
888
|
+
lines: [],
|
|
889
|
+
success: false,
|
|
890
|
+
errors: admission.errors,
|
|
891
|
+
faults: admission.faults,
|
|
892
|
+
repairs: [],
|
|
893
|
+
defaulted: []
|
|
894
|
+
};
|
|
895
|
+
this.#emitter.emit("rate", subject, refused);
|
|
896
|
+
return refused;
|
|
897
|
+
}
|
|
898
|
+
const results = lines.map((line) => this.#rateLine(line, admitted));
|
|
899
|
+
const rated = results.every((entry) => entry.worksheet.success);
|
|
900
|
+
const summed = rated ? (this.#total ?? sumAmounts)(results) : void 0;
|
|
901
|
+
const overflowed = summed !== void 0 && !isFiniteNumber(summed);
|
|
902
|
+
const total = overflowed ? void 0 : summed;
|
|
903
|
+
const success = rated && !overflowed;
|
|
904
|
+
const faults = filterUniqueFaults(results.flatMap((entry) => entry.worksheet.faults));
|
|
905
|
+
const said = new Set(faults.map((fault) => describeFaults([fault])));
|
|
906
|
+
const other = results.flatMap((entry) => {
|
|
907
|
+
const { errors, success: settled } = entry.worksheet;
|
|
908
|
+
if (!settled && errors.length === 0) return [`${entry.id}: The line failed and said nothing`];
|
|
909
|
+
return errors.filter((error) => !said.has(error)).map((error) => `${entry.id}: ${error}`);
|
|
910
|
+
});
|
|
911
|
+
const repairs = filterUniqueFaults([...admission.repairs, ...results.flatMap((entry) => entry.worksheet.repairs)]);
|
|
912
|
+
const defaulted = filterUniqueFields([...admission.defaulted, ...results.flatMap((entry) => entry.worksheet.defaulted)]);
|
|
726
913
|
const result = {
|
|
727
914
|
lines: results,
|
|
728
915
|
...total === void 0 ? {} : { total },
|
|
729
|
-
success
|
|
916
|
+
success,
|
|
917
|
+
errors: [
|
|
918
|
+
...other,
|
|
919
|
+
...said,
|
|
920
|
+
...overflowed ? ["The total is not a finite number"] : []
|
|
921
|
+
],
|
|
922
|
+
faults,
|
|
923
|
+
repairs,
|
|
924
|
+
defaulted
|
|
730
925
|
};
|
|
731
|
-
this.#emitter.emit("rate",
|
|
926
|
+
this.#emitter.emit("rate", admitted, result);
|
|
732
927
|
return result;
|
|
733
928
|
}
|
|
734
929
|
destroy() {
|
|
@@ -742,18 +937,25 @@ var Rater = class {
|
|
|
742
937
|
if (isRatingDefinition(input)) return input.lines;
|
|
743
938
|
throw new RaterError("DEFINITION", "Rating input must be an array of line definitions or a rating definition");
|
|
744
939
|
}
|
|
940
|
+
#readShape(input) {
|
|
941
|
+
if (arrayOf(isLineDefinition)(input)) return void 0;
|
|
942
|
+
const { shape, lines } = input;
|
|
943
|
+
if (shape === void 0 || this.#valid.has(input)) return shape;
|
|
944
|
+
const checks = lines.flatMap((line) => line.rate.groups.flatMap((group) => group.factors.flatMap((factor) => factor.checks ?? [])));
|
|
945
|
+
const fields = lines.flatMap((line) => line.rate.groups.flatMap((group) => group.factors.flatMap((factor) => factor.source.origin === "static" ? [] : [factor.source.field])));
|
|
946
|
+
const errors = [...scanShape(shape), ...scanShapeReads(shape, checks, fields, /* @__PURE__ */ new Set()).errors];
|
|
947
|
+
if (errors.length > 0) throw new RaterError("DEFINITION", `Rating shape is invalid: ${errors.join(", ")}`);
|
|
948
|
+
this.#valid.add(input);
|
|
949
|
+
return shape;
|
|
950
|
+
}
|
|
745
951
|
#rateLine(line, subject) {
|
|
746
952
|
return buildLineResult(line, this.#reasonQuantitative(subject, line.rate), this.#labels);
|
|
747
953
|
}
|
|
748
954
|
#reasonQuantitative(subject, definition) {
|
|
749
|
-
const result = this.#
|
|
750
|
-
if (result
|
|
751
|
-
const
|
|
752
|
-
const
|
|
753
|
-
if (failure.reasoning === "quantitative") return {
|
|
754
|
-
...failure,
|
|
755
|
-
errors
|
|
756
|
-
};
|
|
955
|
+
const result = this.#reason(subject, definition);
|
|
956
|
+
if (isQuantitativeResult(result)) return this.#settle(result);
|
|
957
|
+
const answer = isRecord(result) ? result : {};
|
|
958
|
+
const named = isString(answer.reasoning) ? answer.reasoning : "something else";
|
|
757
959
|
return {
|
|
758
960
|
reasoning: "quantitative",
|
|
759
961
|
value: 0,
|
|
@@ -761,9 +963,30 @@ var Rater = class {
|
|
|
761
963
|
count: 0,
|
|
762
964
|
success: false,
|
|
763
965
|
trace: [],
|
|
764
|
-
errors
|
|
966
|
+
errors: [...isArray(answer.errors) ? answer.errors.filter(isString) : [], `Expected quantitative result, got ${named}`],
|
|
967
|
+
faults: [],
|
|
968
|
+
repairs: [],
|
|
969
|
+
defaulted: []
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
#settle(result) {
|
|
973
|
+
if (!result.success) return result;
|
|
974
|
+
if (result.errors.length === 0 && result.faults.length === 0 && isFiniteNumber(result.value)) return result;
|
|
975
|
+
const said = result.errors.length > 0 ? [] : ["The line succeeded beside a fault or no value"];
|
|
976
|
+
return {
|
|
977
|
+
...result,
|
|
978
|
+
success: false,
|
|
979
|
+
errors: [...result.errors, ...said]
|
|
765
980
|
};
|
|
766
981
|
}
|
|
982
|
+
#reason(subject, definition) {
|
|
983
|
+
try {
|
|
984
|
+
return this.#engine.reason(subject, definition);
|
|
985
|
+
} catch (error) {
|
|
986
|
+
if (isReasonError(error) && error.code === "INVALID") throw new RaterError("DEFINITION", error.message);
|
|
987
|
+
throw error;
|
|
988
|
+
}
|
|
989
|
+
}
|
|
767
990
|
#ensureAlive() {
|
|
768
991
|
if (this.#destroyed) throw new RaterError("DESTROYED", "Rater has been destroyed");
|
|
769
992
|
}
|
|
@@ -787,6 +1010,6 @@ var Rater = class {
|
|
|
787
1010
|
function createRater(options) {
|
|
788
1011
|
return new Rater(options);
|
|
789
1012
|
}
|
|
790
|
-
export { Rater, RaterError, buildEvidence, buildEvidenceRows, buildLineDefinition, buildLineResult, buildRatingDefinition, buildWorksheet, buildWorksheetFactor, buildWorksheetGroup, buildWorksheetStep, buildWorksheetSteps, createRater, isEvidence, isLineDefinition, isLineResult, isRaterError, isRatingDefinition, isRatingResult, isStep, isWorksheet, isWorksheetFactor, isWorksheetGroup, isWorksheetStage, sumAmounts };
|
|
1013
|
+
export { Rater, RaterError, WORKSHEET_STAGES, buildEvidence, buildEvidenceRows, buildLineDefinition, buildLineResult, buildRatingDefinition, buildWorksheet, buildWorksheetFactor, buildWorksheetGroup, buildWorksheetStep, buildWorksheetSteps, createRater, evidenceShape, isEvidence, isLineDefinition, isLineResult, isRaterError, isRatingDefinition, isRatingResult, isStep, isWorksheet, isWorksheetFactor, isWorksheetGroup, isWorksheetStage, lineDefinitionShape, lineResultShape, projectRatingResult, ratingDefinitionContract, ratingDefinitionShape, ratingResultContract, ratingResultShape, sumAmounts, worksheetFactorShape, worksheetGroupShape, worksheetShape, worksheetStepShape };
|
|
791
1014
|
|
|
792
1015
|
//# sourceMappingURL=index.js.map
|