Simplification de Engine/format

getFormatersFromUnit n'est plus utilisé depuis 5879c9bc
pull/700/head
Maxime Quandalle 2019-09-26 09:58:14 +02:00
parent 23aa3eac27
commit 5e5933d35d
No known key found for this signature in database
GPG Key ID: 428641C03D29CA10
4 changed files with 17 additions and 42 deletions

View File

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

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

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 && (