Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active August 29, 2015 14:03
Show Gist options
  • Save Integralist/99577f14fb01101123bb to your computer and use it in GitHub Desktop.
Save Integralist/99577f14fb01101123bb to your computer and use it in GitHub Desktop.
Mori.js Calendar Application (NodeJS) -> copied from the talk http://vimeo.com/96425437
var mori = require("mori");
function Calendar(appointments, previousCalendar) {
appointments = appointments;
var cal = {};
cal.add = function(appointment) {
var withAppointments = mori.conj(appointments, appointment);
return Calendar(withAppointments, cal);
};
cal.upcoming = function(start, n) {
var futureAppointments = mori.filter(function(a) {
return a.date >= start;
}, appointments);
return mori.take(n, futureAppointments);
};
cal.undo = function() {
return previousCalendar || Calendar();
};
return cal;
}
var myCalendar = Calendar().add({
title: 'X',
date: Date.parse('2014-07-11T18:00') // update this
}).add({
title: 'Y',
date: Date.parse('2014-07-11T19:00') // update this
}).add({
title: 'Z',
date: Date.parse('2014-07-11T20:00') // update this
});
var nextAppointments = myCalendar.upcoming(Date.now(), 2);
console.log(
mori.map(function(a) {
return a.title;
}, nextAppointments).toString() // we need to convert the Mori object some how (.toString for ease)
); // => ("Z" "Y")
var previousCalendar = myCalendar.undo().undo();
var appointments = previousCalendar.upcoming(Date.now(), 2);
console.log(
mori.map(function(a) {
return a.title;
}, appointments).toString()
); // => ("X")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment