All files / src/data UnitsLibrary.ts

70.37% Statements 19/27
60% Branches 9/15
100% Functions 3/3
76.47% Lines 13/17

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              24982x 24961x                     41498x 42250x 41477x 41454x       23x   5980x 1265x   23x       208x 208x 211x                
import { UnitTypeEnum } from "@/api/apiStore.gen";
import unitsLibrary from "@/data/units.json";
 
/**
 * Get the available unit options for a given unit type
 */
export function GetUnitSet(unitType: UnitTypeEnum | undefined): UnitItem[] {
  if (!unitType || unitType === "dimensionless") return [];
  return unitsLibrary[unitType] || [];
}
 
/**
 * Get a unit display string for a given unit type and unit
 */
export function GetUnitDisplay(unitSet: UnitItem[], unit: string): string {
  if (unit == "" && unitSet.length > 0) {
    // default unit for now until they get set in the backend
    return unitSet[0].label;
  }
  if (!unit) return "Undefined";
  const unitObj = unitSet.find((u) => u.value === unit);
  if (!unitObj) return GetUnitLabel(unit);
  return unitObj.label;
}
 
export function GetUnitLabel(unit: string | null | undefined): string {
  Iif (!unit) return "";
  for (const unitSet of Object.values(unitsLibrary)) {
    const unitObj = unitSet.find((candidate) => candidate.value === unit);
    Iif (unitObj) return unitObj.label;
  }
  return unit;
}
 
export function GetQuickUnits(unitType, unit) {
  const unitSet = unitsLibrary[unitType];
  Iif (!unitSet) return GetUnitLabel(unit);
  return unitSet.find((u) => u.value === unit)?.label ?? GetUnitLabel(unit);
}
 
export function GetDefaultUnit(unitType: UnitTypeEnum) {
  const unitSet = unitsLibrary[unitType];
  Iif (!unitSet) return "";
  return unitSet[0].value;
}