Skip to content

Instantly share code, notes, and snippets.

@msikma
Created March 14, 2017 12:40
Show Gist options
  • Save msikma/9e3dbe367dbfa24dc8681e2794a619b6 to your computer and use it in GitHub Desktop.
Save msikma/9e3dbe367dbfa24dc8681e2794a619b6 to your computer and use it in GitHub Desktop.
/**
* Returns the days of the week from a given start day, indicated with a three-letter code.
* Normally, the start day is Monday, but any day is usable depending on locale.
*
* @param {String} startDay One of 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'
* @returns {Array} List of days in the week starting from the indicated first day
*/
export const weekdayList = (startDay = 'mon') => {
const days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
const start = days.indexOf(startDay)
let rest
// Ensure we have a valid starting day.
switch (start) {
case -1:
throw RangeError(`Invalid start day: ${startDay}. Must be one of ${days}.`)
case 0:
return days
default:
rest = days.splice(0, start)
return [...days, ...rest]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment