Skip to content

Commit

Permalink
Export DateUtils (fix #76)
Browse files Browse the repository at this point in the history
  • Loading branch information
gpbl committed Nov 3, 2015
1 parent b252da2 commit f555948
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 0 deletions.
3 changes: 3 additions & 0 deletions DateUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

module.exports = require("./lib/DateUtils");
71 changes: 71 additions & 0 deletions src/DateUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const DateUtils = {

clone(d) {
return new Date(d.getTime());
},

isDayOutsideMonth(d1, d2) {
return d1.getMonth() !== d2.getMonth();
},

isSameDay(d1, d2) {
return d1.getDate() === d2.getDate() &&
d1.getMonth() === d2.getMonth() &&
d1.getFullYear() === d2.getFullYear();
},

isPastDay(d) {
const today = new Date();
today.setHours(0, 0, 0, 0);
return d < today;
},

isDayBetween(d, d1, d2) {
d = DateUtils.clone(d);
d1 = DateUtils.clone(d1);
d2 = DateUtils.clone(d2);

d.setHours(0, 0, 0, 0);
d1.setHours(0, 0, 0, 0);
d2.setHours(0, 0, 0, 0);
return (d1 < d && d < d2) || (d2 < d && d < d1);
},

addDayToRange(day, range) {
let { from, to } = range;
if (!from) {
from = day;
}
else if (from && to && DateUtils.isSameDay(from, to) && DateUtils.isSameDay(day, from)) {
// reset when selecting again the first day
from = null;
to = null;
}
else if (to && day < from) {
from = day;
}
else if (to && DateUtils.isSameDay(day, to)) {
from = day;
to = day;
}
else {
to = day;
if (to < from) {
to = from;
from = day;
}
}

return { from, to };
},

isDayInRange(day, range) {
const { from, to } = range;
return (from && DateUtils.isSameDay(day, from)) ||
(to && DateUtils.isSameDay(day, to)) ||
(from && to && DateUtils.isDayBetween(day, from, to));
}

};

export default DateUtils;
154 changes: 154 additions & 0 deletions test/DateUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@

import { expect } from "chai";
import DateUtils from "../src/DateUtils";

describe("DateUtils", () => {

describe("clone", () => {
it("should clone a date", () => {
const date = new Date();
const newDate = DateUtils.clone(date);
expect(newDate).to.not.equal(date);
});
});

describe("isDayOutsideMonth", () => {
it("detects days outside a month", () => {
const isOutside = DateUtils.isDayOutsideMonth(
new Date(2015, 10, 30),
new Date(2015, 11, 1)
);
expect(isOutside).to.be.true;
});
it("does detect days inside a month", () => {
const isOutside = DateUtils.isDayOutsideMonth(
new Date(2015, 11, 30),
new Date(2015, 11, 1)
);
expect(isOutside).to.be.false;
});
});

describe("isSameDay", () => {
it("returns true if two days differ only by time", () => {
const day1 = new Date(2015, 10, 11, 5, 25);
const day2 = new Date(2015, 10, 11, 7, 40);
const isSameDay = DateUtils.isSameDay(day1, day2);
expect(isSameDay).to.be.true;
});
it("returns false for different days", () => {
const day1 = new Date(2015, 8, 12);
const day2 = new Date(2015, 5, 11);
const isSameDay = DateUtils.isSameDay(day1, day2);
expect(isSameDay).to.be.false;
});
});

describe("isPastDay", () => {
it("detects a day is in the past", () => {
const isPastDay = DateUtils.isPastDay(new Date(2015, 5, 11));
expect(isPastDay).to.be.true;
});
it("detects a day in the future", () => {
const isPastDay = DateUtils.isPastDay(new Date(2100, 1, 11));
expect(isPastDay).to.be.false;
});
it("says today is not a past day", () => {
const isPastDay = DateUtils.isPastDay(new Date());
expect(isPastDay).to.be.false;
});
});

describe("isDayBetween", () => {
it("detects a day between two days", () => {
const d = new Date(2015, 10, 12);
const d1 = new Date(2015, 10, 7);
const d2 = new Date(2015, 10, 16);
const isDayBetweenA = DateUtils.isDayBetween(d, d1, d2);
expect(isDayBetweenA).to.be.true;
const isDayBetweenB = DateUtils.isDayBetween(d, d2, d1);
expect(isDayBetweenB).to.be.true;
});
it("strictly excludes the given days", () => {
const d = new Date(2015, 10, 7);
const d1 = new Date(2015, 10, 7);
const d2 = new Date(2015, 10, 16);
const isDayBetween = DateUtils.isDayBetween(d, d1, d2);
expect(isDayBetween).to.be.false;
});
});

describe("addDayToRange", () => {
it("set the day as starting day, if missing", () => {
const day = new Date();
let range = { from: null, to: null };
range = DateUtils.addDayToRange(day, range);
expect(range).to.deep.equal({ from: day, to: null});
});
it("resets when selecting again the same from day", () => {
const day = new Date();
let range = { from: day, to: day };
range = DateUtils.addDayToRange(day, range);
expect(range).to.deep.equal({ from: null, to: null});
});
it("replaces the first day if given day comes before it ", () => {
const day = new Date(2015, 10, 1);
const to = new Date(2015, 10, 20);
let range = { from: new Date(2015, 10, 5), to: to };
range = DateUtils.addDayToRange(day, range);
expect(range).to.deep.equal({ from: day, to: to});
});
it("set the range to the same day if last day is same as first", () => {
const day = new Date(2015, 10, 7);
const to = new Date(2015, 10, 7);
const from = new Date(2015, 10, 4);
let range = { from: from, to: to };
range = DateUtils.addDayToRange(day, range);
expect(range).to.deep.equal({ from: day, to: day});
});
it("adds the day to the end of the range", () => {
const day = new Date(2015, 10, 5);
const from = new Date(2015, 10, 4);
let range = { from: from };
range = DateUtils.addDayToRange(day, range);
expect(range).to.deep.equal({ from: from, to: day});
});
it("works when last day comes before the first day", () => {
const day = new Date(2015, 10, 4);
const from = new Date(2015, 10, 5);
let range = { from: from };
range = DateUtils.addDayToRange(day, range);
expect(range).to.deep.equal({ from: day, to: from});
});
});

describe("isDayInRange", () => {
it("detects a day in a range", () => {
const day = new Date(2015, 10, 5);
const from = new Date(2015, 10, 4);
const to = new Date(2015, 10, 6);
const range = { from, to };
const isDayInRange = DateUtils.isDayInRange(day, range);
expect(isDayInRange).to.be.true;
});
it("detects a day in a range (inclusive)", () => {
const day = new Date(2015, 10, 4);
const from = new Date(2015, 10, 4);
const to = new Date(2015, 10, 6);
const range = { from, to };
const isDayInRange = DateUtils.isDayInRange(day, range);
expect(isDayInRange).to.be.true;
});
it("detects a day outside a range", () => {
const day = new Date(2015, 10, 15);
const from = new Date(2015, 10, 4);
const to = new Date(2015, 10, 6);
const range = { from, to };
const isDayInRange = DateUtils.isDayInRange(day, range);
expect(isDayInRange).to.be.false;
});
});



});

0 comments on commit f555948

Please sign in to comment.