All files / scripts/mcp-client document-types.ts

100% Statements 12/12
100% Branches 14/14
100% Functions 2/2
100% Lines 9/9

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 51 52 53 54 55 56 57 58 59 60 61 62                              9x                                                       24x 21x 21x                   7x 7x 7x 7x 7x    
/**
 * @module mcp-client/document-types
 * @description Riksdag document type normalisation. Maps raw Swedish
 * `doktyp` API codes to human-readable English type strings and stamps
 * documents with normalised `type`/`subtype` fields.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
/**
 * Map of Swedish riksdag API `doktyp` codes to normalized English type strings.
 *
 * Source: https://data.riksdagen.se/dokumentlista/dokumenttyper/
 */
const DOKTYP_TO_TYPE: Readonly<Record<string, string>> = {
  mot:      'motion',
  bet:      'committee-report',
  prop:     'proposition',
  skr:      'government-communication',
  ip:       'interpellation',
  fr:       'written-question',
  kammakt:  'chamber-action',
  prot:     'minutes',
  sfs:      'statute',
  sfst:     'statute-consolidated',
  ds:       'departmental-report',
  sou:      'government-inquiry',
  dir:      'committee-directive',
  yttr:     'external-opinion',
  fpm:      'eu-factsheet',
  utl:      'referral',
  redog:    'government-account',
  rskr:     'riksdag-communication',
  samtr:    'joint-committee-report',
};
 
/**
 * Normalize a raw `doktyp` API code to a human-readable `type` string.
 * Returns the code unchanged when not recognised, so unknown types remain
 * discoverable rather than silently dropping to 'unknown'.
 */
export function normalizeDocumentType(doktyp: string | undefined): string {
  if (!doktyp) return 'document';
  const lower = doktyp.toLowerCase().trim();
  return DOKTYP_TO_TYPE[lower] ?? lower;
}
 
/**
 * Stamp `type` and `subtype` fields onto a raw API document record in place.
 * - `type`    = normalised English label derived from `doktyp`
 * - `subtype` = mirrors the raw `subtyp` field (kept as-is; Swedish codes)
 * The raw `doktyp` field is preserved untouched for backward compatibility.
 */
export function annotateDocumentTypes(doc: Record<string, unknown>): Record<string, unknown> {
  const doktyp = typeof doc['doktyp'] === 'string' ? doc['doktyp'] : undefined;
  const subtyp = typeof doc['subtyp'] === 'string' ? doc['subtyp'] : undefined;
  if (!doc['type'])    doc['type']    = normalizeDocumentType(doktyp);
  if (!doc['subtype'] && subtyp) doc['subtype'] = subtyp;
  return doc;
}