Skip to content

Commit

Permalink
fix(datetime)!: get day of week key without using locale
Browse files Browse the repository at this point in the history
note that this removes the "locale" parameter from the param signature of several methods:
* helpers.datetime.getDayOfWeekKey
* helpers.location.getNextOpenIntervalAfterToday
* helpers.location.getOpenIntervalsForToday
  • Loading branch information
bashunaimiroy committed Nov 28, 2024
1 parent 904fd8e commit d77f075
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/helpers/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ export const localizeDate = (dateObj: Date, locale: string, format: localizeDate

/**
* Gets the appropriate 3 letter key for the day of the week for a given date object
* used to find the correct key of the hour data to pull data off of
* used to find the correct key of the hour data to pull data off of.
* We localize using the `en-US` locale, because the date keys are in that locale.
*
* @param {Date} dateObj
* @param {locale} string - Hyphenized language/country locale combo, e.g. en-US (BCP 47).
* @param {String} timezone
* @return {DayOfWeek}
*/
export const getDayOfWeekKey = (dateObj: Date, locale: string, timezone: string): DayOfWeek => {
const dayOfWeek = localizeDate(dateObj, locale, DateFormats.weekdayShort, timezone);
export const getDayOfWeekKey = (dateObj: Date, timezone: string): DayOfWeek => {
const dayOfWeek = localizeDate(dateObj, 'en-US', DateFormats.weekdayShort, timezone);
return dayOfWeek.toUpperCase() as DayOfWeek;
};

Expand Down
13 changes: 3 additions & 10 deletions src/helpers/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class Location {
};
}

const nextOpenIntervalAfterToday = this.getNextOpenIntervalAfterToday(locale, timezone, hours);
const nextOpenIntervalAfterToday = this.getNextOpenIntervalAfterToday(timezone, hours);
if (nextOpenIntervalAfterToday) {
const { date, interval: nextInterval } = nextOpenIntervalAfterToday;
const opensAt = getFormattedTime(date, nextInterval.open, DateFormats.hourNminuteN, locale, timezone, tzOffsetString);
Expand Down Expand Up @@ -173,7 +173,6 @@ export class Location {
nextDateObj.setDate(nextDateObj.getDate() + 1);
const dayOfWeekKey = getDayOfWeekKey(
nextDateObj,
locale,
timezone,
);

Expand Down Expand Up @@ -217,19 +216,16 @@ export class Location {
/**
* Gets open intervals for today
*
* @param {string} locale - Hyphenized language/country locale combo, e.g. en-US (BCP 47).
* @param {string} timezone
* @param {Hours} hours
* @return {OpenInterval[]}
*/
getOpenIntervalsForToday(
locale: string,
timezone: string,
hours: Hours
): OpenInterval[] {
const dayOfWeekKey: DayOfWeek = getDayOfWeekKey(
new Date(),
locale,
timezone
);
return hours[dayOfWeekKey];
Expand All @@ -248,7 +244,7 @@ export class Location {
timezone: string,
hours: Hours
): OpenInterval | null {
const todayOpenIntervals = this.getOpenIntervalsForToday(locale, timezone, hours);
const todayOpenIntervals = this.getOpenIntervalsForToday(timezone, hours);
if (!todayOpenIntervals.length) {
return null;
}
Expand Down Expand Up @@ -282,7 +278,7 @@ export class Location {
timezone: string,
hours: Hours
): OpenInterval | null {
const todayOpenIntervals = this.getOpenIntervalsForToday(locale, timezone, hours);
const todayOpenIntervals = this.getOpenIntervalsForToday(timezone, hours);
if (!todayOpenIntervals.length) {
return null;
}
Expand Down Expand Up @@ -311,13 +307,11 @@ export class Location {
/**
* Get the next opening period after today within the next seven days
*
* @param {string} locale - Hyphenized language/country locale combo, e.g. en-US (BCP 47).
* @param {string} timezone
* @param {Hours} hours
* @return {Object | null} { open: openTime, close: closeTime }
*/
getNextOpenIntervalAfterToday(
locale: string,
timezone: string,
hours: Hours,
): {
Expand All @@ -331,7 +325,6 @@ export class Location {
nextDateObj.setDate(nextDateObj.getDate() + 1);
const dayOfWeekKey = getDayOfWeekKey(
nextDateObj,
locale,
timezone,
);

Expand Down
1 change: 0 additions & 1 deletion test/helpers.location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,6 @@ describe('Check open intervals for today', () => {
vi.setSystemTime(date);

const result = sdk.helpers.location.getOpenIntervalsForToday(
'en-US',
'UTC',
location['PICKUP'].hours
);
Expand Down
22 changes: 10 additions & 12 deletions typedocs/classes/helpers_location.Location.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions typedocs/modules/helpers_datetime.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d77f075

Please sign in to comment.