Split formatters into time and number formatters

This commit is contained in:
Matthias 2022-04-09 10:24:38 +02:00
parent ca361a26dc
commit 3d7876957b
3 changed files with 36 additions and 31 deletions

View File

@ -0,0 +1,2 @@
export * from './numberformat';
export * from './timeformat';

View File

@ -0,0 +1,34 @@
export function isUndefined(val): boolean {
return val === undefined || val === null;
}
export function formatPercent(value: number, decimals = 3): string {
return !isUndefined(value) ? `${(value * 100).toFixed(decimals)}%` : '';
}
/**
* Format number to `decimals` without trailing zeros
* @param value Number to format
* @param decimals number of decimals (Defaults to 8)
* @returns Formatted string
*/
export function formatPrice(value: number, decimals = 8): string {
return !isUndefined(value) ? parseFloat(value.toFixed(decimals)).toString() : '';
}
/**
* Formats price in the format "<price> <StakeCurrency>" using "deciaml" decimals
* @param price Price to format
* @param currency currency to use
* @param decimals Decimals
* @returns
*/
export function formatPriceCurrency(price, currency: string, decimals = 3) {
return `${formatPrice(price, decimals)} ${currency}`;
}
export default {
isUndefined,
formatPrice,
formatPercent,
};

View File

@ -2,35 +2,6 @@ import { parse, toDate, getHours } from 'date-fns';
import { format, utcToZonedTime } from 'date-fns-tz';
import humanizeDuration from 'humanize-duration';
export function isUndefined(val): boolean {
return val === undefined || val === null;
}
export function formatPercent(value: number, decimals = 3): string {
return !isUndefined(value) ? `${(value * 100).toFixed(decimals)}%` : '';
}
/**
* Format number to `decimals` without trailing zeros
* @param value Number to format
* @param decimals number of decimals (Defaults to 8)
* @returns Formatted string
*/
export function formatPrice(value: number, decimals = 8): string {
return !isUndefined(value) ? parseFloat(value.toFixed(decimals)).toString() : '';
}
/**
* Formats price in the format "<price> <StakeCurrency>" using "deciaml" decimals
* @param price Price to format
* @param currency currency to use
* @param decimals Decimals
* @returns
*/
export function formatPriceCurrency(price, currency: string, decimals = 3) {
return `${formatPrice(price, decimals)} ${currency}`;
}
export function dateFromString(datestring: string, format: string): Date {
return parse(datestring, format, 0);
}
@ -113,8 +84,6 @@ export function humanizeDurationFromSeconds(duration: number): string {
}
export default {
formatPrice,
formatPercent,
timestampms,
timestampmsWithTimezone,
timestampToDateString,