diff --git a/utils/helpers.ts b/utils/helpers.ts new file mode 100644 index 0000000..9937d01 --- /dev/null +++ b/utils/helpers.ts @@ -0,0 +1,43 @@ +import { Transaction, BalanceInfo } 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 = (): string => { + return Date.now().toString(36) + Math.random().toString(36).substr(2); +}; + +export const getCurrentDate = (): string => { + return new Date().toISOString(); +}; \ No newline at end of file