2019-06-11 10:03:45 +00:00
|
|
|
import React from 'react'
|
|
|
|
import { memoizeWith } from 'ramda'
|
|
|
|
import { serialiseUnit } from 'Engine/units'
|
|
|
|
|
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-06-20 16:51:40 +00:00
|
|
|
let booleanTranslations = { true: '✅', false: '✘' }
|
|
|
|
|
|
|
|
/* Or maybe this :
|
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
|
|
|
*/
|
2019-06-11 10:03:45 +00:00
|
|
|
|
2019-06-20 16:51:40 +00:00
|
|
|
let style = `
|
|
|
|
border: 2px dashed chartreuse
|
|
|
|
font-family: 'Courier New', Courier, monospace;
|
|
|
|
`
|
2019-06-11 10:03:45 +00:00
|
|
|
|
2019-06-20 16:51:40 +00:00
|
|
|
export default ({
|
|
|
|
nodeValue: value,
|
|
|
|
unit,
|
|
|
|
nilValueSymbol,
|
|
|
|
numFractionDigits,
|
|
|
|
children,
|
|
|
|
negative
|
|
|
|
}) => {
|
|
|
|
/* 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
|
2019-06-11 10:03:45 +00:00
|
|
|
let valueType = typeof nodeValue,
|
2019-06-20 16:51:40 +00:00
|
|
|
unitText =
|
|
|
|
unit !== null && (typeof unit == 'object' ? serialiseUnit(unit) : unit),
|
|
|
|
formattedValue =
|
|
|
|
valueType === 'object' ? (
|
|
|
|
JSON.stringify(nodeValue)
|
|
|
|
) : valueType === 'boolean' ? (
|
|
|
|
booleanTranslations[nodeValue]
|
|
|
|
) : unit === '€' ? (
|
|
|
|
numberFormatter('currency', numFractionDigits)(nodeValue)
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
{numberFormatter('decimal', numFractionDigits)(nodeValue)}
|
|
|
|
|
|
|
|
{unitText}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
|
|
|
if (
|
|
|
|
(nilValueSymbol !== undefined && nodeValue === 0) ||
|
|
|
|
Number.isNaN(nodeValue)
|
|
|
|
)
|
|
|
|
return <span css={style}>-</span>
|
|
|
|
|
2019-06-11 10:03:45 +00:00
|
|
|
return nodeValue == undefined ? null : (
|
2019-06-20 16:51:40 +00:00
|
|
|
<span css={style}>
|
|
|
|
{negative ? '-' : ''}
|
|
|
|
{formattedValue}
|
|
|
|
</span>
|
2019-06-11 10:03:45 +00:00
|
|
|
)
|
|
|
|
}
|