<commit_before>// This class exists just to pick the correct operation class,
// when receiving operations (JSON) from remote clients/server
// it is a class and not a function for consistency, the API matches
// the other classes; Operation.from({}), Insert.from({}) etc.
class Operation {
  static from(data) {
    switch (data.command) {
      case "content":
        return Content.from(data);
      case "insert":
        return Insert.from(data);
      case "remove":
        return Remove.from(data);
      default:
        throw "Unkown operation: " + data.command;
    }
  }

  copy() {
    return Operation.from(this);
  }

  identical(other) {
    return (
      this.time === other.time &&
      this.command === other.command &&
      this.row === other.row &&
      this.column === other.column &&
      this.string === other.string
    );
  }

  before(other) {
    return new Date(this.time) < new Date(other.time);
  }

  after(other) {
    return new Date(this.time) > new Date(other.time);
  }

  json() {
    return JSON.stringify(this);
  }
}

class Insert extends Operation {
  constructor(string, row, column, time = new Date().toISOString()) {
    super();
    this.time = time;
    this.command = "insert";
    this.string = string;
    this.row = row;
    this.column = column;
  }

  static from(data) {
    return new Insert(data.string, data.row, data.column, data.time);
  }

  apply(content) {
    const lines = this.string.split("\n"); // Lines to insert into content
    const row = this.row;
    const column = this.column;

    const before = content.slice(0, row);
    const affected = content[row];
    const after = content.slice(row + 1);

    // Combine affected line and lines to insert:
    const edited = [];
    if (lines.length === 1) {
      // Easy case, we are not inserting any newlines,
      // so we just insert the string somewhere in the affected line:
      edited.push(affected.slice(0, column) + lines[0] + affected.slice(column));
    }
    else {
      // We are inserting more than 1 line, i.e. at least one newline:
      const first = affected.slice(0, column) + lines[0];
      const last = lines[lines.length - 1] + affected.slice(column);

      // extra is all the lines between first and last, if any:
      const extra = lines.slice(1, lines.length - 1);
      console.assert(extra.length === lines.length - 2);<commit_msg>Added explanation message to assert<commit_after>// This class exists just to pick the correct operation class,
// when receiving operations (JSON) from remote clients/server
// it is a class and not a function for consistency, the API matches
// the other classes; Operation.from({}), Insert.from({}) etc.
class Operation {
  static from(data) {
    switch (data.command) {
      case "content":
        return Content.from(data);
      case "insert":
        return Insert.from(data);
      case "remove":
        return Remove.from(data);
      default:
        throw "Unkown operation: " + data.command;
    }
  }

  copy() {
    return Operation.from(this);
  }

  identical(other) {
    return (
      this.time === other.time &&
      this.command === other.command &&
      this.row === other.row &&
      this.column === other.column &&
      this.string === other.string
    );
  }

  before(other) {
    return new Date(this.time) < new Date(other.time);
  }

  after(other) {
    return new Date(this.time) > new Date(other.time);
  }

  json() {
    return JSON.stringify(this);
  }
}

class Insert extends Operation {
  constructor(string, row, column, time = new Date().toISOString()) {
    super();
    this.time = time;
    this.command = "insert";
    this.string = string;
    this.row = row;
    this.column = column;
  }

  static from(data) {
    return new Insert(data.string, data.row, data.column, data.time);
  }

  apply(content) {
    const lines = this.string.split("\n"); // Lines to insert into content
    const row = this.row;
    const column = this.column;

    const before = content.slice(0, row);
    const affected = content[row];
    const after = content.slice(row + 1);

    // Combine affected line and lines to insert:
    const edited = [];
    if (lines.length === 1) {
      // Easy case, we are not inserting any newlines,
      // so we just insert the string somewhere in the affected line:
      edited.push(affected.slice(0, column) + lines[0] + affected.slice(column));
    }
    else {
      // We are inserting more than 1 line, i.e. at least one newline:
      const first = affected.slice(0, column) + lines[0];
      const last = lines[lines.length - 1] + affected.slice(column);

      // extra is all the lines between first and last, if any:
      const extra = lines.slice(1, lines.length - 1);
      console.assert(
        extra.length === lines.length - 2,
        "Since we have removed first and last element of lines, extra should be 2 shorter");
{'fraction_non_alphanumeric': 0.13007135575942916, 'fraction_numerical': 0.0050968399592252805, 'mean_word_length': 2.588880760790051, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, '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': '7010332', 'n_tokens_mistral': 1474, 'n_tokens_neox': 1430, 'n_words': 463}