mirror of
https://github.com/freqtrade/frequi.git
synced 2024-11-10 10:21:55 +00:00
Split formatters into time and number formatters
This commit is contained in:
parent
ca361a26dc
commit
3d7876957b
2
src/shared/formatters/index.ts
Normal file
2
src/shared/formatters/index.ts
Normal file
|
@ -0,0 +1,2 @@
|
|||
export * from './numberformat';
|
||||
export * from './timeformat';
|
34
src/shared/formatters/numberformat.ts
Normal file
34
src/shared/formatters/numberformat.ts
Normal 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,
|
||||
};
|
|
@ -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,
|
Loading…
Reference in New Issue
Block a user