<commit_before>/**
 * Create an vCalendar file with all the current courses on it,
 * set to weekly, starting with the current week.
 */
var createVCalendar = function(periods) {
  var vCalendarData = 'BEGIN:VCALENDAR\r\n'+
    'VERSION:2.0\r\n' +
    'PRODID:-//yacs//NONSGML v1.0//EN\r\n';

  var uidCounter = 0;

  // Helper to pad single digit numbers with a 0 and leave double digit numbers unchanged.
  var pad0 = function(num) { return ('0' + num).slice(-2); }

  // Helper to extract the vCalendar formatted time from a Date. It looks like this:
  // YYYYMMDDTHHMMSS, where T is a literal T
  var getVCalendarStamp = function(d) {
    return d.getFullYear() + pad0(d.getMonth()+1) + pad0(d.getDate()) + 'T'
      + pad0(d.getHours()) + pad0(d.getMinutes()) + '00';
  }

  // Helper to convert a single period into a full VEVENT.
  var periodToVevent = function(period) {
    var d = new Date();
    var nowstamp = getVCalendarStamp(d); // save for the DTSTAMP field

    // dates need to be shuffled around by setting the date, so calculate how much it
    // needs to move by
    var weekday_offset = period.day - d.getDay();
    d.setDate(d.getDate() + weekday_offset);

    // at this point the date is set correctly, then the time needs to be set
    d.setHours(Math.floor(period.start / 60));
    d.setMinutes(period.start % 60);

    var startstamp = getVCalendarStamp(d);

    // compute end and take end stamp
    d.setMinutes(d.getMinutes() + (period.end - period.start));
    var endstamp = getVCalendarStamp(d);

    uidCounter++;
    return 'BEGIN:VEVENT\r\n' +
      'UID:event' + uidCounter + '@yacs.cs.rpi.edu\r\n' +
      'SUMMARY:' + period.tooltip + '\r\n' +
      'DTSTAMP:' + nowstamp + '\r\n' +
      'DTSTART:' + startstamp + '\r\n' +
      'DTEND:' + endstamp + '\r\n' +
      'RRULE:FREQ=WEEKLY' + '\r\n' +
      'END:VEVENT\r\n';

  };

  each(periods, function(period) {
    vCalendarData += periodToVevent(period);
  });
  vCalendarData += 'END:VCALENDAR';
  return vCalendarData;

};<commit_msg>Made vcalendar a full lib file<commit_after>/**
 * @namespace vCalendar
 * @description Functions associated with the vCalendar (ICS) download on the schedule page
 * @memberOf Yacs
 */
window.Yacs.vCalendar = new function() {
  var self = this;

  /**
   * Given a set of periods in the same form as they would be used
   * to generate a single schedule, return a string containing
   * that data formatted into a VCalendar, which if it were a file
   * could be imported into a calendar application.
   * All periods are set to recur weekly, starting at the current
   * week.
   */
  self.createVCalendar = function(periods) {
    var vCalendarData = 'BEGIN:VCALENDAR\r\n'+
      'VERSION:2.0\r\n' +
      'PRODID:-//yacs//NONSGML v1.0//EN\r\n';

    var uidCounter = 0;

    // Helper to pad single digit numbers with a 0 and leave double digit numbers unchanged.
    var pad0 = function(num) { return ('0' + num).slice(-2); }

    // Helper to extract the vCalendar formatted time from a Date. It looks like this:
    // YYYYMMDDTHHMMSS, where T is a literal T
    var getVCalendarStamp = function(d) {
      return d.getFullYear() + pad0(d.getMonth()+1) + pad0(d.getDate()) + 'T'
        + pad0(d.getHours()) + pad0(d.getMinutes()) + '00';
    }

    // Helper to convert a single period into a full VEVENT.
    var periodToVevent = function(period) {
      var d = new Date();
      var nowstamp = getVCalendarStamp(d); // save for the DTSTAMP field

      // dates need to be shuffled around by setting the date, so calculate how much it
      // needs to move by
      var weekday_offset = period.day - d.getDay();
      d.setDate(d.getDate() + weekday_offset);

      // at this point the date is set correctly, then the time needs to be set
      d.setHours(Math.floor(period.start / 60));
      d.setMinutes(period.start % 60);

      var startstamp = getVCalendarStamp(d);

      // compute end and take end stamp
      d.setMinutes(d.getMinutes() + (period.end - period.start));
      var endstamp = getVCalendarStamp(d);

      uidCounter++;
      return 'BEGIN:VEVENT\r\n' +
        'UID:event' + uidCounter + '@yacs.cs.rpi.edu\r\n' +
        'SUMMARY:' + period.tooltip + '\r\n' +
        'DTSTAMP:' + nowstamp + '\r\n' +
        'DTSTART:' + startstamp + '\r\n' +
        'DTEND:' + endstamp + '\r\n' +
        'RRULE:FREQ=WEEKLY' + '\r\n' +
        'END:VEVENT\r\n';

    };

    each(periods, function(period) {
      vCalendarData += periodToVevent(period);
    });
    vCalendarData += 'END:VCALENDAR';
    return vCalendarData;

  };

  /**
   * Prompt the user to download a vCalendar string as a file
   * named yacs-schedule.ics.
   * Do this by attaching a phantom <a> to the DOM and setting its
   * href to the calendar data, then simulating a click on it.
   */
  self.download = function(vCalendarData) {
    var elt = document.createElement('a');
    elt.setAttribute('href', 'data:text/calendar;charset=utf8,' + encodeURIComponent(vCalendarData));
    elt.setAttribute('download', 'yacs-schedule.ics');
    document.body.appendChild(elt);
    elt.click();
    document.body.removeChild(elt);
  };
};
{'fraction_non_alphanumeric': 0.1336432306798373, 'fraction_numerical': 0.007941119504164245, 'mean_word_length': 2.8223538119911176, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4540202', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1720, 'n_words': 507}