Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/new feature #3

Merged
merged 3 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 150 additions & 1 deletion __test__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
const {DateRangeCheckerError, isEndDateInRange, isInRange, isStartDateAndEndDateIncludeRange, isStartDateAndEndDateInRange, isStartDateInRange} = require("../src")
const {
DateRangeCheckerError,
isEndDateInRange,
isInRange,
isStartDateAndEndDateIncludeRange,
isStartDateAndEndDateInRange,
isStartDateInRange,
findOverlappingDates,
findNonOverlappingDates
} = require("../src")

describe('checkDateRangeFormat', () => {
test('Throws error if dateRange is undefined', () => {
Expand Down Expand Up @@ -132,3 +141,143 @@ describe('isStartDateAndEndDateIncludeRange', () => {
expect(result).toBe(false)
})
})

describe('findOverlappingDates', () => {
test('returns empty array when date ranges do not overlap', () => {
const result = findOverlappingDates(
{ startDate: new Date('2022-01-01'), endDate: new Date('2023-12-31') },
{ startDate: new Date('2024-01-01'), endDate: new Date('2024-11-01') }
)
expect(result).toEqual([]);
})

test('If there are overlapping dates, those dates are returned.', () => {
const referenceDateRange = { startDate: new Date('2022-01-01'), endDate: new Date('2022-01-15') };
const comparisonDateRange = { startDate: new Date('2022-01-05'), endDate: new Date('2022-01-20') };

const result = findOverlappingDates(referenceDateRange, comparisonDateRange);

expect(result).toEqual([
new Date('2022-01-05'),
new Date('2022-01-06'),
new Date('2022-01-07'),
new Date('2022-01-08'),
new Date('2022-01-09'),
new Date('2022-01-10'),
new Date('2022-01-11'),
new Date('2022-01-12'),
new Date('2022-01-13'),
new Date('2022-01-14'),
new Date('2022-01-15'),
]);
});

test('returns overlapping dates when one range is completely within the other', () => {
const referenceDateRange = { startDate: new Date('2022-01-05'), endDate: new Date('2022-01-10') };
const comparisonDateRange = { startDate: new Date('2022-01-01'), endDate: new Date('2022-01-15') };

const result = findOverlappingDates(referenceDateRange, comparisonDateRange);

expect(result).toEqual([
new Date('2022-01-05'),
new Date('2022-01-06'),
new Date('2022-01-07'),
new Date('2022-01-08'),
new Date('2022-01-09'),
new Date('2022-01-10')
]);
});

test('returns overlapping dates when date ranges are identical', () => {
const referenceDateRange = { startDate: new Date('2022-01-05'), endDate: new Date('2022-01-15') };
const comparisonDateRange = { startDate: new Date('2022-01-05'), endDate: new Date('2022-01-15') };

const result = findOverlappingDates(referenceDateRange, comparisonDateRange);

expect(result).toEqual([
new Date('2022-01-05'),
new Date('2022-01-06'),
new Date('2022-01-07'),
new Date('2022-01-08'),
new Date('2022-01-09'),
new Date('2022-01-10'),
new Date('2022-01-11'),
new Date('2022-01-12'),
new Date('2022-01-13'),
new Date('2022-01-14'),
new Date('2022-01-15')
]);
});
})

describe('findNonOverlappingDates', () => {
test('returns merging two date ranges when there are no overlapping dates', () => {
const referenceDateRange = { startDate: new Date('2022-01-01'), endDate: new Date('2022-01-10') };
const comparisonDateRange = { startDate: new Date('2022-01-11'), endDate: new Date('2022-01-15') };

const result = findNonOverlappingDates(referenceDateRange, comparisonDateRange);

expect(result).toEqual([
new Date('2022-01-01'),
new Date('2022-01-02'),
new Date('2022-01-03'),
new Date('2022-01-04'),
new Date('2022-01-05'),
new Date('2022-01-06'),
new Date('2022-01-07'),
new Date('2022-01-08'),
new Date('2022-01-09'),
new Date('2022-01-10'),
new Date('2022-01-11'),
new Date('2022-01-12'),
new Date('2022-01-13'),
new Date('2022-01-14'),
new Date('2022-01-15')
]);
})

test('returns empty array when both date ranges are same', () => {
const referenceDateRange = { startDate: new Date('2022-01-01'), endDate: new Date('2022-01-10') };
const comparisonDateRange = { startDate: new Date('2022-01-01'), endDate: new Date('2022-01-10') };

const result = findNonOverlappingDates(referenceDateRange, comparisonDateRange);

expect(result).toEqual([]);
})

test('returns nonOverlapping dates', () => {
const referenceDateRange = { startDate: new Date('2022-01-01'), endDate: new Date('2022-01-10') };
const comparisonDateRange = { startDate: new Date('2022-01-05'), endDate: new Date('2022-01-15') };

const result = findNonOverlappingDates(referenceDateRange, comparisonDateRange);

expect(result).toEqual([
new Date('2022-01-01'),
new Date('2022-01-02'),
new Date('2022-01-03'),
new Date('2022-01-04'),
new Date('2022-01-11'),
new Date('2022-01-12'),
new Date('2022-01-13'),
new Date('2022-01-14'),
new Date('2022-01-15')
]);

const referenceDateRangeReverse = { startDate: new Date('2022-01-05'), endDate: new Date('2022-01-15') };
const comparisonDateRangeReverse = { startDate: new Date('2022-01-01'), endDate: new Date('2022-01-10') };

const resultReverse = findNonOverlappingDates(referenceDateRangeReverse, comparisonDateRangeReverse);

expect(resultReverse).toEqual([
new Date('2022-01-01'),
new Date('2022-01-02'),
new Date('2022-01-03'),
new Date('2022-01-04'),
new Date('2022-01-11'),
new Date('2022-01-12'),
new Date('2022-01-13'),
new Date('2022-01-14'),
new Date('2022-01-15')
]);
})
})
48 changes: 33 additions & 15 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "date-range-checker",
"version": "1.0.4",
"version": "1.1.0",
"description": "A utility library for comparing date ranges",
"main": "dist/cjs/index.js",
"module": "dist/mjs/index.js",
Expand Down Expand Up @@ -31,9 +31,10 @@
"@types/node": "^20.11.25",
"jest": "^29.7.0",
"ts-jest": "^29.1.2",
"typescript": "^5.4.2"
"typescript": "^5.4.2",
"ts-node": "^10.9.2"
},
"dependencies": {
"ts-node": "^10.9.2"

}
}
Loading