Skip to content
Snippets Groups Projects
Select Git revision
  • f5baf710e7e443d68105b71e12e0e8f005ce125e
  • master default protected
  • 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
  • 270-concevoir-la-page-d-accueil-leximpact
  • 219-conversion-des-montants-min-et-max-de-l-axe-des-x-en-smic
  • 0.0.1177
  • 0.0.1176
  • 0.0.1175
  • 0.0.1174
  • 0.0.1173
  • 0.0.1172
  • 0.0.1171
  • 0.0.1170
  • 0.0.1169
  • 0.0.1168
  • 0.0.1167
  • 0.0.1166
  • 0.0.1165
  • 0.0.1164
  • 0.0.1163
  • 0.0.1162
  • 0.0.1161
  • 0.0.1160
  • 0.0.1159
  • 0.0.1158
41 results

postcss.config.js

Blame
  • values.ts 2.62 KiB
    import { getUnitByName } from "$lib/units"
    
    export function formatValue(value: number, unitName?: string): string {
      return valueFormatter(value, unitName)(value)
    }
    
    /// This function is needed, because Intl.NumberFormat option
    /// `signDisplay: "negative"` is not yet supported by most browsers.
    export function removeNegativeZero(value: number): number {
      return Object.is(value, -0) ? 0 : value
    }
    
    export function valueFormatter(
      baseValue: unknown,
      unitName?: string | undefined | null,
      compact = false,
    ): (value: unknown) => string {
      const unit = getUnitByName(unitName)
      return baseValue === undefined
        ? ((() => "") as (value: unknown) => string)
        : typeof baseValue === "boolean"
          ? (((value: boolean) => (value ? "vrai" : "faux")) as (
              value: unknown,
            ) => string)
          : typeof baseValue === "number"
            ? unit?.ratio // rate
              ? (value: unknown): string =>
                  new Intl.NumberFormat("fr-FR", {
                    maximumFractionDigits: 2,
                    minimumFractionDigits: 2,
                    style: "percent",
                  }).format(removeNegativeZero(value as number))
              : unitName != null && unitName.startsWith("currency-")
                ? compact
                  ? (value: unknown): string =>
                      new Intl.NumberFormat("fr-FR", {
                        currency: unitName.replace(/^currency-/, ""),
                        maximumFractionDigits: 2,
                        minimumFractionDigits: 0,
                        notation: "compact", // Example: 3,2 Md €
                        style: "currency",
                      }).format(removeNegativeZero(Math.round(value as number)))
                  : (value: unknown): string =>
                      new Intl.NumberFormat("fr-FR", {
                        currency: unitName.replace(/^currency-/, ""),
                        maximumFractionDigits: 0,
                        minimumFractionDigits: 0,
                        style: "currency",
                      }).format(removeNegativeZero(value as number))
                : unitName === "people" && compact
                  ? (value: unknown): string =>
                      new Intl.NumberFormat("fr-FR", {
                        maximumFractionDigits: 1,
                        minimumFractionDigits: 0,
                        notation: "compact", // Example: 3,2 M
                      }).format(removeNegativeZero(value as number))
                  : (value: unknown): string =>
                      new Intl.NumberFormat("fr-FR").format(
                        removeNegativeZero(value as number),
                      )
            : (((value: unknown) => (value as string).toString()) as (
                value: unknown,
              ) => string)
    }