/** * Reduces the Data Structure to the account names. * @param {any[]} data * @returns */ function getAccounts(data) { const resultData = []; for (let i = 0; i < data.length; i++) { resultData.push(data[i].name); } return resultData; } /** * Sums up every entry and returns it. * @param {any[]} entries * @returns */ function getSummation(entries) { return entries.reduce((sum, e) => sum + parseFloat(e.amount), 0); } /** * Turns the given String in dd-MM-yyyy into a Javascript Date Object * @param {string} s * @returns */ function toDateByComponents(s) { const parts = s.split("-"); if (parts.length !== 3) return null; const day = parseInt(parts[0]); const month = parseInt(parts[1]) - 1; const year = parseInt(parts[2]); if (isNaN(day) || isNaN(month) || isNaN(year)) return null; return new Date(year, month, day).getTime(); } /** * formats the given date into a dd-MM-yyyy String * @param {Date} d * @returns */ function toStringDate(d) { console.log(d); return ("" + d.getDate()).padStart(2, "0") + "-" + ("" + (d.getMonth() + 1)).padStart(2, "0") + "-" + d.getFullYear(); } /** * Maps each entry to a line in a ListView while adhering to the visible amount of char columns * @param {any[]} entries * @param {number} visibleCols * @returns */ function toListViewEntries(entries, visibleCols) { let cols = visibleCols; return entries.map(e => { const amount = parseFloat(e.amount); const subject = ("" + e.subject).substr(0, cols - 20).padEnd(cols - 20, ' '); const sum = ((amount >= 0 ? "+" : "") + amount.toFixed(2)).substr(0, 10).padStart(10, ' '); const d = new Date(e.date); const date = d.getDate().toString().padStart(2, '0') + "-" + (d.getMonth() + 1).toString().padStart(2, '0') + "-" + d.getFullYear(); let s = (date.substr(0, 10)) + " " + subject + " " + sum; return s; }); } exports.getAccounts = getAccounts; exports.getSummation = getSummation; exports.toDateByComponents = toDateByComponents; exports.toListViewEntries = toListViewEntries; exports.toStringDate = toStringDate;