<commit_before>// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {ColumnDescriptor, Events, Parameters} from './DataGrid.js';  // eslint-disable-line no-unused-vars
import {ViewportDataGrid, ViewportDataGridNode} from './ViewportDataGrid.js';

/**
 * @unrestricted
 * @extends {ViewportDataGrid<!SortableDataGridNode<NODE_TYPE>>}
 * @template NODE_TYPE
 */
export class SortableDataGrid extends ViewportDataGrid {
  /**
   * @param {!Parameters} dataGridParameters
   */
  constructor(dataGridParameters) {
    super(dataGridParameters);
    this._sortingFunction = SortableDataGrid.TrivialComparator;
    this.setRootNode(/** @type {!SortableDataGridNode<!NODE_TYPE>} */ (new SortableDataGridNode()));
  }

  /**
   * @param {!SortableDataGridNode<!NODE_TYPE>} a
   * @param {!SortableDataGridNode<!NODE_TYPE>} b
   * @return {number}
   * @template NODE_TYPE
   */
  static TrivialComparator(a, b) {
    return 0;
  }

  /**
   * @param {string} columnId
   * @param {!SortableDataGridNode<!NODE_TYPE>} a
   * @param {!SortableDataGridNode<!NODE_TYPE>} b
   * @return {number}
   * @template NODE_TYPE
   */
  static NumericComparator(columnId, a, b) {
    const aValue = a.data[columnId];
    const bValue = b.data[columnId];
    const aNumber = Number(aValue instanceof Node ? aValue.textContent : aValue);
    const bNumber = Number(bValue instanceof Node ? bValue.textContent : bValue);
    return aNumber < bNumber ? -1 : (aNumber > bNumber ? 1 : 0);
  }

  /**
   * @param {string} columnId
   * @param {!SortableDataGridNode<!NODE_TYPE>} a
   * @param {!SortableDataGridNode<!NODE_TYPE>} b
   * @return {number}
   * @template NODE_TYPE
   */
  static StringComparator(columnId, a, b) {
    const aValue = a.data[columnId];
    const bValue = b.data[columnId];
    const aString = aValue instanceof Node ? aValue.textContent : String(aValue);
    const bString = bValue instanceof Node ? bValue.textContent : String(bValue);
    if (!aString || !bString) {
      return 0;
    }
    return aString < bString ? -1 : (aString > bString ? 1 : 0);
  }

  /**
   * @param {function(!SortableDataGridNode<!NODE_TYPE>, !SortableDataGridNode<!NODE_TYPE>):number} comparator
   * @param {boolean} reverseMode
   * @param {!SortableDataGridNode<!NODE_TYPE>} a
   * @param {!SortableDataGridNode<!NODE_TYPE>} b
   * @return {number}
   * @template NODE_TYPE
   */
  static Comparator(comparator, reverseMode, a, b) {
    return reverseMode ? comparator(b, a) : comparator(a, b);
  }

