All files / utils article-category.ts

84% Statements 21/25
70.96% Branches 22/31
100% Functions 1/1
93.33% Lines 14/15

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                                  2x                   12x 12x 3x 3x 3x 3x 3x 3x 2x   2x 2x 2x 2x    
// 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('year-ahead')) return ArticleCategory.YEAR_AHEAD;
  Iif (s.includes('week-in-review')) return ArticleCategory.WEEK_IN_REVIEW;
  Iif (s.includes('month-in-review')) return ArticleCategory.MONTH_IN_REVIEW;
  Iif (s.includes('year-in-review')) return ArticleCategory.YEAR_IN_REVIEW;
  if (s.includes('committee')) return ArticleCategory.COMMITTEE_REPORTS;
  Iif (s.includes('motion') || s.includes('vote') || s.includes('voting'))
    return ArticleCategory.MOTIONS;
  Iif (s.includes('propos') || s.includes('legislat')) return ArticleCategory.PROPOSITIONS;
  Iif (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;
}