<commit_before>"use strict";

var fs = require("fs");
var path = require("path");
var ical = require("ical");
var groups = require("./data/groups");
var R = require("ramda");

var icsDir = "ics";
var eventJson = "data/events.json";

var isHiddenFile = R.pipe(R.lensIndex(0), R.eq("."));

function expandRecurrences(event) {
	var future = new Date(Date.now());
	future.setDate(future.getDate() + 365); // adding years gives you a date in the year "116", so just add days. leap years aren't important, we just want a rough future date

	if (event.rrule === undefined) {
		return [event];
	}
	return event.rrule.all(function(date) {
		return date < future;
	}).map(function(date) {
		var copy = R.clone(event);
		var duration = copy.end - copy.start;
		copy.start = date;
		copy.end = new Date(date);
		copy.end.setMilliseconds(copy.end.getMilliseconds() + duration);
		return copy;
	});
}

function enhanceEvent(group, event) {
	if (event.location) {
		event.mapQuery = event.location.replace("(", " ").replace(")", " ");
	}

	var start = event.start;
	event.startDate = start.toString();
	event.startDateJson = start.toJSON();

	var end = event.end;
	event.endDate = end.toString();
	event.endDateJson = end.toJSON();
	if (end < Date.now()) {
		event.expired = true;
	}

	event.group = group;
	event.groupName = groups[group].name;
	event.groupUrl = groups[group].web;

	delete event.rrule;

	return event;
}

var icsPath = R.curryN(2, path.join)(icsDir);

function loadCal(filename) {
	var group = filename.substr(0, filename.indexOf("."));
	return R.pipe(
		R.values,
		R.filter(R.propEq("type", "VEVENT")),
		R.chain(expandRecurrences),
		R.map(enhanceEvent.bind(this, group))
	)(ical.parseFile(icsPath(filename)));
}

function isOld(date) {
	var now = new Date(Date.now());
	now.setDate(now.getDate() - 1);
	return date < now;
}

function writeJson(filename, data) {
	fs.writeFileSync(filename, JSON.stringify(data, null, 4), { encoding: "UTF8" });
}

var events = R.pipe(
	R.reject(isHiddenFile),
	R.chain(loadCal),
	R.sortBy(R.prop("start")),
	R.reject(R.compose(isOld, R.prop("end")))
)(fs.readdirSync(icsDir));

writeJson(eventJson, events);<commit_msg>Tabs to spaces<commit_after>"use strict";

var fs = require("fs");
var path = require("path");
var ical = require("ical");
var groups = require("./data/groups");
var R = require("ramda");

var icsDir = "ics";
var eventJson = "data/events.json";

var isHiddenFile = R.pipe(R.lensIndex(0), R.eq("."));

function expandRecurrences(event) {
  var future = new Date(Date.now());
  future.setDate(future.getDate() + 365); // adding years gives you a date in the year "116", so just add days. leap years aren't important, we just want a rough future date

  if (event.rrule === undefined) {
    return [event];
  }
  return event.rrule.all(function(date) {
    return date < future;
  }).map(function(date) {
    var copy = R.clone(event);
    var duration = copy.end - copy.start;
    copy.start = date;
    copy.end = new Date(date);
    copy.end.setMilliseconds(copy.end.getMilliseconds() + duration);
    return copy;
  });
}

function enhanceEvent(group, event) {
  if (event.location) {
    event.mapQuery = event.location.replace("(", " ").replace(")", " ");
  }

  var start = event.start;
  event.startDate = start.toString();
  event.startDateJson = start.toJSON();

  var end = event.end;
  event.endDate = end.toString();
  event.endDateJson = end.toJSON();
  if (end < Date.now()) {
    event.expired = true;
  }

  event.group = group;
  event.groupName = groups[group].name;
  event.groupUrl = groups[group].web;

  delete event.rrule;

  return event;
}

var icsPath = R.curryN(2, path.join)(icsDir);

function loadCal(filename) {
  var group = filename.substr(0, filename.indexOf("."));
  return R.pipe(
    R.values,
    R.filter(R.propEq("type", "VEVENT")),
    R.chain(expandRecurrences),
    R.map(enhanceEvent.bind(this, group))
  )(ical.parseFile(icsPath(filename)));
}

function isOld(date) {
  var now = new Date(Date.now());
  now.setDate(now.getDate() - 1);
  return date < now;
}

function writeJson(filename, data) {
  fs.writeFileSync(filename, JSON.stringify(data, null, 4), { encoding: "UTF8" });
}

var events = R.pipe(
  R.reject(isHiddenFile),
  R.chain(loadCal),
  R.sortBy(R.prop("start")),
  R.reject(R.compose(isOld, R.prop("end")))
)(fs.readdirSync(icsDir));

writeJson(eventJson, events);
{'fraction_non_alphanumeric': 0.17617526243724327, 'fraction_numerical': 0.005476951163852123, 'mean_word_length': 4.168632075471698, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, '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': '3965231', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1551, 'n_words': 349}