I am writing module that other people can use in their code. It's a calendar. I want to allow users to set locale parameter so that they have month names and other data in language of their choice.
I could create Calendar class that accepts locale, startOfWeekDay as arguments.
Methods of that class will those properties so there is no need to pass them to each method.
export class Calendar {
static WEEKDAYS = {sunday: 0, ...};
constructor(year, month, options = {locale: 'en-US', startOfWeekDay: Calendar.WEEKDAYS.sunday}) {
this.locale = options.locale;
this.startOfWeekDay = options.startOfWeekDay;
this.calendar = this.getCalendar(year, month);
}
/**
* returns True or False whether given day is first day of week
*/
isFirstDayOfWeek(day) {
// depends on this.startOfWeekDay
}
/**
* returns True or False whether given day is last day of week
*/
isLastDayOfWeek(day) {
// depends on this.startOfWeekDay
}
/**
* returns list of objects representing a calendar
* [ {day: 1, month: 1, year: 2019, weekDayName: 'Sunday' }, ... ]
*/
getCalendar(year, month) {
// depends on this.locale to return correct weekDayName
}
/**
* returns list of week day names: ['Sunday', 'Monday', ... ];
*/
getWeekDays() {
// depends on this.locale & this.startOfWeekDay
}
}
If I would want to rewrite it using more functional approach, what first comes to mind is to ask user to pass locale to each function that depends on it:
const WEEKDAYS = {sunday: 0, ...};
export function isFirstDayOfWeek(startOfWeekDay, day) {}
export function isLastDayOfWeek(startOfWeekDay, day) {}
export function getCalendar(year, month, options = {locale: 'en-US'}) {}
export function getWeekDays(options = {locale: 'en-US', startOfWeekDay: WEEKDAYS.sunday}) {}
What functional programming concepts would you use?