<commit_before>/** @jsx React.DOM */

(function () {


Votr.sortAs = {};


Votr.sortAs.personName = function (text) {
  var words = text.replace(/,/g, '').split(' ');
  words = _.filter(words, (word) => !word.match(/\.$/));
  words.push(words.shift());   // first name goes to the end
  return words.join(' ').toLowerCase();
  // TODO: consider using latinise (see fajr).
};


Votr.sortAs.number = function (text) {
  return +text.replace(/,/g, '.');
  // TODO: this won't be needed when fladgejt starts returning numbers
};


Votr.sortAs.date = function (date) {
  if (date.match(/^\d\d\.\d\d\.\d\d\d\d/)) {
    return date.substring(6, 10) + date.substring(3, 5) + date.substring(0, 2) + date.substring(10);
  }
  return date;
};


Votr.sortAs.interval = function (text) {
  var index = text.indexOf('do ');
  if (index == -1) return '';
  return Votr.sortAs.date(text.substring(index + 3));
};


Votr.sortTable = function (items, columns, query, queryKey) {
  var orderString = query[queryKey] || columns.defaultOrder;
  var order = orderString ? orderString.split(/(?=[ad])/) : [];
  var orderLength = order.length;
  var orderAsc = order.map((orderItem) => orderItem.substring(0, 1) == 'a');
  var orderColumns = order.map((orderItem) => columns[orderItem.substring(1)]);

  items = items.map((item, index) => {
    var criteria = [];
    for (var i = 0; i < orderLength; i++) {
      var [label, prop, process, preferDesc] = orderColumns[i];
      var value = prop ? item[prop] : item;
      if (process) value = process(value);
      if (!prop && !process) value = undefined;
      criteria.push(value);
    }
    return { item, index, criteria };
  });

  items.sort((a, b) => {
    for (var i = 0; i < orderLength; i++) {
      var ac = a.criteria[i], bc = b.criteria[i];
      if (ac === bc) continue;
      if (ac < bc) return orderAsc[i] ? -1 : 1;
      if (ac > bc) return orderAsc[i] ? 1 : -1;
    }
    return a.index - b.index;
  });

  items = items.map((a) => a.item);

  function handleClick(event) {
    var index = event.target.getAttribute('data-index');
    var [label, prop, process, preferDesc] = columns[index];

    var newOrder = _.without(order, 'a' + index, 'd' + index);
    newOrder.unshift((
      order[0] == 'a' + index ? 'd' :
      order[0] == 'd' + index ? 'a' :
      preferDesc ? 'd' : 'a') + index);

    var newQuery = _.assign({}, query);
    newQuery[queryKey] = newOrder.join('');
    Votr.navigate(newQuery);
  }

  var header = <tr>
    {columns.map(([label, prop, process, preferDesc], index) =>
      <th key={index} data-index={index} onClick={handleClick}
          className={'sort ' + (order[0] == 'a' + index ? 'asc' :
                                order[0] == 'd' + index ? 'desc' : '')}>
        {label}
      </th>
    )}
  </tr>;

  return [items, header];
};

<commit_msg>Fix sorting when the <td> has child elements.<commit_after>/** @jsx React.DOM */

(function () {


Votr.sortAs = {};


Votr.sortAs.personName = function (text) {
  var words = text.replace(/,/g, '').split(' ');
  words = _.filter(words, (word) => !word.match(/\.$/));
  words.push(words.shift());   // first name goes to the end
  return words.join(' ').toLowerCase();
  // TODO: consider using latinise (see fajr).
};


Votr.sortAs.number = function (text) {
  return +text.replace(/,/g, '.');
  // TODO: this won't be needed when fladgejt starts returning numbers
};


Votr.sortAs.date = function (date) {
  if (date.match(/^\d\d\.\d\d\.\d\d\d\d/)) {
    return date.substring(6, 10) + date.substring(3, 5) + date.substring(0, 2) + date.substring(10);
  }
  return date;
};


Votr.sortAs.interval = function (text) {
  var index = text.indexOf('do ');
  if (index == -1) return '';
  return Votr.sortAs.date(text.substring(index + 3));
};


Votr.sortTable = function (items, columns, query, queryKey) {
  var orderString = query[queryKey] || columns.defaultOrder;
  var order = orderString ? orderString.split(/(?=[ad])/) : [];
  var orderLength = order.length;
  var orderAsc = order.map((orderItem) => orderItem.substring(0, 1) == 'a');
  var orderColumns = order.map((orderItem) => columns[orderItem.substring(1)]);

  items = items.map((item, index) => {
    var criteria = [];
    for (var i = 0; i < orderLength; i++) {
      var [label, prop, process, preferDesc] = orderColumns[i];
      var value = prop ? item[prop] : item;
      if (process) value = process(value);
      if (!prop && !process) value = undefined;
      criteria.push(value);
    }
    return { item, index, criteria };
  });

  items.sort((a, b) => {
    for (var i = 0; i < orderLength; i++) {
      var ac = a.criteria[i], bc = b.criteria[i];
      if (ac === bc) continue;
      if (ac < bc) return orderAsc[i] ? -1 : 1;
      if (ac > bc) return orderAsc[i] ? 1 : -1;
    }
    return a.index - b.index;
  });

  items = items.map((a) => a.item);

  function handleClick(event) {
    var index = event.currentTarget.getAttribute('data-index');
    var [label, prop, process, preferDesc] = columns[index];

    var newOrder = _.without(order, 'a' + index, 'd' + index);
    newOrder.unshift((
      order[0] == 'a' + index ? 'd' :
      order[0] == 'd' + index ? 'a' :
      preferDesc ? 'd' : 'a') + index);

    var newQuery = _.assign({}, query);
    newQuery[queryKey] = newOrder.join('');
    Votr.navigate(newQuery);
  }

  var header = <tr>
    {columns.map(([label, prop, process, preferDesc], index) =>
      <th key={index} data-index={index} onClick={handleClick}
          className={'sort ' + (order[0] == 'a' + index ? 'asc' :
                                order[0] == 'd' + index ? 'desc' : '')}>
        {label}
      </th>
    )}
  </tr>;

  return [items, header];
};


{'fraction_non_alphanumeric': 0.1964943032427695, 'fraction_numerical': 0.008413672217353198, 'mean_word_length': 2.8269617706237424, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 28, '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': '3722957', 'n_tokens_mistral': 2064, 'n_tokens_neox': 2015, 'n_words': 491}