Skip to content

Commit

Permalink
feat: add timeUntilStatusChangeMs to OpenStatusDayAndTime interface (#21
Browse files Browse the repository at this point in the history
)
  • Loading branch information
balayayd authored Nov 6, 2024
1 parent 1db486e commit f49d54c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/helpers/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,27 @@ export class Location {
const closeTime = localizeDate(dateObj, locale, DateFormats.hourNminuteNsecondN, timezone, false);
const openUntil = getFormattedTime(dateObj, closeTime, DateFormats.hourNminuteN, locale, timezone, tzOffsetString);
const openUntilDay = getFormattedTime(dateObj, closeTime, DateFormats.weekdayLong, locale, timezone, tzOffsetString);
const timeUntilClose = dateObj.getTime() - dateNow.getTime();
return {
status: this.OpenStatus.CURRENTLY_OPEN,
time: openUntil,
day: openUntilDay,
timeUntilStatusChangeMs: timeUntilClose,
};
}

const nextOpenIntervalToday = this.getNextOpenIntervalToday(locale, timezone, hours);
if (nextOpenIntervalToday) {
const opensAt = getFormattedTime(dateNow, nextOpenIntervalToday.open, DateFormats.hourNminuteN, locale, timezone, tzOffsetString);
const opensAtDay = getFormattedTime(dateNow, nextOpenIntervalToday.open, DateFormats.weekdayLong, locale, timezone, tzOffsetString);
const opensAtDateString = localizeDate(dateNow, locale, DateFormats.yearNmonthNdayN, timezone);
const opensAtDateObj = getDateObjInTimezone(opensAtDateString, nextOpenIntervalToday.open, tzOffsetString);
const timeUntilOpen = opensAtDateObj.getTime() - dateNow.getTime();
return {
status: this.OpenStatus.OPENS_LATER_TODAY,
time: opensAt,
day: opensAtDay,
timeUntilStatusChangeMs: timeUntilOpen,
};
}

Expand All @@ -117,10 +123,14 @@ export class Location {
const { date, interval: nextInterval } = nextOpenIntervalAfterToday;
const opensAt = getFormattedTime(date, nextInterval.open, DateFormats.hourNminuteN, locale, timezone, tzOffsetString);
const opensAtDay = getFormattedTime(date, nextInterval.open, DateFormats.weekdayLong, locale, timezone, tzOffsetString);
const opensAtDateString = localizeDate(date, locale, DateFormats.yearNmonthNdayN, timezone);
const opensAtDateObj = getDateObjInTimezone(opensAtDateString, nextOpenIntervalAfterToday.interval.open, tzOffsetString);
const timeUntilOpen = opensAtDateObj.getTime() - dateNow.getTime();
return {
status: this.OpenStatus.OPENS_ANOTHER_DAY,
time: opensAt,
day: opensAtDay,
timeUntilStatusChangeMs: timeUntilOpen,
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/types/helpers/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export interface OpenStatusDayAndTime {
status: OpenStatus;
time: string;
day: string;
/**
* Milliseconds until the `status` changes to a different `OpenStatus` value
*/
timeUntilStatusChangeMs?: number;
}

export interface Address {
Expand Down
24 changes: 21 additions & 3 deletions test/helpers.location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ describe('Location fulfillment status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.CURRENTLY_OPEN,
time: '3:00 pm',
day: 'Monday',
timeUntilStatusChangeMs: 7200000,
});
});

Expand All @@ -453,6 +454,7 @@ describe('Location fulfillment status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.OPENS_LATER_TODAY,
time: '4:00 pm',
day: 'Monday',
timeUntilStatusChangeMs: 1800000,
});
});

Expand All @@ -467,6 +469,7 @@ describe('Location fulfillment status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.OPENS_ANOTHER_DAY,
time: '12:00 pm',
day: 'Tuesday',
timeUntilStatusChangeMs: 46800000,
});
});

Expand All @@ -481,6 +484,7 @@ describe('Location fulfillment status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.OPENS_LATER_TODAY,
time: '12:00 pm',
day: 'Monday',
timeUntilStatusChangeMs: 7200000,
});
});

