All files / scripts/generate-news-indexes/helpers article-merge.ts

100% Statements 15/15
62.5% Branches 5/8
100% Functions 5/5
100% Lines 15/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 44 45 46 47 48 49 50                                            2x   2x 28x 11134x 11134x 974x   11134x 11134x         2x   2x 28x 11134x 11134x 11134x         2x    
/**
 * @module generate-news-indexes/helpers/article-merge
 * @description Cross-language merge helpers. Builds the slug → languages map
 * used by the index renderer to present "available in" badges.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import type { NewsArticleMetadata } from '../types.js';
import { LANG_SUFFIX_RE } from './slug.js';
 
/**
 * Build map of base slugs to available languages for cross-language discovery.
 *
 * O(n) implementation: two-pass approach avoids the previous O(n²) nested
 * iteration that caused timeouts when the news/ directory grew large.
 *
 * Pass 1 – baseSlug → string[] of languages that have that article.
 * Pass 2 – article.slug → the language list from pass 1.
 */
export function buildSlugToLanguagesMap(articlesByLang: Record<string, NewsArticleMetadata[]>): Record<string, string[]> {
  const baseSlugToLangs: Record<string, string[]> = {};
 
  Object.entries(articlesByLang).forEach(([lang, articles]) => {
    articles.forEach((article) => {
      const baseSlug: string = article.slug.replace(LANG_SUFFIX_RE, '.html');
      if (!baseSlugToLangs[baseSlug]) {
        baseSlugToLangs[baseSlug] = [];
      }
      Eif (!baseSlugToLangs[baseSlug]!.includes(lang)) {
        baseSlugToLangs[baseSlug]!.push(lang);
      }
    });
  });
 
  const slugToLanguages: Record<string, string[]> = {};
 
  Object.entries(articlesByLang).forEach(([lang, articles]) => {
    articles.forEach((article) => {
      Eif (!slugToLanguages[article.slug]) {
        const baseSlug: string = article.slug.replace(LANG_SUFFIX_RE, '.html');
        slugToLanguages[article.slug] = baseSlugToLangs[baseSlug] ?? [lang];
      }
    });
  });
 
  return slugToLanguages;
}