SPB Git

Repository Public

regions.ts 4.9 KB 155 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : UQO Éval — Le Grand Défi Immobilier8 * File             : packages/engine/src/regions.ts9 * Module           : engine10 * Repository       : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website  : https://jeux.uqo-eval.app12 * Created          : 2026-11-2513 * Last Modified    : 2026-11-2514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/**44 * Régions administratives et municipalités du Québec.45 * Source : Répertoire des municipalités (MAMH, données ouvertes du Québec).46 */47import municipalitiesRaw from "./reference/municipalities.json" with { type: "json" };4849export const REGIONS: Record<string, string> = {50  "01": "Bas-Saint-Laurent",51  "02": "Saguenay–Lac-Saint-Jean",52  "03": "Capitale-Nationale",53  "04": "Mauricie",54  "05": "Estrie",55  "06": "Montréal",56  "07": "Outaouais",57  "08": "Abitibi-Témiscamingue",58  "09": "Côte-Nord",59  "10": "Nord-du-Québec",60  "11": "Gaspésie–Îles-de-la-Madeleine",61  "12": "Chaudière-Appalaches",62  "13": "Laval",63  "14": "Lanaudière",64  "15": "Laurentides",65  "16": "Montérégie",66  "17": "Centre-du-Québec",67};6869export interface Municipality {70  code: string;71  name: string;72  regionCode: string;73  mrc: string;74}7576/** Clé de comparaison : minuscules, sans accents, abréviations développées. */77export function normalizeName(value: string): string {78  return value79    .normalize("NFD")80    .replace(/[̀-ͯ]/g, "")81    .toLowerCase()82    .replace(/[’'`]/g, " ")83    .replace(/[-–—_/.,]/g, " ")84    .replace(/\bste\b/g, "sainte")85    .replace(/\bst\b/g, "saint")86    .replace(/\s+/g, " ")87    .trim();88}8990const byCode = new Map<string, Municipality>();91const byName = new Map<string, Municipality | null>();9293for (const row of municipalitiesRaw as (string | null)[][]) {94  const [code, name, regionCode, mrc] = row;95  if (!code || !name || !regionCode) continue;96  const m: Municipality = { code, name, regionCode, mrc: mrc ?? "" };97  byCode.set(code, m);98  const key = normalizeName(name);99  // Un nom porté par plusieurs municipalités est ambigu : on ne tranche pas.100  byName.set(key, byName.has(key) ? null : m);101}102103export function municipalityByCode(code: string | null | undefined): Municipality | null {104  if (!code) return null;105  return byCode.get(code) ?? null;106}107108export function municipalityByName(name: string | null | undefined): Municipality | null {109  if (!name) return null;110  return byName.get(normalizeName(name)) ?? null;111}112113/** Étiquettes de région rencontrées à la source, lorsqu'elles sont sans ambiguïté. */114const LABEL_TO_CODE: Record<string, string> = {115  "bas saint laurent": "01",116  "saguenay lac saint jean": "02",117  "capitale nationale": "03",118  "region de quebec": "03",119  charlevoix: "03",120  portneuf: "03",121  mauricie: "04",122  estrie: "05",123  "the estrie": "05",124  "eastern townships": "05",125  montreal: "06",126  "montreal ile": "06",127  outaouais: "07",128  "abitibi temiscamingue": "08",129  "cote nord": "09",130  "nord du quebec": "10",131  "gaspesie iles de la madeleine": "11",132  "chaudiere appalaches": "12",133  laval: "13",134  lanaudiere: "14",135  laurentides: "15",136  "the laurentians": "15",137  monteregie: "16",138  "monteregie ouest": "16",139  "monteregie rive sud montreal": "16",140  "rive sud de montreal": "16",141  "centre du quebec": "17",142};143144export function regionCodeFromLabel(label: string | null | undefined): string | null {145  if (!label) return null;146  const trimmed = label.trim();147  if (/^\d{2}$/.test(trimmed)) return REGIONS[trimmed] ? trimmed : null;148  return LABEL_TO_CODE[normalizeName(trimmed)] ?? null;149}150151export function regionName(code: string | null | undefined): string | null {152  if (!code) return null;153  return REGIONS[code] ?? null;154}155
Files main
Menu

Theme