Skip to content

Commit

Permalink
Merge pull request #3 from tu2mf-org/feature/new_feature
Browse files Browse the repository at this point in the history
Feature/new feature
  • Loading branch information
hkc321 authored Apr 20, 2024
2 parents d741e07 + ca18edc commit 931d798
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 23 deletions.
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

0 comments on commit 931d798

Please sign in to comment.