Skip to content
Snippets Groups Projects
Select Git revision
  • 0cda492c325d4d4845c0578b15885f96d85dd686
  • master default protected
  • maj-readme
  • update-budget-msg-2022
  • share-with-metadata
  • explications-resultats-macros-PLF2022
  • plf_fix
  • benoit-cty-master-patch-87290
  • activate-plf-2021
  • stats
  • switch-plf
  • carto
  • adapt-matomo
  • js-to-ts
  • nbre-part-logic
  • add-nbre-part
  • 1.0.0
17 results

next.config.js

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