<commit_before>
class PreviousResults {
  constructor(initialResults) {
    if (initialResults && !Array.isArray(initialResults)) {
      throw new Error(`If an initialValue is passed to PreviousResults constructor it must be an array but it was ${typeof initialResults}`);
    }
    this.results = initialResults || [];
  }

  first() {
    return this.results[0];
  }

  last() {
    return this.results.length
      ? this.results[this.results.length - 1]
      : undefined;
  }

  getStats() {
    return this.results.reduce((acc, problem) => {
      const { task, actual, timeTaken } = problem;
      const [,,, answer] = task;

      const {
        incorrects,
      } = acc;
      let {
        correctCount,
        longestTime,
        shortestTime,
      } = acc;

      if (!longestTime) {
        longestTime = problem;
      } else {
        const { timeTaken: currentLongTime } = longestTime;

        if (currentLongTime < timeTaken) {
          longestTime = problem;
        }
      }

      if (!shortestTime) {
        shortestTime = problem;
      } else {
        const { timeTaken: currentShortTime } = shortestTime;

        if (currentShortTime > timeTaken) {
          shortestTime = problem;
        }
      }


      if (answer === actual) {
        correctCount += 1;
      } else {
        incorrects.push(problem);
      }

      return {
        correctCount,
        incorrects,
        longestTime,
        shortestTime,
        totalTime: acc.totalTime + timeTaken,
      };
    }, { correctCount: 0, incorrects: [], totalTime: 0 });
  }
}

module.exports = PreviousResults;
<commit_msg>Make a BaseArray class for future use. Issue #219<commit_after>
class BaseArray {
  constructor(initial) {
    if (initial && !Array.isArray(initial)) {
      throw new Error(`If an initial value is passed to BaseArray constructor it must be an array but it was ${typeof initial}`);
    }
    this.array = initial || [];
  }

  first() {
    return this.array[0];
  }

  last() {
    return this.array.length
      ? this.array[this.array.length - 1]
      : undefined;
  }

  push(item) {
    this.array.push(item);
  }
}

class PreviousResults extends BaseArray {
  getStats() {
    return this.array.reduce((acc, problem) => {
      const { task, actual, timeTaken } = problem;
      const [,,, answer] = task;

      const {
        incorrects,
      } = acc;
      let {
        correctCount,
        longestTime,
        shortestTime,
      } = acc;

      if (!longestTime) {
        longestTime = problem;
      } else {
        const { timeTaken: currentLongTime } = longestTime;

        if (currentLongTime < timeTaken) {
          longestTime = problem;
        }
      }

      if (!shortestTime) {
        shortestTime = problem;
      } else {
        const { timeTaken: currentShortTime } = shortestTime;

        if (currentShortTime > timeTaken) {
          shortestTime = problem;
        }
      }


      if (answer === actual) {
        correctCount += 1;
      } else {
        incorrects.push(problem);
      }

      return {
        correctCount,
        incorrects,
        longestTime,
        shortestTime,
        totalTime: acc.totalTime + timeTaken,
      };
    }, { correctCount: 0, incorrects: [], totalTime: 0 });
  }
}

module.exports = PreviousResults;

{'fraction_non_alphanumeric': 0.11258677935405977, 'fraction_numerical': 0.00392393600965892, 'mean_word_length': 1.9802158273381294, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1873465', 'n_tokens_mistral': 999, 'n_tokens_neox': 981, 'n_words': 231}