Expand All @@ -502,7 +506,7 @@ describe('Location fulfillment status day and time variations', () => {
'close_formatted': '4:00 AM'
},
];
// Monday January 1, 2024 10:00:00 pm
// Monday January 1, 2024 8:00:00 pm
const date = new Date(2024, 0, 1, 20, 0);
vi.setSystemTime(date);

Expand All @@ -511,6 +515,7 @@ describe('Location fulfillment status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.CURRENTLY_OPEN,
time: '4:00 am',
day: 'Tuesday',
timeUntilStatusChangeMs: 28800000,
});
});

Expand All @@ -532,7 +537,7 @@ describe('Location fulfillment status day and time variations', () => {
'close_formatted': '12:30 AM'
},
];
// Monday January 1, 2024 10:00:00 am
// Monday January 1, 2024 8:00:00 pm
const date = new Date(2024, 0, 1, 20);
vi.setSystemTime(date);

Expand All @@ -541,6 +546,7 @@ describe('Location fulfillment status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.CURRENTLY_OPEN,
time: '12:30 am',
day: 'Tuesday',
timeUntilStatusChangeMs: 16200000,
});
});

Expand Down Expand Up @@ -572,6 +578,7 @@ describe('Location fulfillment status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.CURRENTLY_OPEN,
time: '12:30 am',
day: 'Tuesday',
timeUntilStatusChangeMs: 1800000,
});
});

Expand Down Expand Up @@ -603,6 +610,7 @@ describe('Location fulfillment status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.CURRENTLY_OPEN,
time: '12:30 am',
day: 'Tuesday',
timeUntilStatusChangeMs: 1800000,
});
});

Expand All @@ -625,6 +633,7 @@ describe('Location fulfillment status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.CURRENTLY_OPEN,
time: '12:59 am',
day: 'Monday',
timeUntilStatusChangeMs: 2940000,
});
});

Expand Down Expand Up @@ -769,6 +778,7 @@ describe('Location fulfillment status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.CURRENTLY_OPEN,
time: '12:00 am',
day: 'Saturday',
timeUntilStatusChangeMs: 396000000,
});
});

Expand All @@ -788,6 +798,7 @@ describe('Location fulfillment status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.CURRENTLY_OPEN,
time: '3:00 pm',
day: 'Monday',
timeUntilStatusChangeMs: 7200000,
});
});

Expand All @@ -807,6 +818,7 @@ describe('Location fulfillment status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.OPENS_LATER_TODAY,
time: '4:00 pm',
day: 'Monday',
timeUntilStatusChangeMs: 1800000,
});
});

Expand All @@ -826,6 +838,7 @@ describe('Location fulfillment status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.OPENS_ANOTHER_DAY,
time: '12:00 pm',
day: 'Tuesday',
timeUntilStatusChangeMs: 46800000,
});
});
});
Expand All @@ -852,6 +865,7 @@ describe('Location business hours status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.CURRENTLY_OPEN,
time: '3:00 pm',
day: 'Monday',
timeUntilStatusChangeMs: 7200000,
});
});

Expand All @@ -866,6 +880,7 @@ describe('Location business hours status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.OPENS_LATER_TODAY,
time: '4:00 pm',
day: 'Monday',
timeUntilStatusChangeMs: 1800000,
});
});

Expand All @@ -880,6 +895,7 @@ describe('Location business hours status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.OPENS_ANOTHER_DAY,
time: '12:00 pm',
day: 'Tuesday',
timeUntilStatusChangeMs: 46800000,
});
});

Expand All @@ -894,6 +910,7 @@ describe('Location business hours status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.OPENS_LATER_TODAY,
time: '12:00 pm',
day: 'Monday',
timeUntilStatusChangeMs: 7200000,
});
});

Expand All @@ -915,7 +932,7 @@ describe('Location business hours status day and time variations', () => {
'close_formatted': '4:00 AM'
},
];
// Monday January 1, 2024 10:00:00 pm
// Monday January 1, 2024 8:00:00 pm
const date = new Date(2024, 0, 1, 20, 0);
vi.setSystemTime(date);

Expand All @@ -924,6 +941,7 @@ describe('Location business hours status day and time variations', () => {
status: sdk.helpers.location.OpenStatus.CURRENTLY_OPEN,
time: '4:00 am',
day: 'Tuesday',
timeUntilStatusChangeMs: 28800000,
});
});

Expand Down

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

0 comments on commit f49d54c

Please sign in to comment.