Skip to content

Instantly share code, notes, and snippets.

@Harrison-M
Created June 27, 2012 00:45
Show Gist options
  • Save Harrison-M/3000502 to your computer and use it in GitHub Desktop.
Save Harrison-M/3000502 to your computer and use it in GitHub Desktop.
Scheduling Kata

Write a component that allow you to schedule calendar events (similar to google calendar or outlook).

Here are some features that you may want to implement:

ability to schedule something "today"

ability to schedule something for as specific date eg: 12/15/2012

ability to schedule something for "next tuesday"

ability to schedule something for "the first of the month"

ability to schedule something "every other monday starting next monday"

your component should provide ways to ask if there are any events for a specific date, for example: if I schedule an event "the first of the month", I should be able to ask if there is an event occurrence on 12/1/2055.

the sky's the limit

your implementation doesn't need to take in strings for event information, use whatever structures make sense for your component.

var oneday = 86400000;
var events = [];
function clearEvents()
{
events = [];
}
function isEventOn(date)
{
return events.indexOf(date.getTime()) != -1
}
function schedule(date)
{
var parsedDate = new Date(date);
if(parsedDate.toString() === "Invalid Date")
{
events.push(parseCasual(date));
}
else
{
events.push(normalizeDate(parsedDate));
}
}
function parseCasual(request)
{
request = request.trim().toLowerCase();
if(request === "today")
{
return normalizeDate(new Date(Date.now()));
}
if(request === "tomorrow")
{
return normalizeDate(new Date(Date.now() + oneday));
}
}
function normalizeDate(date)
{
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
return date.getTime();
}
describe("normalized", function() {
it("normalizes", function(){
return expect(new Date("1/1/2012").getTime()).toEqual(normalizeDate(new Date("1/1/2012 1:00")));
});
});
describe("scheduled", function() {
return it("is scheduled", function() {
var date = "1/1/2012";
schedule(date);
return expect(events).toContain(new Date(date).getTime());
});
});
describe("parse", function() {
var now = Date.now();
it("can schedule today", function(){
schedule("today");
return expect(events).toContain(normalizeDate(new Date(now)));
});
it("can schedule tomorrow", function(){
schedule("tomorrow");
return expect(events).toContain(normalizeDate(new Date(now + 86400000)));
});
});
describe("clearevents",function() {
it("can clear events", function() {
schedule("1/1/2012");
clearEvents();
return expect(events.length).toEqual(0);
});
});
describe("iseventon", function() {
it("can detect when an event isn't there", function(){
clearEvents();
return expect(isEventOn(new Date("1/1/9999"))).toBeFalsy();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment