<commit_before>/* global moment */
/* exported Periods */

var namespace = 'periodTracker';
var datePattern = 'YYYY-MM-DD';
// this regexp is not strict as the date validation will be performed by moment.js
// var dateRegExp = new RegExp(/^\d{4}-\d{2}-\d{2}$/g);

var Periods = function() {
  var _today = moment().format(datePattern);
  var _list = [];
  var _intervals = [];
  var _average = 0;
  var _next = '';
  var _countdown = 0;

  Object.defineProperty(this, 'list', {
    get: function() {
      return _list;
    }
  });

  Object.defineProperty(this, 'intervals', {
    get: function() {
      return _intervals;
    }
  });

  Object.defineProperty(this, 'average', {
    get: function() {
      return _average;
    }
  });

  Object.defineProperty(this, 'next', {
    get: function() {
      return _next;
    }
  });

  Object.defineProperty(this, 'countdown', {
    get: function() {
      return _countdown;
    }
  });

  /**
   * Sort dates in descending order
   */
  var sortDesc = function() {
    _list.sort();
    _list.reverse();
  };

  /**
   * Check if date is valid
   * @returns {boolean}
   */
  var isValidDate = function(date) {
    // check if the pattern is ok
    // TODO
    // if (!dateRegExp.test(date)) {
    //   return false;
    // }
    // check if the date is actually valid
    return moment(date, datePattern).isValid();
  };

  /**
   * Add single occurance
   * @param {string} date - string in YYYY-DD-MM format
   * @returns {number} -1 if not successful, otherwise index number of the added element
   */
  var add = function(date) {
    if (!isValidDate(date)) {
      return -1;
    }
    if (_list.indexOf(date) !== -1) {
      return -1;
    }
    _list.unshift(date);
    sortDesc();
    var index = _list.indexOf(date);
    return index;
  };

  /**
   * Change single occurance
   * @param {string} oldDate - string in YYYY-DD-MM format
   * @param {string} newDate - string in YYYY-DD-MM format
   * @returns {number|object} -1 if not successful, otherwise object with index numbers of the added and removed elements
   */
  var edit = function(oldDate, newDate) {
    if (!isValidDate(oldDate) || !isValidDate(newDate)) {
      return -1;
    }
    var oldIndex = _list.indexOf(oldDate);
    if (_list.indexOf(newDate) !== -1 || oldIndex === -1) {
      return -1;
    }
    _list.splice(oldIndex, 1);
    _list.push(newDate);
    sortDesc();
    var newIndex = _list.indexOf(newDate);
    return {
      o: oldIndex,
      n: newIndex
    };
  };

  /**
   * Remove single occurance
   * @param {string} date - string in YYYY-DD-MM format
   * @returns {number} -1 if not successful, otherwise index number of the removed element
   */
  var remove = function(date) {
    if (!isValidDate(date)) {
      return -1;
    }
    var index = _list.indexOf(date);
    if (index === -1) {
      return -1;
    }
    _list.splice(index, 1);
    return index;
  };

  /**
   * Modify occurances
   * @param {string} how - How to change
   * @param {string} date - string in YYYY-DD-MM format
   * @returns {number} -1 if not successful, otherwise index number of the affected element
   */
  var modify = function(how, date) {
    var mod = -1;
    if (how === 'add') {
      mod = add(date);
    }
    if (how === 'edit') {
      mod = edit(date);
    }
    if (how === 'remove') {
      mod = remove(date);
    }
    if (mod !== -1) {
      calcAll();
      if (save()) {
        return mod;
      }
      return -1;
    }
    return -1;
  };

  /**
   * Calculate interval in days between occurances
   */
  var calcIntervals = function() {
    for (var i = 1; i < _list.length; i++) {
      _intervals.push( moment.range(_list[i], _list[i - 1]).diff('days') );
    }
  };

  /**
   * Calculate average interval in days between occurances
   */
  var calcAverage = function() {
    var sum = 0;
    var arr = _intervals.slice(0, 3);
    if (!arr.length) {
      _average = 0;
      return;
    }<commit_msg>Fixed calc intervals to start from fresh at every update<commit_after>/* global moment */
/* exported Periods */

var namespace = 'periodTracker';
var datePattern = 'YYYY-MM-DD';
// this regexp is not strict as the date validation will be performed by moment.js
// var dateRegExp = new RegExp(/^\d{4}-\d{2}-\d{2}$/g);

var Periods = function() {
  var _today = moment().format(datePattern);
  var _list = [];
  var _intervals = [];
  var _average = 0;
  var _next = '';
  var _countdown = 0;

  Object.defineProperty(this, 'list', {
    get: function() {
      return _list;
    }
  });

  Object.defineProperty(this, 'intervals', {
    get: function() {
      return _intervals;
    }
  });

  Object.defineProperty(this, 'average', {
    get: function() {
      return _average;
    }
  });

  Object.defineProperty(this, 'next', {
    get: function() {
      return _next;
    }
  });

  Object.defineProperty(this, 'countdown', {
    get: function() {
      return _countdown;
    }
  });

  /**
   * Sort dates in descending order
   */
  var sortDesc = function() {
    _list.sort();
    _list.reverse();
  };

  /**
   * Check if date is valid
   * @returns {boolean}
   */
  var isValidDate = function(date) {
    // check if the pattern is ok
    // TODO
    // if (!dateRegExp.test(date)) {
    //   return false;
    // }
    // check if the date is actually valid
    return moment(date, datePattern).isValid();
  };

  /**
   * Add single occurance
   * @param {string} date - string in YYYY-DD-MM format
   * @returns {number} -1 if not successful, otherwise index number of the added element
   */
  var add = function(date) {
    if (!isValidDate(date)) {
      return -1;
    }
    if (_list.indexOf(date) !== -1) {
      return -1;
    }
    _list.unshift(date);
    sortDesc();
    var index = _list.indexOf(date);
    return index;
  };

  /**
   * Change single occurance
   * @param {string} oldDate - string in YYYY-DD-MM format
   * @param {string} newDate - string in YYYY-DD-MM format
   * @returns {number|object} -1 if not successful, otherwise object with index numbers of the added and removed elements
   */
  var edit = function(oldDate, newDate) {
    if (!isValidDate(oldDate) || !isValidDate(newDate)) {
      return -1;
    }
    var oldIndex = _list.indexOf(oldDate);
    if (_list.indexOf(newDate) !== -1 || oldIndex === -1) {
      return -1;
    }
    _list.splice(oldIndex, 1);
    _list.push(newDate);
    sortDesc();
    var newIndex = _list.indexOf(newDate);
    return {
      o: oldIndex,
      n: newIndex
    };
  };

  /**
   * Remove single occurance
   * @param {string} date - string in YYYY-DD-MM format
   * @returns {number} -1 if not successful, otherwise index number of the removed element
   */
  var remove = function(date) {
    if (!isValidDate(date)) {
      return -1;
    }
    var index = _list.indexOf(date);
    if (index === -1) {
      return -1;
    }
    _list.splice(index, 1);
    return index;
  };

  /**
   * Modify occurances
   * @param {string} how - How to change
   * @param {string} date - string in YYYY-DD-MM format
   * @returns {number} -1 if not successful, otherwise index number of the affected element
   */
  var modify = function(how, date) {
    var mod = -1;
    if (how === 'add') {
      mod = add(date);
    }
    if (how === 'edit') {
      mod = edit(date);
    }
    if (how === 'remove') {
      mod = remove(date);
    }
    if (mod !== -1) {
      calcAll();
      if (save()) {
        return mod;
      }
      return -1;
    }
    return -1;
  };

  /**
   * Calculate interval in days between occurances
   */
  var calcIntervals = function() {
    _intervals = [];
    for (var i = 1; i < _list.length; i++) {
      _intervals.push( moment.range(_list[i], _list[i - 1]).diff('days') );
    }
  };

  /**
   * Calculate average interval in days between occurances
   */
  var calcAverage = function() {
    var sum = 0;
    var arr = _intervals.slice(0, 3);
    if (!arr.length) {
      _average = 0;
      return;
    }
{'fraction_non_alphanumeric': 0.1478978978978979, 'fraction_numerical': 0.007757757757757758, 'mean_word_length': 2.287947346770876, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7339439', 'n_tokens_mistral': 2693, 'n_tokens_neox': 2637, 'n_words': 752}