Source: app/util/daysOfWeekArray.js

import moment from 'moment';

/*
 * Array of string-formated weekdays
 * 0: 'Sun'
 * 1: 'Mon'
 * 2: 'Tue'
 * 3: 'Wed'
 * 4: 'Thu'
 * 5: 'Fri'
 * 6: 'Sat'
 *
 * Deprecated function: Use isoDaysOfWeek instead.
 */
const daysOfWeek = [...new Array(7)].map((_, dayNumber) => {
  // eslint-disable-next-line no-console
  console.warn('This function is deprecated in favour of isoDaysOfWeek');
  return moment()
    .weekday(dayNumber)
    .format('ddd');
});

export default daysOfWeek;

/**
 * Array with an Object string-formated isoweekdays and numeric values
 *
 * @weekStart: To initialise a week start day other than Monday, provide the corresponding day for the argument weekStart
 * @format: The format of the days being returned. Default is full day e.g. Monday. Use 'ddd' for short name e.g. Mon.
 * @returns {
 *  name: string,
 *  value: number,
 * }
 *
 * @example
 * isoDaysOfWeek(1) = [
 * {value: 1, title: 'Monday'},
 * {value: 2, title: 'Tuesday'},
 * {value: 3, title: 'Wednesday'},
 * {value: 4, title: 'Thursday'},
 * {value: 5, title: 'Friday'},
 * {value: 6, title: 'Saturday'},
 * {value: 0, title: 'Sunday'},
 * ];
 */

export const isoDaysOfWeek = (weekStart = 1, format = 'dddd') => {
  const isoWeek = [...new Array(7)].map((_, dayNumber) => ({
    title: moment()
      .isoWeekday(dayNumber)
      .format(format),
    value: dayNumber,
  }));

  const shiftedItems = [...new Array(weekStart)].map(() => isoWeek.splice(0, 1)[0]);

  return [].concat(isoWeek, shiftedItems);
};