This sample of code takes data gathered by hitting the Invoice endpoint and calculates the date of the next invoice in it. It is used in two places: first, in the registration summary page, as shown below. It is also shown on the Update Card Information page on the payment user accounts.
const parseDateTime = (invoiceData) => {
let dateParseArray = [];
invoiceData.forEach(element => {
let date = new Date((Date.parse(element.InvoiceDate.substring(0, 10))));
dateParseArray.push(date);
});
let today = new Date();
dateParseArray = dateParseArray.sort((a, b) => {
let distanceA = Math.abs(today - a);
let distanceB = Math.abs(today - b);
return distanceA - distanceB;
}).filter((d) => {
return d - today > 0;
});
if (dateParseArray[0] === undefined) {
return "";
}
const nextScheduledMonth = dateParseArray[0].getUTCMonth() + 1;
const nextScheduledDay = dateParseArray[0].getUTCDate();
const nextScheduledYear = dateParseArray[0].getUTCFullYear();
const dateString = `${nextScheduledMonth}/${nextScheduledDay}/${nextScheduledYear}`;
return dateString;
};
export { parseDateTime };
Some of the salient features of this code chunk include the use of features in JavaScript EcmaScript 6 (ES6).
This includes the use of arrow functions, templated strings, and use of keywords
let
and
const
. This matches industry standard specifications of how to write JavaScript. Additionally, I configured ESLint,
a JavaScript static code analysis tool, to enforce these styles throughout our JavaScript files. This helps
keep our JavaScript styles consistent, a key software engineering principle.
The sample uses a sorting algorithm to find the nearest date to today in the future, so it demonstrates
understanding of last semester's Algorithms/Introduction to Software Engineering course. Additionally,
this code is encapsulated in its own utility class to minimize code duplication and maximize code reuse.