Created
March 14, 2017 12:40
-
-
Save msikma/9e3dbe367dbfa24dc8681e2794a619b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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