| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- export type TextDisplayFormat = {
- prefix?: string
- suffix?: string
- emptyText?: string
- }
- export type TextDeviceFormat = {
- decimalPlaces?: number
- showUnit?: boolean
- }
- export const formatTextDisplayValue = (
- value: unknown,
- { prefix = '', suffix = '', emptyText = '--' }: TextDisplayFormat = {}
- ) => {
- const text = value === null || value === undefined ? '' : String(value)
- if (text === '') {
- return emptyText
- }
- return `${prefix}${text}${suffix}`
- }
- export const formatTextDeviceValue = (
- value: unknown,
- unit: string | undefined,
- { decimalPlaces = 2, showUnit = true }: TextDeviceFormat = {}
- ) => {
- if (value === null || value === undefined || value === '') {
- return ''
- }
- const precision = Math.min(Math.max(Math.trunc(decimalPlaces), 0), 6)
- const text =
- typeof value === 'number' && Number.isFinite(value) ? value.toFixed(precision) : String(value)
- if (!showUnit || !unit) {
- return text
- }
- return `${text} ${unit}`
- }
|