  /**
   * @param {!Array.<string>} columnNames
   * @param {!Array.<string>} values
   * @param {string} displayName
   * @return {?SortableDataGrid<!SortableDataGridNode<!NODE_TYPE>>}
   * @template NODE_TYPE
   */
  static create(columnNames, values, displayName) {
    const numColumns = columnNames.length;
    if (!numColumns) {
      return null;
    }

    const columns = /** @type {!Array<!ColumnDescriptor>} */ ([]);
    for (let i = 0; i < columnNames.length; ++i) {
      const id = String(i);
      columns.push(/** @type {!ColumnDescriptor} */ ({id, title: columnNames[i], sortable: true}));
    }

    const nodes = [];
    for (let i = 0; i < values.length / numColumns; ++i) {
      /** @type {!Object<number, string>} */
      const data = {};
      for (let j = 0; j < columnNames.length; ++j) {
        data[j] = values[numColumns * i + j];
      }

      const node = new SortableDataGridNode(data);
      node.selectable = false;
      nodes.push(node);
    }

    const dataGrid = new SortableDataGrid(/** @type {!Parameters} */ ({displayName, columns}));
    const length = nodes.length;
    const rootNode = dataGrid.rootNode();
    for (let i = 0; i < length; ++i) {
      rootNode.appendChild(nodes[i]);
    }

    dataGrid.addEventListener(Events.SortingChanged, sortDataGrid);

    function sortDataGrid() {
      const nodes = dataGrid.rootNode().children;
      const sortColumnId = dataGrid.sortColumnId();
      if (!sortColumnId) {
        return;
      }

      let columnIsNumeric = true;<commit_msg>Fix type for SortableDataGrid.create<commit_after>// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {ColumnDescriptor, Events, Parameters} from './DataGrid.js';  // eslint-disable-line no-unused-vars
import {ViewportDataGrid, ViewportDataGridNode} from './ViewportDataGrid.js';

/**
 * @unrestricted
 * @extends {ViewportDataGrid<!SortableDataGridNode<NODE_TYPE>>}
 * @template NODE_TYPE
 */
export class SortableDataGrid extends ViewportDataGrid {
  /**
   * @param {!Parameters} dataGridParameters
   */
  constructor(dataGridParameters) {
    super(dataGridParameters);
    this._sortingFunction = SortableDataGrid.TrivialComparator;
    this.setRootNode(/** @type {!SortableDataGridNode<!NODE_TYPE>} */ (new SortableDataGridNode()));
  }

  /**
   * @param {!SortableDataGridNode<!NODE_TYPE>} a
   * @param {!SortableDataGridNode<!NODE_TYPE>} b
   * @return {number}
   * @template NODE_TYPE
   */
  static TrivialComparator(a, b) {
    return 0;
  }

  /**
   * @param {string} columnId
   * @param {!SortableDataGridNode<!NODE_TYPE>} a
   * @param {!SortableDataGridNode<!NODE_TYPE>} b
   * @return {number}
   * @template NODE_TYPE
   */
  static NumericComparator(columnId, a, b) {
    const aValue = a.data[columnId];
    const bValue = b.data[columnId];
    const aNumber = Number(aValue instanceof Node ? aValue.textContent : aValue);
    const bNumber = Number(bValue instanceof Node ? bValue.textContent : bValue);
    return aNumber < bNumber ? -1 : (aNumber > bNumber ? 1 : 0);
  }

  /**
   * @param {string} columnId
   * @param {!SortableDataGridNode<!NODE_TYPE>} a
   * @param {!SortableDataGridNode<!NODE_TYPE>} b
   * @return {number}
   * @template NODE_TYPE
   */
  static StringComparator(columnId, a, b) {
    const aValue = a.data[columnId];
    const bValue = b.data[columnId];
    const aString = aValue instanceof Node ? aValue.textContent : String(aValue);
    const bString = bValue instanceof Node ? bValue.textContent : String(bValue);
    if (!aString || !bString) {
      return 0;
    }
    return aString < bString ? -1 : (aString > bString ? 1 : 0);
  }

  /**
   * @param {function(!SortableDataGridNode<!NODE_TYPE>, !SortableDataGridNode<!NODE_TYPE>):number} comparator
   * @param {boolean} reverseMode
   * @param {!SortableDataGridNode<!NODE_TYPE>} a
   * @param {!SortableDataGridNode<!NODE_TYPE>} b
   * @return {number}
   * @template NODE_TYPE
   */
  static Comparator(comparator, reverseMode, a, b) {
    return reverseMode ? comparator(b, a) : comparator(a, b);
  }

  /**
   * @param {!Array.<string>} columnNames
   * @param {!Array.<*>} values
   * @param {string} displayName
   * @return {?SortableDataGrid<!SortableDataGridNode<!NODE_TYPE>>}
   * @template NODE_TYPE
   */
  static create(columnNames, values, displayName) {
    const numColumns = columnNames.length;
    if (!numColumns) {
      return null;
    }

    const columns = /** @type {!Array<!ColumnDescriptor>} */ ([]);
    for (let i = 0; i < columnNames.length; ++i) {
      const id = String(i);
      columns.push(/** @type {!ColumnDescriptor} */ ({id, title: columnNames[i], sortable: true}));
    }

    const nodes = [];
    for (let i = 0; i < values.length / numColumns; ++i) {
      /** @type {!Object<number, *>} */
      const data = {};
      for (let j = 0; j < columnNames.length; ++j) {
        data[j] = values[numColumns * i + j];
      }

      const node = new SortableDataGridNode(data);
      node.selectable = false;
      nodes.push(node);
    }

    const dataGrid = new SortableDataGrid(/** @type {!Parameters} */ ({displayName, columns}));
    const length = nodes.length;
    const rootNode = dataGrid.rootNode();
    for (let i = 0; i < length; ++i) {
      rootNode.appendChild(nodes[i]);
    }

    dataGrid.addEventListener(Events.SortingChanged, sortDataGrid);

    function sortDataGrid() {
      const nodes = dataGrid.rootNode().children;
      const sortColumnId = dataGrid.sortColumnId();
      if (!sortColumnId) {
        return;
      }

      let columnIsNumeric = true;
{'fraction_non_alphanumeric': 0.14141048824593128, 'fraction_numerical': 0.0038577456298975288, 'mean_word_length': 3.438737292669877, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 45, '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': '3505433', 'n_tokens_mistral': 2752, 'n_tokens_neox': 2666, 'n_words': 637}