feat: add helper utility functions
- Add formatRupiah for currency formatting - Add formatDate for date formatting - Add calculateBalance to compute totals - Add generateId for unique IDs - Add getCurrentDate for timestamp
This commit is contained in:
parent
5de3a82e56
commit
d1b344df42
43
utils/helpers.ts
Normal file
43
utils/helpers.ts
Normal file
@ -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();
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue
Block a user