diff --git a/src/shared/formatters/numberformat.ts b/src/shared/formatters/numberformat.ts index bab1d568..cf0f71e7 100644 --- a/src/shared/formatters/numberformat.ts +++ b/src/shared/formatters/numberformat.ts @@ -1,9 +1,9 @@ -export function isUndefined(val): boolean { - return val === undefined || val === null; +export function isNotUndefined(val: T | undefined | null): val is T { + return !(val === undefined || val === null); } export function formatPercent(value: number, decimals = 3): string { - return !isUndefined(value) ? `${(value * 100).toFixed(decimals)}%` : ''; + return isNotUndefined(value) ? `${(value * 100).toFixed(decimals)}%` : ''; } /** @@ -12,10 +12,9 @@ export function formatPercent(value: number, decimals = 3): string { * @param decimals number of decimals (Defaults to 15) * @returns Formatted string */ -export function formatPrice(value: number, decimals = 15): string { +export function formatPrice(value: number | null, decimals = 15): string { // const format = new Intl.NumberFormat('', {maximumFractionDigits: decimals} - // return !isUndefined(value) ? parseFloat(value.toFixed(decimals)).toString() : ''; - return !isUndefined(value) + return isNotUndefined(value) ? value.toLocaleString('fullwide', { useGrouping: false, maximumFractionDigits: decimals, @@ -30,12 +29,12 @@ export function formatPrice(value: number, decimals = 15): string { * @param decimals Decimals * @returns */ -export function formatPriceCurrency(price, currency: string, decimals = 3) { +export function formatPriceCurrency(price: number | null, currency: string, decimals = 3) { return `${formatPrice(price, decimals)} ${currency}`; } export default { - isUndefined, + isNotUndefined, formatPrice, formatPercent, }; diff --git a/src/shared/formatters/timeformat.ts b/src/shared/formatters/timeformat.ts index f52d2362..3c0bc2a1 100644 --- a/src/shared/formatters/timeformat.ts +++ b/src/shared/formatters/timeformat.ts @@ -1,7 +1,7 @@ import { parse, toDate } from 'date-fns'; import { format, utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz'; import humanizeDuration from 'humanize-duration'; -import { isUndefined } from './numberformat'; +import { isNotUndefined } from './numberformat'; /** Parse date from string, returns date in UTC! */ export function dateFromString(datestring: string, format: string): Date { @@ -61,7 +61,7 @@ export function timestampToDateString(ts: number | Date): string { * @param datestring Input string (in the format yyyy-MM-dd) */ export function dateStringToTimeRange(datestring: string): string { - return isUndefined(datestring) ? '' : datestring.replace(/-/g, ''); + return isNotUndefined(datestring) ? datestring.replace(/-/g, '') : ''; } export function timestampHour(ts: number): number {