Skip to content

Commit

Permalink
catch range errors when dates are invalid and instead return boolean …
Browse files Browse the repository at this point in the history
…indicating validity (Hacker0x01#1577)
  • Loading branch information
jkeam authored and martijnrusschen committed Jan 31, 2019
1 parent 59c1b93 commit f9b5957
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/date_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,13 @@ export function isSameDay(date1, date2) {
}

export function isDayInRange(day, startDate, endDate) {
return isWithinInterval(day, { start: startDate, end: endDate });
let valid;
try {
valid = isWithinInterval(day, { start: startDate, end: endDate });
} catch (err) {
valid = false;
}
return valid;
}

// *** Diffing ***
Expand Down Expand Up @@ -326,7 +332,14 @@ export function isTimeInDisabledRange(time, { minTime, maxTime }) {
setMinutes(base, getMinutes(maxTime)),
getHours(maxTime)
);
return !isWithinInterval(baseTime, { start: min, end: max });

let valid;
try {
valid = !isWithinInterval(baseTime, { start: min, end: max });
} catch (err) {
valid = false;
}
return valid;
}

export function monthDisabledBefore(day, { minDate, includeDates } = {}) {
Expand Down
55 changes: 54 additions & 1 deletion test/date_utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ import {
monthDisabledBefore,
monthDisabledAfter,
getEffectiveMinDate,
getEffectiveMaxDate
getEffectiveMaxDate,
isTimeInDisabledRange,
isDayInRange
} from "../src/date_utils";
import setMinutes from "date-fns/setMinutes";
import setHours from "date-fns/setHours";

describe("date_utils", function() {
describe("isSameDay", function() {
Expand Down Expand Up @@ -245,4 +249,53 @@ describe("date_utils", function() {
assert(isEqual(getEffectiveMaxDate({ maxDate, includeDates }), date1));
});
});

describe("isTimeInDisabledRange", () => {
it("should tell if time is in disabled range", () => {
const date = newDate("2016-03-15");
const time = setHours(setMinutes(date, 30), 1);
const minTime = setHours(setMinutes(date, 30), 0);
const maxTime = setHours(setMinutes(date, 30), 5);
expect(isTimeInDisabledRange(time, { minTime, maxTime })).to.be.false;
});

it("should tell if time is not in disabled range", () => {
const date = newDate("2016-03-15");
const time = setHours(setMinutes(date, 30), 0);
const minTime = setHours(setMinutes(date, 30), 1);
const maxTime = setHours(setMinutes(date, 30), 5);
expect(isTimeInDisabledRange(time, { minTime, maxTime })).to.be.true;
});

it("should not throw an exception if max time is before min time", () => {
const date = newDate("2016-03-15");
const time = setHours(setMinutes(date, 30), 10);
const minTime = setHours(setMinutes(date, 30), 5);
const maxTime = setHours(setMinutes(date, 30), 0);
expect(isTimeInDisabledRange(time, { minTime, maxTime })).to.be.false;
});
});

describe("isDayInRange", () => {
it("should tell if day is in range", () => {
const day = newDate("2016-02-15");
const startDate = newDate("2016-02-01");
const endDate = newDate("2016-03-15");
expect(isDayInRange(day, startDate, endDate)).to.be.true;
});

it("should tell if day is not in range", () => {
const day = newDate("2016-07-15");
const startDate = newDate("2016-02-15");
const endDate = newDate("2016-03-15");
expect(isDayInRange(day, startDate, endDate)).to.be.false;
});

it("should not throw exception if end date is before start date", () => {
const day = newDate("2016-02-01");
const startDate = newDate("2016-02-15");
const endDate = newDate("2016-01-15");
expect(isDayInRange(day, startDate, endDate)).to.be.false;
});
});
});

0 comments on commit f9b5957

Please sign in to comment.