mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-17 05:10:31 +00:00
68 lines
1.4 KiB
TypeScript
68 lines
1.4 KiB
TypeScript
import { get } from 'lodash';
|
|
import * as accounting from 'accounting';
|
|
import * as Currencies from 'js-money/lib/currency';
|
|
|
|
const getNegativeFormat = (formatName) => {
|
|
switch (formatName) {
|
|
case 'parentheses':
|
|
return '(%s%v)';
|
|
case 'mines':
|
|
return '-%s%v';
|
|
}
|
|
};
|
|
|
|
const getCurrencySign = (currencyCode) => {
|
|
return get(Currencies, `${currencyCode}.symbol`);
|
|
};
|
|
|
|
export interface IFormatNumberSettings {
|
|
precision?: number;
|
|
divideOn1000?: boolean;
|
|
excerptZero?: boolean;
|
|
negativeFormat?: string;
|
|
thousand?: string;
|
|
decimal?: string;
|
|
zeroSign?: string;
|
|
money?: boolean;
|
|
currencyCode?: string;
|
|
symbol?: string;
|
|
}
|
|
|
|
export const formatNumber = (
|
|
balance,
|
|
{
|
|
precision = 2,
|
|
divideOn1000 = false,
|
|
excerptZero = false,
|
|
negativeFormat = 'mines',
|
|
thousand = ',',
|
|
decimal = '.',
|
|
zeroSign = '',
|
|
money = true,
|
|
currencyCode,
|
|
symbol = '',
|
|
}: IFormatNumberSettings,
|
|
) => {
|
|
const formattedSymbol = getCurrencySign(currencyCode);
|
|
const negForamt = getNegativeFormat(negativeFormat);
|
|
const format = '%s%v';
|
|
|
|
let formattedBalance = parseFloat(balance);
|
|
|
|
if (divideOn1000) {
|
|
formattedBalance /= 1000;
|
|
}
|
|
return accounting.formatMoney(
|
|
formattedBalance,
|
|
money ? formattedSymbol : symbol ? symbol : '',
|
|
precision,
|
|
thousand,
|
|
decimal,
|
|
{
|
|
pos: format,
|
|
neg: negForamt,
|
|
zero: excerptZero ? zeroSign : format,
|
|
},
|
|
);
|
|
};
|