When creating a function to be reusable throughout the project I came across something strange. After looking at this function it looks very much like it could be refactored.
The function should only return the short month and day, in that order. If it receives a parameter, it will be a Firestore Timestamp with the following format:
{
"seconds":1667420699,
"nanoseconds":394000000
}
If it does not receive a parameter, it must return today's month and day. So I did this:
function shortMonthDay(timestamp = undefined) {
let date
if (timestamp) {
date = new Date(timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000)
} else {
date = new Date()
}
return date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric'
}) // Nov 2
}
The function works as it should, it doesn't return any errors. But I wonder if it could be improved and how.