|
| 1 | +class LedgerEntry { |
| 2 | + constructor(date, description, change) { |
| 3 | + this.date = new Date(date); |
| 4 | + this.description = description; |
| 5 | + this.change = change; |
| 6 | + } |
| 7 | +} |
| 8 | + |
| 9 | +class FormattedLedgerEntry { |
| 10 | + constructor(entry, locale, dateFormat, currencyFormat) { |
| 11 | + this.entry = entry; |
| 12 | + this.locale = locale; |
| 13 | + this.dateFormat = dateFormat; |
| 14 | + this.currencyFormat = currencyFormat; |
| 15 | + } |
| 16 | + |
| 17 | + date() { |
| 18 | + return this.entry.date.toLocaleDateString(this.locale, this.dateFormat); |
| 19 | + } |
| 20 | + |
| 21 | + description(length = 25) { |
| 22 | + if (this.entry.description.length > length) { |
| 23 | + return `${this.entry.description.substring(0, length - 3)}...`; |
| 24 | + } |
| 25 | + |
| 26 | + return this.entry.description.padEnd(length, ' '); |
| 27 | + } |
| 28 | + |
| 29 | + change(offset = 13) { |
| 30 | + const formatted = (this.entry.change / 100).toLocaleString( |
| 31 | + this.locale, |
| 32 | + this.currencyFormat, |
| 33 | + ); |
| 34 | + |
| 35 | + const trailingSpace = formatted.includes(')') ? '' : ' '; |
| 36 | + return `${formatted}${trailingSpace}`.padStart(offset, ' '); |
| 37 | + } |
| 38 | + |
| 39 | + toTableRow() { |
| 40 | + return [this.date(), this.description(), this.change()].join(' | '); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +const OPTIONS = { |
| 45 | + HEADERS: { |
| 46 | + 'en-US': ['Date', 'Description', 'Change'], |
| 47 | + 'nl-NL': ['Datum', 'Omschrijving', 'Verandering'], |
| 48 | + }, |
| 49 | + headerRow: function (locale) { |
| 50 | + const [date, description, change] = this.HEADERS[locale]; |
| 51 | + return [ |
| 52 | + date.padEnd(10, ' '), |
| 53 | + description.padEnd(25, ' '), |
| 54 | + change.padEnd(13, ' '), |
| 55 | + ].join(' | '); |
| 56 | + }, |
| 57 | + dateFormatOptions: function () { |
| 58 | + return { |
| 59 | + day: '2-digit', |
| 60 | + month: '2-digit', |
| 61 | + year: 'numeric', |
| 62 | + }; |
| 63 | + }, |
| 64 | + currencyFormatOptions: function (currency, locale) { |
| 65 | + return { |
| 66 | + style: 'currency', |
| 67 | + currency: currency, |
| 68 | + currencySign: locale === 'en-US' ? 'accounting' : 'standard', |
| 69 | + currencyDisplay: locale === 'en-US' ? 'symbol' : 'narrowSymbol', |
| 70 | + }; |
| 71 | + }, |
| 72 | +}; |
| 73 | + |
| 74 | +export const createEntry = (date, description, change) => |
| 75 | + new LedgerEntry(date, description, change); |
| 76 | + |
| 77 | +export function formatEntries(currency, locale, entries) { |
| 78 | + let dateFormat = OPTIONS.dateFormatOptions(); |
| 79 | + let currencyFormat = OPTIONS.currencyFormatOptions(currency, locale); |
| 80 | + |
| 81 | + let rows = entries |
| 82 | + .sort( |
| 83 | + (a, b) => |
| 84 | + a.date - b.date || |
| 85 | + a.change - b.change || |
| 86 | + a.description.localeCompare(b.description), |
| 87 | + ) |
| 88 | + .map((entry) => { |
| 89 | + let formattedEntry = new FormattedLedgerEntry( |
| 90 | + entry, |
| 91 | + locale, |
| 92 | + dateFormat, |
| 93 | + currencyFormat, |
| 94 | + ); |
| 95 | + return formattedEntry.toTableRow(); |
| 96 | + }); |
| 97 | + |
| 98 | + return [OPTIONS.headerRow(locale), ...rows].join('\n'); |
| 99 | +} |
0 commit comments