« Back to Index

Mori.js Calendar Application (NodeJS) -> copied from the talk http://vimeo.com/96425437

View original Gist on GitHub

Mori Calendar.js

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")