Last active
January 5, 2025 16:28
-
-
Save malko/b39f8814a78fb72c9013ad1a0b6f816e to your computer and use it in GitHub Desktop.
periodOverlap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { describe, expect, it } from 'vitest' | |
import { periodOverlap, type Period } from './date' | |
describe('periodOverlap', () => { | |
it('should return true if periods overlap', () => { | |
const period1: Period = { start: "2023-04-01", end: "2023-04-10" } | |
const period2: Period = { start: "2023-04-05", end: "2023-04-15" } | |
expect(periodOverlap(period1, period2)).toBe(true) | |
expect(periodOverlap(period2, period1)).toBe(true) | |
}) | |
it('should return true if one period starts and ends within another', () => { | |
const period1: Period = { start: "2023-04-01", end: "2023-04-10" } | |
const period2: Period = { start: "2023-04-05", end: "2023-04-07" } | |
expect(periodOverlap(period1, period2)).toBe(true) | |
expect(periodOverlap(period2, period1)).toBe(true) | |
}) | |
it('should return false if periods do not overlap', () => { | |
const period1: Period = { start: "2023-04-01", end: "2023-04-10" } | |
const period2: Period = { start: "2023-04-11", end: "2023-04-15" } | |
expect(periodOverlap(period1, period2)).toBe(false) | |
expect(periodOverlap(period2, period1)).toBe(false) | |
}) | |
it('should handle edge cases where periods touch (inclusive)', () => { | |
const period1: Period = { start: "2023-04-01", end: "2023-04-10" } | |
const period2: Period = { start: "2023-04-10", end: "2023-04-15" } | |
expect(periodOverlap(period1, period2)).toBe(true) | |
expect(periodOverlap(period2, period1)).toBe(true) | |
}) | |
it('should handle edge cases where periods touch (exclusive)', () => { | |
const period1: Period = { start: "2023-04-01", end: "2023-04-10" } | |
const period2: Period = { start: "2023-04-10", end: "2023-04-15" } | |
expect(periodOverlap(period1, period2, true)).toBe(false) | |
expect(periodOverlap(period2, period1, true)).toBe(false) | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface PeriodString { | |
start: string | |
end: string | |
} | |
export interface PeriodDate { | |
start: Date | |
end: Date | |
} | |
export type Period = PeriodString | PeriodDate | |
type PeriodOverlap = { | |
(period1: PeriodString, period2: PeriodString, exclusive?: boolean): boolean | |
(period1: PeriodDate, period2: PeriodDate, exclusive?: boolean): boolean | |
} | |
/** | |
* check if two periods overlap | |
* @param period1 - first period | |
* @param period2 - second period | |
* @param exclusive - if true, check for strict overlap (exclusive), otherwise inclusive (default) | |
* @returns true if periods overlap, false otherwise. | |
*/ | |
export const periodOverlap: PeriodOverlap = (period1: Period, period2: Period, exclusive: boolean = false) => { | |
if (exclusive) { | |
return period1.start < period2.end && period2.start < period1.end | |
} | |
return period1.start <= period2.end && period2.start <= period1.end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment