Revert "Simplification de Engine/format"

This reverts commit 3971496e28.
pull/695/head
Maxime Quandalle 2019-09-26 10:06:46 +02:00
parent 3971496e28
commit f0f2123924
No known key found for this signature in database
GPG Key ID: 428641C03D29CA10
3 changed files with 39 additions and 15 deletions

View File

@ -26,7 +26,7 @@ export default function PercentageField({ onChange, value, debounce }) {
max="1"
/>
<span style={{ display: 'inline-block', width: '3em' }}>
{formatPercentage(localValue)}
{formatPercentage(localValue)} %
</span>
</div>
)

View File

@ -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',

View File

@ -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 }) {
<b>
<Trans>Votre taux </Trans> :{' '}
</b>
{formatPercentage(explanation.taux)}
{formatPercentage(explanation.taux)} %
</span>
)}
{explanation.returnRate && (