Skip to content
Snippets Groups Projects
Select Git revision
  • f2a19a017c0f260e8f0853cde989ad9cae833173
  • master default protected
  • suppression_allegements_specifiques
  • budgetaire_retraites_plf
  • doc-script-gen-off-tests
  • 366-signe-a-cote-du-droit-en-vigueur-sur-l-ui-pour-indiquer-que-la-reforme-a-eu-lieu-mais-qu-elle-n
  • revalo_retraites
  • 381-pb-affichage-labels-des-parametres-sur-plus-de-3-lignes
  • ajoute-duplicate-aide-logement
  • poc_castype_ia
  • parametres-editables-budget
  • ui-parametres
  • 355-les-dispositifs-prestations-sociales-du-graphique-se-cachent-montrent-en-meme-temps-2
  • 358-les-variables-dont-le-montant-est-nul-apparaissent-en-bleu-et-non-cliquables
  • 356-ajuster-la-largeur-sur-les-graphiques-budgetaires
  • incoherence_cas_type_0
  • fix-ui-suppression-tranches-baremes
  • ajout-agregat-cehr-version-plf
  • impact_carbone
  • xlsx
  • header_revamp
  • 0.0.1191
  • 0.0.1190
  • 0.0.1189
  • 0.0.1188
  • 0.0.1187
  • 0.0.1186
  • 0.0.1185
  • 0.0.1184
  • 0.0.1183
  • 0.0.1182
  • 0.0.1181
  • 0.0.1180
  • 0.0.1179
  • 0.0.1178
  • 0.0.1177
  • 0.0.1176
  • 0.0.1175
  • 0.0.1174
  • 0.0.1173
  • 0.0.1172
41 results

units.ts

Blame
  • units.ts 1.85 KiB
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    import unitsUnknown from "@leximpact/socio-fiscal-openfisca-json/units.yaml"
    import {
      getUnitAtDate as getUnitAtDateOriginal,
      getUnitLabel as getUnitLabelOriginal,
      getUnitShortLabel as getUnitShortLabelOriginal,
      type ConstantUnit,
      type PeriodUnit,
      type Unit,
    } from "@openfisca/json-model"
    
    const frenchPluralRules = new Intl.PluralRules(["fr-FR"])
    export const units = unitsUnknown as Unit[]
    const unitByName = Object.fromEntries(units.map((unit) => [unit.name, unit]))
    
    export function getUnitAtDate(
      name: string | undefined | null,
      date: string,
    ): ConstantUnit | undefined {
      return getUnitAtDateOriginal(unitByName, name, date)
    }
    
    export function getUnitByName(
      name: string | undefined | null,
    ): Unit | undefined {
      if (name === undefined || name === null) {
        return undefined
      }
      return unitByName[name]
    }
    
    export function getUnitLabel(
      name: string | undefined | null,
      date: string,
      { periodUnit, value = 123 }: { periodUnit?: PeriodUnit; value?: number } = {},
    ): string {
      const unitLabel = getUnitLabelOriginal(
        unitByName,
        name,
        date,
        frenchPluralRules.select(value ?? 0),
      )
      if (
        unitLabel === "" ||
        periodUnit === undefined ||
        !["day", "month", "year"].includes(periodUnit)
      ) {
        return unitLabel
      }
      return `${unitLabel}/an`
    }
    
    export function getUnitShortLabel(
      name: string | undefined | null,
      date: string,
      { periodUnit, value = 123 }: { periodUnit?: PeriodUnit; value?: number } = {},
    ): string {
      const unitShortLabel = getUnitShortLabelOriginal(
        unitByName,
        name,
        date,
        frenchPluralRules.select(value ?? 0),
      )
      if (
        unitShortLabel === "" ||
        periodUnit === undefined ||
        !["day", "month", "year"].includes(periodUnit)
      ) {
        return unitShortLabel
      }
      return `${unitShortLabel}/an`
    }