/** * Utility for formatting technical data values. * Handles long lists of standards and simplifies repetitive strings. */ export interface FormattedTechnicalValue { original: string; isList: boolean; parts: string[]; displayValue: string; } /** * Formats a technical value string. * Detects if it's a list (separated by / or ,) and tries to clean it up. */ export function formatTechnicalValue(value: string | null | undefined): FormattedTechnicalValue { if (!value) { return { original: '', isList: false, parts: [], displayValue: '' }; } const str = String(value).trim(); // Detect list separators let parts: string[] = []; if (str.includes(' / ')) { parts = str.split(' / ').map((p) => p.trim()); } else if (str.includes(' /')) { parts = str.split(' /').map((p) => p.trim()); } else if (str.includes('/ ')) { parts = str.split('/ ').map((p) => p.trim()); } else if (str.split('/').length > 2) { // Check if it's actually many standards separated by / without spaces // e.g. EN123/EN456/EN789 const split = str.split('/'); if (split.length > 3) { parts = split.map((p) => p.trim()); } } // If no parts found yet, try comma if (parts.length === 0 && str.includes(', ')) { parts = str.split(', ').map((p) => p.trim()); } // Filter out empty parts parts = parts.filter(Boolean); // If we have parts, let's see if we can simplify them if (parts.length > 2) { // Find common prefix to condense repetitive standards let commonPrefix = ''; const first = parts[0]; const last = parts[parts.length - 1]; let i = 0; while (i < first.length && first.charAt(i) === last.charAt(i)) { i++; } commonPrefix = first.substring(0, i); // If a meaningful prefix exists (e.g., "EN 60 332-1-") if (commonPrefix.length > 4) { const suffixParts: string[] = []; for (let idx = 0; idx < parts.length; idx++) { if (idx === 0) { suffixParts.push(parts[idx]); } else { const suffix = parts[idx].substring(commonPrefix.length).trim(); if (suffix) { suffixParts.push(suffix); } } } // Condense into a single string like "EN 60 332-1-2 / -3 / -4" // Wait, returning a single string might still wrap badly. // Instead, we return them as chunks or just a condensed string. const condensedString = suffixParts[0] + ' / -' + suffixParts.slice(1).join(' / -'); return { original: str, isList: false, // Turn off badge rendering to use text block instead parts: [condensedString], displayValue: condensedString, }; } // If no common prefix, return as list so UI can render badges return { original: str, isList: true, parts, displayValue: parts.join(', '), }; } return { original: str, isList: false, parts: [str], displayValue: str, }; }