mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-15 04:13:47 +00:00
refactor: unify logic
This commit is contained in:
committed by
Carlos Valente
parent
25c7915cf7
commit
f5936e5254
@@ -0,0 +1,60 @@
|
||||
import { checkIsNextDay } from './checkIsNextDay';
|
||||
import { MILLIS_PER_HOUR } from './conversionUtils';
|
||||
|
||||
describe('checkIsNextDay', () => {
|
||||
it('returns false if the previous event duration is 0', () => {
|
||||
const previousStart = 0;
|
||||
const previousDuration = 0;
|
||||
const timeStart = 0;
|
||||
expect(checkIsNextDay(previousStart, timeStart, previousDuration)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('returns false if event starts after one before', () => {
|
||||
const previousStart = 10;
|
||||
const previousDuration = 2;
|
||||
const timeStart = 11;
|
||||
expect(checkIsNextDay(previousStart, timeStart, previousDuration)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('returns true if event starts after one before', () => {
|
||||
const previousStart = 10;
|
||||
const previousDuration = 2;
|
||||
const timeStart = 9;
|
||||
expect(checkIsNextDay(previousStart, timeStart, previousDuration)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('returns true if event starts at the same time as one before', () => {
|
||||
const previousStart = 10;
|
||||
const previousDuration = 2;
|
||||
const timeStart = 10;
|
||||
expect(checkIsNextDay(previousStart, timeStart, previousDuration)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should account for an event that crossed midnight', () => {
|
||||
const previousStart = 20 * MILLIS_PER_HOUR;
|
||||
const previousDuration = 6 * MILLIS_PER_HOUR; // event finished at 02:00:00
|
||||
const timeStart = 1 * MILLIS_PER_HOUR;
|
||||
expect(checkIsNextDay(previousStart, timeStart, previousDuration)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should account for an event that crossed midnight and there is a gap', () => {
|
||||
const previousStart = 23 * MILLIS_PER_HOUR;
|
||||
const timeStart = 2 * MILLIS_PER_HOUR;
|
||||
const previousDuration = 2 * MILLIS_PER_HOUR;
|
||||
expect(checkIsNextDay(previousStart, timeStart, previousDuration)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should account for an event that crossed midnight with no overlaps', () => {
|
||||
const previousStart = 20 * MILLIS_PER_HOUR;
|
||||
const previousDuration = 6 * MILLIS_PER_HOUR; // event finished at 02:00:00
|
||||
const timeStart = 19 * MILLIS_PER_HOUR;
|
||||
expect(checkIsNextDay(previousStart, timeStart, previousDuration)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should account for an event that finishes exactly at midnight', () => {
|
||||
const previousStart = 23 * MILLIS_PER_HOUR;
|
||||
const previousDuration = 1 * MILLIS_PER_HOUR;
|
||||
const timeStart = 2 * MILLIS_PER_HOUR;
|
||||
expect(checkIsNextDay(previousStart, timeStart, previousDuration)).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -1,13 +1,40 @@
|
||||
import { dayInMs } from './conversionUtils.js';
|
||||
|
||||
/**
|
||||
* Utility function checks whether a given event is the day after from its predecessor
|
||||
* We consider an event to be the day after, if it begins before the start of the previous
|
||||
* @example day after
|
||||
* 09:00 - 10:00
|
||||
* 08:00 - 10:30
|
||||
* @example day after
|
||||
* 23:00 - 00:00
|
||||
* 02:00 - 03:00
|
||||
* @example same day
|
||||
* 09:00 - 10:00
|
||||
* 09:30 - 10:30
|
||||
* @example same day, but previous crosses midnight
|
||||
* 23:00 - 01:00
|
||||
* 02:00 - 03:00
|
||||
* @example same day, but previous crosses midnight (with overlap)
|
||||
* 22:00 - 02:00
|
||||
* 01:00 - 03:00
|
||||
*/
|
||||
export function checkIsNextDay(previousStart: number, timeStart: number, previousDuration: number): boolean {
|
||||
return previousDuration === 0 ? false : timeStart <= previousStart;
|
||||
if (previousDuration === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (timeStart <= previousStart) {
|
||||
const normalisedPreviousEnd = previousStart + previousDuration;
|
||||
if (normalisedPreviousEnd === dayInMs) {
|
||||
return true;
|
||||
}
|
||||
// handle exception for an event that finishes exactly at midnight
|
||||
if (normalisedPreviousEnd > dayInMs) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -7,10 +7,9 @@ describe('getTimeFromPrevious', () => {
|
||||
const previousEnd = 71700000; // 19:55
|
||||
const previousDuration = 2100000; // 35 minutes
|
||||
const currentStart = 75600000; // 21:00
|
||||
const currentEnd = 81000000; // 22:30
|
||||
const expected = 75600000 - 71700000; // current start - previousEnd
|
||||
|
||||
expect(getTimeFromPrevious(currentStart, currentEnd, previousStart, previousEnd, previousDuration)).toBe(expected);
|
||||
expect(getTimeFromPrevious(currentStart, previousStart, previousEnd, previousDuration)).toBe(expected);
|
||||
});
|
||||
|
||||
it('accounts for partially overlapping events', () => {
|
||||
@@ -18,10 +17,9 @@ describe('getTimeFromPrevious', () => {
|
||||
const previousEnd = 12;
|
||||
const previousDuration = 2;
|
||||
const currentStart = 11;
|
||||
const currentEnd = 12;
|
||||
const expected = -(previousEnd - currentStart);
|
||||
|
||||
expect(getTimeFromPrevious(currentStart, currentEnd, previousStart, previousEnd, previousDuration)).toBe(expected);
|
||||
expect(getTimeFromPrevious(currentStart, previousStart, previousEnd, previousDuration)).toBe(expected);
|
||||
});
|
||||
|
||||
it('accounts for events that are fully contained', () => {
|
||||
@@ -29,10 +27,9 @@ describe('getTimeFromPrevious', () => {
|
||||
const previousEnd = 16;
|
||||
const previousDuration = 8;
|
||||
const currentStart = 10;
|
||||
const currentEnd = 15;
|
||||
const expected = -(previousEnd - currentStart);
|
||||
|
||||
expect(getTimeFromPrevious(currentStart, currentEnd, previousStart, previousEnd, previousDuration)).toBe(expected);
|
||||
expect(getTimeFromPrevious(currentStart, previousStart, previousEnd, previousDuration)).toBe(expected);
|
||||
});
|
||||
|
||||
it('fully overlapping events are the next day', () => {
|
||||
@@ -40,9 +37,28 @@ describe('getTimeFromPrevious', () => {
|
||||
const previousEnd = 12 * MILLIS_PER_HOUR;
|
||||
const previousDuration = previousEnd - previousStart;
|
||||
const currentStart = 10 * MILLIS_PER_HOUR;
|
||||
const currentEnd = 12 * MILLIS_PER_HOUR;
|
||||
const expected = dayInMs - previousDuration;
|
||||
|
||||
expect(getTimeFromPrevious(currentStart, currentEnd, previousStart, previousEnd, previousDuration)).toBe(expected);
|
||||
expect(getTimeFromPrevious(currentStart, previousStart, previousEnd, previousDuration)).toBe(expected);
|
||||
});
|
||||
|
||||
it('accounts for events that are the day after', () => {
|
||||
const previousStart = 20 * MILLIS_PER_HOUR;
|
||||
const previousEnd = 23 * MILLIS_PER_HOUR;
|
||||
const previousDuration = 3 * MILLIS_PER_HOUR;
|
||||
const currentStart = 22 * MILLIS_PER_HOUR;
|
||||
const expected = -MILLIS_PER_HOUR; // (previousEnd - currentStart);
|
||||
|
||||
expect(getTimeFromPrevious(currentStart, previousStart, previousEnd, previousDuration)).toBe(expected);
|
||||
});
|
||||
|
||||
it('accounts for events that cross midnight', () => {
|
||||
const previousStart = 20 * MILLIS_PER_HOUR;
|
||||
const previousEnd = 2 * MILLIS_PER_HOUR;
|
||||
const previousDuration = 6 * MILLIS_PER_HOUR;
|
||||
const currentStart = 1 * MILLIS_PER_HOUR;
|
||||
const expected = -MILLIS_PER_HOUR; // (previousEnd - currentStart);
|
||||
|
||||
expect(getTimeFromPrevious(currentStart, previousStart, previousEnd, previousDuration)).toBe(expected);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,13 +7,12 @@ import { dayInMs } from './conversionUtils.js';
|
||||
*/
|
||||
export function getTimeFromPrevious(
|
||||
currentStart: number,
|
||||
currentEnd: number,
|
||||
previousStart?: number,
|
||||
previousEnd?: number,
|
||||
previousDuration?: number,
|
||||
): number {
|
||||
// there is no previous event
|
||||
if (previousStart == null || previousEnd == null || previousDuration == null) {
|
||||
if (previousStart === undefined || previousEnd === undefined || previousDuration === undefined) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -35,7 +34,6 @@ export function getTimeFromPrevious(
|
||||
}
|
||||
|
||||
// event overlaps with previous
|
||||
// TODO: account for midnight roll
|
||||
const overlap = previousEnd - currentStart;
|
||||
if (overlap > 0) {
|
||||
// time is a negative number indicating the amount of overlap
|
||||
|
||||
Reference in New Issue
Block a user