All files / src/utils utils.ts

83.16% Statements 84/101
75.3% Branches 61/81
90.9% Functions 10/11
82.6% Lines 76/92

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227        315x                   17x 3x   11x       22x 11x 11x 9x   11x                                 93x 93x   93x 93x   93x   93x 93x 93x 93x 93x 37x 37x 37x 37x             93x 56x 56x 56x                               93x 56x 84x 84x 84x 4x 4x 4x     4x   4x         93x   93x                         91x 91x   91x 91x 91x     197x           91x     91x 5x                 86x   1x   83x   1x   1x                                                           120x     119x 8x 3x 3x       5x 2x 2x 2x 2x 2x 2x           3x 3x 3x 3x 3x   3x 3x   3x 2x 1x                 58x    
import Plotly from 'plotly.js-dist-min';
 
/* normalize key: “tango://host:port/device/attr” → “device/attr” */
export function canonicalAttr(name: string): string {
  return name.replace(/^tango:\/\/[^/]+\/(.+)$/, '$1');
}
 
/* Coerce to number; non-numeric → NaN */
export function toNum(v: any): number {
  const n = typeof v === 'number' ? v : Number(v);
  return Number.isFinite(n) ? n : NaN;
}
 
export function parseMargins(str?: string): Partial<Plotly.Layout['margin']> {
  if (!str) return {};
  return str
    .split(',')
    .map((pair) => pair.trim())
    .filter(Boolean)
    .reduce(
      (acc, part) => {
        const [key, val] = part.split(':').map((s) => s.trim());
        const num = Number(val);
        if (['t', 'r', 'b', 'l', 'pad'].includes(key) && Number.isFinite(num)) {
          (acc as any)[key] = num;
        }
        return acc;
      },
      {} as Partial<Plotly.Layout['margin']>
    );
}
 
/**
 * Extract `{station}` and `{number}`:
 * - `{number}` = digits immediately after "tpm" (e.g. tpm01 → "01")
 * - `{station}` = path without the trailing "-tpmNN" suffix on the last device segment
 *
 * Example:
 *   key = "low-mccs/tile/s8-1-tpm01/fpga1temperature"
 *   station = "low-mccs/tile/s8-1"
 *   number  = "01"
 */
function extractStationAndNumber(attr: string): { station: string; number: string } {
  const key = canonicalAttr(attr);
  const parts = key.split('/');
 
  const deviceParts = parts.slice(0, -1);
  const stationParts = [...deviceParts];
 
  let number = '';
 
  const lastIdx = stationParts.length - 1;
  Eif (lastIdx >= 0) {
    const lastSeg = stationParts[lastIdx];
    const localTpm = lastSeg.match(/tpm(\d+)\b/i);
    if (localTpm) {
      number = localTpm[1];
      const cleaned = lastSeg.replace(/-?tpm\d+\b/i, '');
      if (cleaned) {
        stationParts[lastIdx] = cleaned;
      } else E{
        stationParts.splice(lastIdx, 1);
      }
    }
  }
 
  if (!number && stationParts.length > 0) {
    const joined = stationParts.join('/');
    const globalTpm = joined.match(/(?:^|[/-])tpm(\d+)\b/i);
    Iif (globalTpm) {
      number = globalTpm[1];
      for (let i = stationParts.length - 1; i >= 0; i--) {
        if (/tpm\d+/i.test(stationParts[i])) {
          const cleaned = stationParts[i].replace(/-?tpm\d+\b/i, '');
          if (cleaned) {
            stationParts[i] = cleaned;
          } else {
            stationParts.splice(i, 1);
          }
          break;
        }
      }
    }
  }
 
  if (!number && stationParts.length > 0) {
    for (let i = stationParts.length - 1; i >= 0; i--) {
      const seg = stationParts[i];
      const numericMatch = /(\d+)(?!.*\d)/.exec(seg);
      if (numericMatch) {
        number = numericMatch[1];
        const prefix = seg.slice(0, numericMatch.index).replace(/[-_/]+$/g, '');
        Iif (prefix) {
          stationParts[i] = prefix;
        } else {
          stationParts.splice(i, 1);
        }
        break;
      }
    }
  }
 
  const station = stationParts.join('/').replace(/\/+$/g, '');
 
  return { station, number };
}
 
