All files / web/src/utils model-sorting.ts

100% Statements 14/14
92.3% Branches 12/13
100% Functions 2/2
100% Lines 14/14

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                                1x   37x 37x     37x 37x 29x 29x     37x 37x   37x 37x           1x 8x 8x  
/**
 * Utility function to sort model options alphabetically.
 * Sorts primarily by model name, then by provider name when model names are equal.
 */
 
interface SortableModel {
  modelName?: string;
  name?: string;
  providerName?: string;
  provider?: string;
}
 
/**
 * Comparator function for sorting models.
 * Sorts by model name first (alphabetically), then by provider name (alphabetically).
 */
export function compareModels<T extends SortableModel>(a: T, b: T): number {
  // Get model names (support both modelName and name properties)
  const modelNameA = (a.modelName ?? a.name ?? '').toLowerCase();
  const modelNameB = (b.modelName ?? b.name ?? '').toLowerCase();
 
  // Compare model names first
  const modelComparison = modelNameA.localeCompare(modelNameB);
  if (modelComparison !== 0) {
    return modelComparison;
  }
 
  // If model names are equal, compare provider names
  const providerNameA = (a.providerName ?? a.provider ?? '').toLowerCase();
  const providerNameB = (b.providerName ?? b.provider ?? '').toLowerCase();
 
  return providerNameA.localeCompare(providerNameB);
}
 
/**
 * Sorts an array of models alphabetically by model name, then by provider name.
 * Returns a new sorted array (does not mutate the original).
 */
export function sortModels<T extends SortableModel>(models: T[]): T[] {
  return [...models].sort(compareModels);
}