43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
|
|
import { BalanceInfo, Transaction } from '../types';
|
||
|
|
|
||
|
|
export const formatRupiah = (amount: number): string => {
|
||
|
|
return new Intl.NumberFormat('id-ID', {
|
||
|
|
style: 'currency',
|
||
|
|
currency: 'IDR',
|
||
|
|
minimumFractionDigits: 0,
|
||
|
|
maximumFractionDigits: 0,
|
||
|
|
}).format(amount);
|
||
|
|
};
|
||
|
|
|
||
|
|
export const formatDate = (dateString: string): string => {
|
||
|
|
const date = new Date(dateString);
|
||
|
|
return new Intl.DateTimeFormat('id-ID', {
|
||
|
|
day: 'numeric',
|
||
|
|
month: 'short',
|
||
|
|
year: 'numeric',
|
||
|
|
}).format(date);
|
||
|
|
};
|
||
|
|
|
||
|
|
export const calculateBalance = (transactions: Transaction[]): BalanceInfo => {
|
||
|
|
const income = transactions
|
||
|
|
.filter((t) => t.type === 'income')
|
||
|
|
.reduce((sum, t) => sum + t.amount, 0);
|
||
|
|
|
||
|
|
const expense = transactions
|
||
|
|
.filter((t) => t.type === 'expense')
|
||
|
|
.reduce((sum, t) => sum + t.amount, 0);
|
||
|
|
|
||
|
|
return {
|
||
|
|
total: income - expense,
|
||
|
|
income,
|
||
|
|
expense,
|
||
|
|
};
|
||
|
|
};
|
||
|
|
|
||
|
|
export const generateId = (): any => {
|
||
|
|
return
|
||
|
|
};
|
||
|
|
|
||
|
|
export const getCurrentDate = (): string => {
|
||
|
|
return new Date().toISOString();
|
||
|
|
};
|