/**
 * Format a legend/label from an attribute path.
 *
 * Supported placeholders in `format`:
 *   {fullname}, {lastname}, {initialname}, {number}, {station}
 *
 * Modes without braces also supported (back-compat):
 *   "fullname" | "lastname" | "initialname" | "number"
 */
export function formatLegendName(attr: string, format: string): string {
  const key = canonicalAttr(attr);
  const parts = key.split('/');
 
  const fullname = key;
  const lastname = parts[parts.length - 1] ?? '';
  const initialname = (
    parts
      .slice(0, -1)
      .map((p) => (p ? p[0] : ''))
      .join('/') +
    '/' +
    lastname
  ).replace(/^\/+/, '');
 
  const { station, number } = extractStationAndNumber(attr);
 
  // If format contains placeholders, replace them
  if (/\{.*\}/.test(format)) {
    return format
      .replace(/\{fullname\}/gi, fullname)
      .replace(/\{lastname\}/gi, lastname)
      .replace(/\{initialname\}/gi, initialname)
      .replace(/\{number\}/gi, number)
      .replace(/\{station\}/gi, station);
  }
 
  // Fallback to mode matching if no placeholders
  switch ((format || '').toLowerCase()) {
    case 'fullname':
      return fullname;
    case 'lastname':
      return lastname;
    case 'initialname':
      return initialname;
    case 'number':
      return number;
    default:
      return fullname;
  }
}
 
export { extractStationAndNumber }; // exported in case we want it elsewhere
 
/* ────────────────────────────────────────────────────────────────── */
/*                           Min/Max helpers                          */
/* ────────────────────────────────────────────────────────────────── */
 
export type MinMaxLike =
  | number
  | string
  | { min?: number; max?: number }
  | [number, number]
  | null
  | undefined;
 
/**
 * Try to coerce a value coming from HDB++ into:
 *  - y: a scalar (mid = (min+max)/2 when both exist)
 *  - min/max: when available
 */
export function coerceMinMax(value: MinMaxLike): {
  y: number | null;
  min?: number;
  max?: number;
} {
  if (value === null || value === undefined) return { y: null };
 
  // plain number or numeric string
  if (typeof value === 'number') return { y: Number.isFinite(value) ? value : null };
  if (typeof value === 'string') {
    const num = Number(value);
    return { y: Number.isFinite(num) ? num : null };
  }
 
  // tuple [min, max]
  if (Array.isArray(value) && value.length >= 2) {
    const a = Number(value[0]);
    const b = Number(value[1]);
    Eif (Number.isFinite(a) && Number.isFinite(b)) {
      const min = Math.min(a, b);
      const max = Math.max(a, b);
      return { y: (min + max) / 2, min, max };
    }
    return { y: null };
  }
 
  // object {min, max}
  Eif (typeof value === 'object') {
    const minRaw = (value as any).min;
    const maxRaw = (value as any).max;
    const min = Number(minRaw);
    const max = Number(maxRaw);
 
    const hasMin = Number.isFinite(min);
    const hasMax = Number.isFinite(max);
 
    if (hasMin && hasMax) return { y: (min + max) / 2, min, max };
    if (hasMin && !hasMax) return { y: min, min };
    Eif (!hasMin && hasMax) return { y: max, max };
    return { y: null };
  }
 
  return { y: null };
}
 
/** Convenience: scalar numeric from arbitrary HDB++ value (or null) */
export function numericValue(value: MinMaxLike): number | null {
  return coerceMinMax(value).y;
}