All files / src/pages/flowsheet-page/economics/settings-panel/model settingsValueUtils.ts

86.2% Statements 25/29
78.26% Branches 18/23
75% Functions 3/4
96% Lines 24/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66      36x 36x       6x 6x       11x 11x 11x 11x 11x       25x       11x 11x       10x 10x       104x 104x       2x               26x 26x   72x       18x   94x 188x 94x            
import { NUMERIC_DRAFT_PAYLOAD_KEYS } from "./settingsOptions";
 
export function nullableString(value: string) {
  const trimmed = value.trim();
  return trimmed ? trimmed : null;
}
 
export function nullableNumber(value: string) {
  const trimmed = value.trim();
  return trimmed ? Number(trimmed) : null;
}
 
export function nullablePositiveInteger(value: string) {
  const trimmed = value.trim();
  Iif (!trimmed) return null;
  const parsed = Number(trimmed);
  Iif (!Number.isInteger(parsed) || parsed <= 0) return null;
  return parsed;
}
 
export function isFiniteNumber(value: string) {
  return Number.isFinite(Number(value));
}
 
export function isPositiveFiniteNumber(value: string) {
  const parsed = Number(value);
  return Number.isFinite(parsed) && parsed > 0;
}
 
export function isPercentValue(value: string) {
  const parsed = Number(value);
  return Number.isFinite(parsed) && parsed >= 0 && parsed <= 100;
}
 
export function normalizeDecimal(value: string) {
  const parsed = Number(value);
  return Number.isFinite(parsed) ? String(parsed) : value;
}
 
export function draftPayloadsMatch(a: unknown, b: unknown) {
  return (
    JSON.stringify(normalizeDraftPayload(a)) ===
    JSON.stringify(normalizeDraftPayload(b))
  );
}
 
function normalizeDraftPayload(value: unknown, key?: string): unknown {
  if (typeof value === "string" && key && NUMERIC_DRAFT_PAYLOAD_KEYS.has(key)) {
    const trimmed = value.trim();
    return trimmed ? normalizeDecimal(trimmed) : null;
  }
  if (!value || typeof value !== "object") return value;
  if (Array.isArray(value)) {
    return value.map((item) => normalizeDraftPayload(item));
  }
  return Object.fromEntries(
    Object.entries(value)
      .filter(([, item]) => item !== undefined)
      .sort(([a], [b]) => a.localeCompare(b))
      .map(([itemKey, item]) => [
        itemKey,
        normalizeDraftPayload(item, itemKey),
      ]),
  );
}