<commit_before>/*jslint indent: 2, maxlen: 80 */

// TODO
// - Write tests
//
// - Refactor the code; clean it up

$(function () {
  console.log("ready!");
  build_calendar_by_year(2014);

  $('#year').click(function () {
    year_clicked(this);
  });

  $('.month_name').click(function () {
    month_clicked(this);
  });

  $('.day').click(function () {
    day_clicked(this);
  });
});

var SELECTED = 'selected';
var SELSTART = 'select-start';
var SELEND   = 'select-end';
var MONTHS   = ['january', 'february', 'march', 'april',
                'may', 'june', 'july', 'august',
                'september', 'october', 'november', 'december'];
var DAYNAMES = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

function resetSelection() {
  $('.selected').removeClass(SELECTED);
  $('.select-start').removeClass(SELSTART);
  $('.select-end').removeClass(SELEND);
}

function resetSelectedDays() {
  $('.day.selected').removeClass(SELECTED);
}

function range(low, high) {
  var i, rangeArray = [];

  for (i = low; i <= high; i++) {
    rangeArray.push(i);
  }

  return rangeArray;
}

function build_calendar_by_year(year) {
  var populateWeek = function (startDate, startDay, numOfDays) {
    var i, dayNumber, weekDays = [0, 0, 0, 0, 0, 0, 0]; // Exactly 7 elements

    if (numOfDays > 7) {
      numOfDays = 7;
    }

    // TODO Clean this up; this is hard to read!
    for (i = startDay, dayNumber = startDate; i < numOfDays; i++, dayNumber++) {
      weekDays[i] = dayNumber;
    }

    return { weekdays: weekDays, daynumber: dayNumber };
  };

  var populateWeekDayNames = function (monthID) {
    var tableID   = '#' + monthID;

    $(tableID).append('<thead />');

    var tableHeader = $(tableID).find('thead')[0];

    $(tableHeader).append('<tr>');
    var tableHeaderRow = $(tableHeader).find('tr');
    $(tableHeaderRow).append('<th class="month_name" colspan="7">' + monthID + '</th>');

    $(tableHeader).append('<tr>');
    var tableHeaderRow = $(tableHeader).find('tr')[1];

    DAYNAMES.forEach(function (dayname) {
      $(tableHeaderRow).append('<th>' + dayname + '</th>');
    });
  };

  var populateMonth = function (year, month) {
    var monthID   = MONTHS[month];
    var firstDay  = new Date(year, month, 1).getDay(); // 0-6 => Sun-Sat
    var week      = [];
    var nextDate  = 1;
    var monthDays = new Date(year, month+1, 0).getDate(); // # of days in month
    var rowOffset = 0;
    var tableID   = '#' + monthID;

    populateWeekDayNames(monthID);

    $(tableID).append('<tbody />');
    var tableBody = $(tableID).find('tbody')[0];

    var weekInfo, tableBodyRow;
    while (monthDays > 0) {
      weekInfo  = populateWeek(nextDate, firstDay, monthDays); // first week of the month
      $(tableBody).append('<tr>');
      tableBodyRow = $(tableBody).find('tr:last');

      weekInfo.weekdays.forEach(function (weekday) {
        var dayText = '';
        var dayDate = weekday;
        var dayClass = '';

        if (dayDate > 0) {
          dayText = weekday;
          dayClass = 'day';
        }

        $(tableBodyRow).append('<td class="' + dayClass + '">' + dayText + '</td>');
      });

      // Generate data for the next week
      nextDate   = weekInfo.daynumber;
      monthDays -= 7 - firstDay;
      firstDay   = 0;
    }
  };

  // Layout the calendar tables
  var calendarYear = $('#year-calendar');
  $(calendarYear).append('<p id="year" />');

  MONTHS.forEach(function (month) {
    $(calendarYear).append('<table id="' + month + '" />');
  });

  // Update the year
  $('#year').text('' + year);

  for (var month = 0; month < 12; month++) {
    populateMonth(year, month);<commit_msg>Improved the month day calculation and looping<commit_after>/*jslint indent: 2, maxlen: 80 */

// TODO
// - Write tests
//
// - Refactor the code; clean it up

$(function () {
  console.log("ready!");
  build_calendar_by_year(2014);

  $('#year').click(function () {
    year_clicked(this);
  });

  $('.month_name').click(function () {
    month_clicked(this);
  });

  $('.day').click(function () {
    day_clicked(this);
  });
});

var SELECTED = 'selected';
var SELSTART = 'select-start';
var SELEND   = 'select-end';
var MONTHS   = ['january', 'february', 'march', 'april',
                'may', 'june', 'july', 'august',
                'september', 'october', 'november', 'december'];
var DAYNAMES = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

function resetSelection() {
  $('.selected').removeClass(SELECTED);
  $('.select-start').removeClass(SELSTART);
  $('.select-end').removeClass(SELEND);
}

function resetSelectedDays() {
  $('.day.selected').removeClass(SELECTED);
}

function range(low, high) {
  var i, rangeArray = [];

  for (i = low; i <= high; i++) {
    rangeArray.push(i);
  }

  return rangeArray;
}

function build_calendar_by_year(year) {
  var populateWeek = function (startDate, startDay, numOfDays) {
    var i, dayNumber, weekDays = [0, 0, 0, 0, 0, 0, 0]; // Exactly 7 elements

    if (numOfDays > 7) {
      numOfDays = 7;
    }

    // TODO Clean this up; this is hard to read!
    for (i = startDay, dayNumber = startDate; i < numOfDays; i++, dayNumber++) {
      weekDays[i] = dayNumber;
    }

    return { weekdays: weekDays, daynumber: dayNumber };
  };

  var populateWeekDayNames = function(monthID) {
    var tableID   = '#' + monthID;

    $(tableID).append('<thead />');

    var tableHeader = $(tableID).find('thead').first();

    $(tableHeader).append('<tr>');
    var tableHeaderRow = $(tableHeader).find('tr');
    $(tableHeaderRow).append('<th class="month_name" colspan="7">' + monthID + '</th>');

    $(tableHeader).append('<tr>');
    var tableHeaderRow = $(tableHeader).find('tr')[1];

    DAYNAMES.forEach(function (dayname) {
      $(tableHeaderRow).append('<th>' + dayname + '</th>');
    });
  };

  var populateMonth = function (year, month) { // Month range: 0-11
    var buildWeek = function() {
      return [0, 0, 0, 0, 0, 0, 0];
    };
    var fillWeek = function(startDate) {
      startDate = startDate || 0;
      while(startDate < 7) {
        week[startDate] = monthDate;
        monthDate++;
        startDate++;

        if (monthDate > daysInMonth) {
          break;
        }
      }
    };
    var monthArray = [];
    var monthDate = 1;
    var daysInWeek;

    // Get the number of days for the month
    var daysInMonth = new Date(year, month + 1, 0).getDate();

    // Find which day the first day of the month falls on
    var firstDay = new Date(year, month, 1).getDay();

    // Create first row of the week, based on the first day
    var week = buildWeek();
    fillWeek(firstDay);
    monthArray = week;

    // thereafter, fill the days until the end of the month
    while(monthDate <= daysInMonth) {
      week = buildWeek();
      fillWeek();
      monthArray = monthArray.concat(week);
    }

    // Now display the month in HTML
    var tableBody;
    var monthID = MONTHS[month];
    var tableID = '#' + monthID;
    var createTableRow;

    populateWeekDayNames(monthID);
    $(tableID).append('<tbody />');
    tableBody = $(tableID).find('tbody').first();
    createTableRow = (function() {
      $(tableBody).append('<tr>');
      return $(tableBody).find('tr:last');
    });

    tableRow = createTableRow();
    for (var i = 0; i < monthArray.length; i++) {
      if (i % 7 == 0) {
        tableRow = createTableRow();
      }

      if (monthArray[i] > 0) {
        $(tableRow).append('<td class="day">' + monthArray[i] + '</td>');
      } else {
        $(tableRow).append('<td />');
      }
    }
  };

  // Layout the calendar tables
  var calendarYear = $('#year-calendar');
  $(calendarYear).append('<p id="year" />');

  MONTHS.forEach(function (month) {
    $(calendarYear).append('<table id="' + month + '" />');
  });

  // Update the year
  $('#year').text('' + year);

  for (var month = 0; month < 12; month++) {
    populateMonth(year, month);
{'fraction_non_alphanumeric': 0.18646635525817448, 'fraction_numerical': 0.009720994823885873, 'mean_word_length': 2.8909626719056973, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6916625', 'n_tokens_mistral': 2752, 'n_tokens_neox': 2583, 'n_words': 629}