2019-07-03 16:49:31 +00:00
|
|
|
import { React, T } from 'Components'
|
2019-06-11 10:03:45 +00:00
|
|
|
import { memoizeWith } from 'ramda'
|
|
|
|
import { serialiseUnit } from 'Engine/units'
|
2019-07-01 15:59:57 +00:00
|
|
|
import withLanguage from './utils/withLanguage'
|
2019-06-11 10:03:45 +00:00
|
|
|
|
2019-06-20 16:51:40 +00:00
|
|
|
const NumberFormat = memoizeWith(
|
|
|
|
(...args) => JSON.stringify(args),
|
|
|
|
Intl.NumberFormat
|
|
|
|
)
|
2019-06-11 10:03:45 +00:00
|
|
|
|
2019-06-20 16:51:40 +00:00
|
|
|
let numberFormatter = (style, numFractionDigits = 2) => (value, language) =>
|
2019-06-11 10:03:45 +00:00
|
|
|
NumberFormat(language, {
|
|
|
|
style,
|
|
|
|
currency: 'EUR',
|
2019-06-20 16:51:40 +00:00
|
|
|
maximumFractionDigits: numFractionDigits,
|
|
|
|
minimumFractionDigits: numFractionDigits
|
2019-06-11 10:03:45 +00:00
|
|
|
}).format(value)
|
|
|
|
|
2019-07-03 16:49:31 +00:00
|
|
|
// let booleanTranslations = { true: '✅', false: '❌' }
|
2019-06-20 16:51:40 +00:00
|
|
|
|
2019-06-11 10:03:45 +00:00
|
|
|
let booleanTranslations = {
|
|
|
|
fr: { true: 'Oui', false: 'Non' },
|
|
|
|
en: { true: 'Yes', false: 'No' }
|
|
|
|
}
|
|
|
|
|
2019-06-20 16:51:40 +00:00
|
|
|
let style = `
|
|
|
|
font-family: 'Courier New', Courier, monospace;
|
|
|
|
`
|
2019-06-11 10:03:45 +00:00
|
|
|
|
2019-07-01 15:59:57 +00:00
|
|
|
export default withLanguage(
|
|
|
|
({
|
|
|
|
nodeValue: value,
|
|
|
|
unit,
|
|
|
|
nilValueSymbol,
|
|
|
|
numFractionDigits,
|
|
|
|
children,
|
|
|
|
negative,
|
|
|
|
language
|
|
|
|
}) => {
|
|
|
|
/* Either an entire rule object is passed, or just the right attributes and the value as a JSX child*/
|
|
|
|
let nodeValue = value === undefined ? children : value
|
|
|
|
let valueType = typeof nodeValue,
|
|
|
|
unitText =
|
|
|
|
unit !== null && (typeof unit == 'object' ? serialiseUnit(unit) : unit),
|
|
|
|
formattedValue =
|
2019-07-03 16:49:31 +00:00
|
|
|
valueType === 'string' ? (
|
|
|
|
<T>{nodeValue}</T>
|
|
|
|
) : valueType === 'object' ? (
|
|
|
|
nodeValue.nom
|
2019-07-01 15:59:57 +00:00
|
|
|
) : valueType === 'boolean' ? (
|
2019-07-03 16:49:31 +00:00
|
|
|
booleanTranslations[language][nodeValue]
|
2019-07-01 15:59:57 +00:00
|
|
|
) : unit === '€' ? (
|
|
|
|
numberFormatter('currency', numFractionDigits)(nodeValue, language)
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
{numberFormatter('decimal', numFractionDigits)(nodeValue)}
|
|
|
|
|
|
|
|
{unitText}
|
|
|
|
</>
|
|
|
|
)
|
2019-06-20 16:51:40 +00:00
|
|
|
|
2019-07-01 15:59:57 +00:00
|
|
|
if (
|
|
|
|
(nilValueSymbol !== undefined && nodeValue === 0) ||
|
|
|
|
Number.isNaN(nodeValue)
|
|
|
|
)
|
|
|
|
return (
|
|
|
|
<span css={style} className="value">
|
|
|
|
-
|
|
|
|
</span>
|
|
|
|
)
|
2019-06-20 16:51:40 +00:00
|
|
|
|
2019-07-01 15:59:57 +00:00
|
|
|
return nodeValue == undefined ? null : (
|
|
|
|
<span css={style} className="value">
|
|
|
|
{negative ? '-' : ''}
|
|
|
|
{formattedValue}
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|