<commit_before>// Mirrors Go text OT library.
//
// TODO: Shared, data-driven unit tests.

'use strict';

var DEBUG_OT = false;

function opFromString(s) {
  var colon = s.indexOf(':');
  if (colon === -1) {
    console.assert(false, 'Failed to parse operation "' + s + '"');
  }
  var pos = parseInt(s.substr(1, colon));
  if (s[0] === 'i') {
    return new Insert(pos, s.substr(colon + 1));
  } else if (s[0] === 'd') {
    return new Delete(pos, parseInt(s.substr(colon + 1)));
  }
  console.assert(false, 'Unknown operation "' + s[0] + '"');
}

function opsFromStrings(strs) {
  var ops = new Array(strs.length);
  for (var i = 0; i < strs.length; i++) {
    ops[i] = opFromString(strs[i]);
  }
  return ops;
}

function opsToStrings(ops) {
  var strs = new Array(ops.length);
  for (var i = 0; i < ops.length; i++) {
    strs[i] = ops[i].toString();
  }
  return strs;
}

function Insert(pos, value) {
  this.pos = pos;
  this.value = value;
}

Insert.prototype.toString = function() {
  return 'i' + this.pos + ':' + this.value;
};

Insert.prototype.typeName = function() {
  return 'Insert';
};

function Delete(pos, len) {
  this.pos = pos;
  this.len = len;
}

Delete.prototype.toString = function() {
  return 'd' + this.pos + ':' + this.len;
};

Delete.prototype.typeName = function() {
  return 'Delete';
};

function transformInsertDelete(a, b) {
  if (a.pos <= b.pos) {
    return [a, new Delete(b.pos + a.value.length, b.len)];
  } else if (a.pos < b.pos + b.len) {
    return [new Insert(b.pos, ''),
            new Delete(b.pos, b.len + a.value.length)];
  } else {
    return [new Insert(a.pos - b.len, a.value), b];
  }
}

function transform(a, b) {
  /* jshint -W086 */
  // https://github.com/jshint/jshint/blob/master/src/messages.js
  if (DEBUG_OT) console.log('transform(' + a + ', ' + b + ')');
  switch (a.typeName()) {
  case 'Insert':
    switch (b.typeName()) {
    case 'Insert':
      if (b.pos <= a.pos) {
        return [new Insert(a.pos + b.value.length, a.value), b];
      } else {
        return [a, new Insert(b.pos + a.value.length, b.value)];
      }
    case 'Delete':
      return transformInsertDelete(a, b);
    }
  case 'Delete':
    switch (b.typeName()) {
    case 'Insert':
      var insDel = transformInsertDelete(b, a);
      return [insDel[1], insDel[0]];
    case 'Delete':
      var aEnd = a.pos + a.len;
      var bEnd = b.pos + b.len;
      if (aEnd <= b.pos) {<commit_msg>tweak js ot to better mirror go<commit_after>// Mirrors Go text OT library.
//
// TODO: Shared, data-driven unit tests.

'use strict';

var DEBUG_OT = false;

function opFromString(s) {
  var colon = s.indexOf(':');
  if (colon === -1) {
    console.assert(false, 'Failed to parse operation "' + s + '"');
  }
  var pos = parseInt(s.substr(1, colon));
  if (s[0] === 'i') {
    return new Insert(pos, s.substr(colon + 1));
  } else if (s[0] === 'd') {
    return new Delete(pos, parseInt(s.substr(colon + 1)));
  }
  console.assert(false, 'Unknown operation "' + s[0] + '"');
}

function opsFromStrings(strs) {
  var ops = new Array(strs.length);
  for (var i = 0; i < strs.length; i++) {
    ops[i] = opFromString(strs[i]);
  }
  return ops;
}

function opsToStrings(ops) {
  var strs = new Array(ops.length);
  for (var i = 0; i < ops.length; i++) {
    strs[i] = ops[i].toString();
  }
  return strs;
}

function Insert(pos, value) {
  this.pos = pos;
  this.value = value;
}

Insert.prototype.toString = function() {
  return 'i' + this.pos + ':' + this.value;
};

Insert.prototype.typeName = function() {
  return 'Insert';
};

function Delete(pos, len) {
  this.pos = pos;
  this.len = len;
}

Delete.prototype.toString = function() {
  return 'd' + this.pos + ':' + this.len;
};

Delete.prototype.typeName = function() {
  return 'Delete';
};

function transformInsertDelete(a, b) {
  if (a.pos <= b.pos) {
    return [a, new Delete(b.pos + a.value.length, b.len)];
  } else if (a.pos < b.pos + b.len) {
    return [new Insert(b.pos, ''), new Delete(b.pos, b.len + a.value.length)];
  } else {
    return [new Insert(a.pos - b.len, a.value), b];
  }
}

function transform(a, b) {
  /* jshint -W086 */
  // https://github.com/jshint/jshint/blob/master/src/messages.js
  if (DEBUG_OT) console.log('transform(' + a + ', ' + b + ')');
  switch (a.typeName()) {
  case 'Insert':
    switch (b.typeName()) {
    case 'Insert':
      if (b.pos <= a.pos) {
        return [new Insert(a.pos + b.value.length, a.value), b];
      } else {
        return [a, new Insert(b.pos + a.value.length, b.value)];
      }
    case 'Delete':
      return transformInsertDelete(a, b);
    }
  case 'Delete':
    switch (b.typeName()) {
    case 'Insert':
      var insDel = transformInsertDelete(b, a);
      return [insDel[1], insDel[0]];
    case 'Delete':
      var aEnd = a.pos + a.len;
      var bEnd = b.pos + b.len;
      if (aEnd <= b.pos) {
{'fraction_non_alphanumeric': 0.19169066227889758, 'fraction_numerical': 0.005758946935417524, 'mean_word_length': 2.7844357976653695, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2833573', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1821, 'n_words': 446}