From f0f21239242494c0f3441c2db29e299f57259f45 Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Thu, 26 Sep 2019 10:06:46 +0200 Subject: [PATCH] Revert "Simplification de Engine/format" This reverts commit 3971496e2854ed3199e8fe8453eb58bedbc722c3. --- source/components/PercentageField.js | 2 +- source/engine/format.js | 48 +++++++++++++++----- source/engine/mecanismViews/BarèmeContinu.js | 4 +- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/source/components/PercentageField.js b/source/components/PercentageField.js index 11ffb8721..0418a49ff 100644 --- a/source/components/PercentageField.js +++ b/source/components/PercentageField.js @@ -26,7 +26,7 @@ export default function PercentageField({ onChange, value, debounce }) { max="1" /> - {formatPercentage(localValue)} + {formatPercentage(localValue)} % ) diff --git a/source/engine/format.js b/source/engine/format.js index 75c8ff258..9e2c7d9c5 100644 --- a/source/engine/format.js +++ b/source/engine/format.js @@ -19,6 +19,12 @@ export let numberFormatter = ({ minimumFractionDigits }).format(value) +export const formatCurrency = (value, language) => { + return value == null + ? '' + : numberFormatter({ language })(value).replace(/^(-)?€/, '$1€\u00A0') +} + export const currencyFormat = language => ({ isCurrencyPrefixed: !!numberFormatter({ language, style: 'currency' })( 12 @@ -27,16 +33,34 @@ export const currencyFormat = language => ({ decimalSeparator: formatCurrency(0.1, language).charAt(1) }) -export const formatCurrency = (value, language) => { - return value == null - ? '' - : formatValue({ unit: '€', language, value }).replace(/^(-)?€/, '$1€\u00A0') -} +const sanitizeValue = language => value => + language === 'fr' ? String(value).replace(',', '.') : value -export const formatPercentage = value => - value == null - ? '' - : formatValue({ unit: '%', value, maximumFractionDigits: 2 }) +export const formatPercentage = value => +(value * 100).toFixed(2) +export const normalizePercentage = value => value / 100 + +export const getFormatersFromUnit = (unit, language = 'en') => { + const serializedUnit = typeof unit == 'object' ? serialiseUnit(unit) : unit + const sanitize = sanitizeValue(language) + switch (serializedUnit) { + case '%': + return { + format: v => + numberFormatter({ style: 'percent', language })(v) + .replace('%', '') + .trim(), + normalize: v => normalizePercentage(sanitize(v)) + } + default: + return { + format: x => + Number(x) + ? numberFormatter({ style: 'decimal', language })(Number(x)) + : x, + normalize: x => sanitize(x) + } + } +} export function formatValue({ maximumFractionDigits, @@ -45,9 +69,6 @@ export function formatValue({ unit, value }) { - if (typeof value !== 'number') { - return value - } const serializedUnit = typeof unit == 'object' ? serialiseUnit(unit) : unit switch (serializedUnit) { @@ -61,6 +82,9 @@ export function formatValue({ case '%': return numberFormatter({ style: 'percent', maximumFractionDigits })(value) default: + if (typeof value !== 'number') { + return value + } return ( numberFormatter({ style: 'decimal', diff --git a/source/engine/mecanismViews/BarèmeContinu.js b/source/engine/mecanismViews/BarèmeContinu.js index 19d17d243..e803e9c25 100644 --- a/source/engine/mecanismViews/BarèmeContinu.js +++ b/source/engine/mecanismViews/BarèmeContinu.js @@ -1,11 +1,11 @@ import { ShowValuesConsumer } from 'Components/rule/ShowValuesContext' -import { formatPercentage } from 'Engine/format' import { sortObjectByKeys } from 'Engine/mecanismViews/common' import React from 'react' import { Trans } from 'react-i18next' import { BarèmeAttributes } from './Barème' import './Barème.css' import { Node } from './common' +import { formatPercentage } from 'Engine/format' let Comp = function Barème({ nodeValue, explanation, unit }) { return ( @@ -44,7 +44,7 @@ let Comp = function Barème({ nodeValue, explanation, unit }) { Votre taux :{' '} - {formatPercentage(explanation.taux)} + {formatPercentage(explanation.taux)} % )} {explanation.returnRate && (