Add external row quantity foundation
This commit is contained in:
parent
bc2bc7ece5
commit
dac19b093c
30 changed files with 2750 additions and 22 deletions
43
src/domain/calculations/circuit-device-row-quantity.ts
Normal file
43
src/domain/calculations/circuit-device-row-quantity.ts
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
export interface ExternalQuantityContribution {
|
||||
effectiveQuantity: number;
|
||||
}
|
||||
|
||||
export function calculateCircuitDeviceRowQuantity(
|
||||
manualQuantity: number,
|
||||
externalObjects: ExternalQuantityContribution[]
|
||||
) {
|
||||
assertNonNegativeFinite(manualQuantity, "Manual quantity");
|
||||
return externalObjects.reduce((quantity, object) => {
|
||||
assertPositiveFinite(object.effectiveQuantity, "External effective quantity");
|
||||
return quantity + object.effectiveQuantity;
|
||||
}, manualQuantity);
|
||||
}
|
||||
|
||||
export function assertCircuitDeviceRowQuantity(input: {
|
||||
quantity: number;
|
||||
manualQuantity: number;
|
||||
externalObjects: ExternalQuantityContribution[];
|
||||
}) {
|
||||
assertNonNegativeFinite(input.quantity, "Materialized quantity");
|
||||
const expected = calculateCircuitDeviceRowQuantity(
|
||||
input.manualQuantity,
|
||||
input.externalObjects
|
||||
);
|
||||
if (input.quantity !== expected) {
|
||||
throw new Error(
|
||||
`Materialized quantity ${input.quantity} does not match manual and external quantity ${expected}.`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function assertNonNegativeFinite(value: number, label: string) {
|
||||
if (!Number.isFinite(value) || value < 0) {
|
||||
throw new Error(`${label} must be a non-negative finite number.`);
|
||||
}
|
||||
}
|
||||
|
||||
function assertPositiveFinite(value: number, label: string) {
|
||||
if (!Number.isFinite(value) || value <= 0) {
|
||||
throw new Error(`${label} must be a positive finite number.`);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue