All files / src/utils article-category.ts

90.9% Statements 30/33
75.6% Branches 31/41
100% Functions 1/1
95% Lines 19/20

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                                  16x                   31x 31x 17x 17x 17x 17x 16x 16x 16x 16x 16x   16x 14x 1x 13x 13x 11x 11x    
// SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
 
/**
 * @module Utils/ArticleCategory
 * @description Shared slug→ArticleCategory detection logic.
 *
 * Extracted here so that both `generators/news-indexes.ts` and
 * `utils/news-metadata.ts` can reuse the same classification without
 * creating a circular dependency.
 */
 
import { ArticleCategory } from '../types/index.js';
 
/**
 * Default category for articles that don't match specific patterns.
 */
const DEFAULT_CATEGORY = ArticleCategory.WEEK_AHEAD;
 
/**
 * Detect the article category from a slug.
 * Returns the matching {@link ArticleCategory} value used for badge/accent colours.
 *
 * @param slug - Hyphenated slug string
 * @returns Matching ArticleCategory value
 */
export function detectCategory(slug: string): ArticleCategory {
  const s = slug.toLowerCase();
  if (s.includes('week-ahead')) return ArticleCategory.WEEK_AHEAD;
  Iif (s.includes('month-ahead')) return ArticleCategory.MONTH_AHEAD;
  Iif (s.includes('quarter-ahead')) return ArticleCategory.QUARTER_AHEAD;
  Iif (s.includes('year-ahead')) return ArticleCategory.YEAR_AHEAD;
  if (s.includes('week-in-review')) return ArticleCategory.WEEK_IN_REVIEW;
  Iif (s.includes('month-in-review')) return ArticleCategory.MONTH_IN_REVIEW;
  Iif (s.includes('quarter-in-review')) return ArticleCategory.QUARTER_IN_REVIEW;
  Iif (s.includes('year-in-review')) return ArticleCategory.YEAR_IN_REVIEW;
  Iif (s.includes('term-outlook')) return ArticleCategory.TERM_OUTLOOK;
  Iif (s.includes('election-cycle') || s.includes('electoral-cycle'))
    return ArticleCategory.ELECTION_CYCLE;
  if (s.includes('committee')) return ArticleCategory.COMMITTEE_REPORTS;
  if (s.includes('motion') || s.includes('vote') || s.includes('voting'))
    return ArticleCategory.MOTIONS;
  Iif (s.includes('propos') || s.includes('legislat')) return ArticleCategory.PROPOSITIONS;
  if (s.includes('breaking') || s.includes('urgent')) return ArticleCategory.BREAKING_NEWS;
  Iif (s.includes('deep-analysis') || s.includes('5-whys')) return ArticleCategory.DEEP_ANALYSIS;
  return DEFAULT_CATEGORY;
}