From 5e5933d35dfe142b1728092213ad14a97ee2659e Mon Sep 17 00:00:00 2001 From: Maxime Quandalle Date: Thu, 26 Sep 2019 09:58:14 +0200 Subject: [PATCH] Simplification de Engine/format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit getFormatersFromUnit n'est plus utilisé depuis 5879c9bc --- .../components/CurrencyInput/CurrencyInput.js | 1 - source/components/PercentageField.js | 2 +- source/engine/format.js | 52 +++++-------------- source/engine/mecanismViews/BarèmeContinu.js | 4 +- 4 files changed, 17 insertions(+), 42 deletions(-) diff --git a/source/components/CurrencyInput/CurrencyInput.js b/source/components/CurrencyInput/CurrencyInput.js index cc307339a..0be897326 100644 --- a/source/components/CurrencyInput/CurrencyInput.js +++ b/source/components/CurrencyInput/CurrencyInput.js @@ -52,7 +52,6 @@ export default function CurrencyInput({ thousandSeparator, decimalSeparator } = currencyFormat(language) - console.log({ isCurrencyPrefixed }) // We display negative numbers iff this was the provided value (but we disallow the user to enter them) const valueHasChanged = currentValue !== initialValue diff --git a/source/components/PercentageField.js b/source/components/PercentageField.js index 0418a49ff..11ffb8721 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 9e2c7d9c5..56a1d8a93 100644 --- a/source/engine/format.js +++ b/source/engine/format.js @@ -19,49 +19,25 @@ 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 ).match(/^€/), - thousandSeparator: formatCurrency(1000, language).charAt(1), - decimalSeparator: formatCurrency(0.1, language).charAt(1) + thousandSeparator: numberFormatter({ language })(1000).charAt(1), + decimalSeparator: numberFormatter({ language })(0.1).charAt(1) }) -const sanitizeValue = language => value => - language === 'fr' ? String(value).replace(',', '.') : value - -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 const formatCurrency = (value, language) => { + return value == null + ? '' + : formatValue({ unit: '€', language, value }).replace(/^(-)?€/, '$1€\u00A0') } +export const formatPercentage = value => + value == null + ? '' + : formatValue({ unit: '%', value, maximumFractionDigits: 2 }) + export function formatValue({ maximumFractionDigits, minimumFractionDigits, @@ -69,6 +45,9 @@ export function formatValue({ unit, value }) { + if (typeof value !== 'number') { + return value + } const serializedUnit = typeof unit == 'object' ? serialiseUnit(unit) : unit switch (serializedUnit) { @@ -82,9 +61,6 @@ 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 e803e9c25..19d17d243 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 && (