<commit_before>/**
 * Created by <NAME> (h<EMAIL>) on 8/18/14.
 */
/* global d3, jQuery, _ */
var LineUp;
(function (LineUp, d3, $, _, undefined) {

  function bundleSetter(bundle) {
    return function setBundle(col) {
      col.columnBundle = bundle;
      if (col instanceof LineUp.LayoutStackedColumn) {
        col.children.forEach(setBundle);
      }
    }
  }
  /**
   * An implementation of data storage for reading locally
   * @param tableId
   * @param data
   * @param columns
   * @param config
   * @class
   */
  function LineUpLocalStorage(data, columns, layout, primaryKey, storageConfig) {
    this.storageConfig = $.extend(true, {}, {
      colTypes: {
        'number': LineUp.LineUpNumberColumn,
        'string': LineUp.LineUpStringColumn,
        'categorical': LineUp.LineUpCategoricalColumn
      },
      layoutColumnTypes: {
        'number': LineUp.LayoutNumberColumn,
        'single': LineUp.LayoutStringColumn,
        'string': LineUp.LayoutStringColumn,
        'categorical': LineUp.LayoutCategoricalColumn,
        'stacked': LineUp.LayoutStackedColumn,
        'rank': LineUp.LayoutRankColumn,
        'actions': LineUp.LayoutActionColumn
      }
    }, storageConfig);
    this.config = null; //will be injected by lineup

    var colTypes = this.storageConfig.colTypes;
    var layoutColumnTypes = this.storageConfig.layoutColumnTypes;
    var that = this;

    function toColumn(desc) {
      return new colTypes[desc.type](desc, toColumn, data);
    }

    this.storageConfig.toColumn = toColumn;

    this.primaryKey = primaryKey;
    this.rawdata = data;
    this.data = data;
    this.initialSort = true;
    this.rawcols = columns.map(toColumn);
    this.layout = layout || LineUpLocalStorage.generateDefaultLayout(this.rawcols);

    var colLookup = d3.map();
    this.rawcols.forEach(function (col) {
      colLookup.set(col.column, col);
    });
    function toLayoutColumn(desc) {
      var type = desc.type || 'single';
      if (type === 'single') {
        var col = colLookup.get(desc.column);
        if (col instanceof LineUp.LineUpNumberColumn) {
          type = 'number';
        } else if (col instanceof LineUp.LineUpCategoricalColumn) {
          type = 'categorical';
        }
      }
      return new layoutColumnTypes[type](desc, colLookup, toLayoutColumn, that);
    }

    this.storageConfig.toLayoutColumn = toLayoutColumn;

    this.bundles = {
      "primary": {
        layoutColumns: [],
        needsLayout: true  // this triggers the layout generation at first access to "getColumnLayout"
      }
    };
  }

  LineUp.LineUpLocalStorage = LineUpLocalStorage;
  LineUp.createLocalStorage = function (data, columns, layout, primaryKey, storageConfig) {
    return new LineUpLocalStorage(data, columns, layout, primaryKey, storageConfig);
  };

  /**
   * generate a default layout by just showing all columns with 100 px
   * @param columns
   * @returns {{primary: (Array|*)}}
   */
  LineUpLocalStorage.generateDefaultLayout = function (columns) {
    var layout = columns.map(function (c) {
      return {
        column: c.column,
        width: c instanceof LineUp.LineUpStringColumn ? 200 : 100
      };
    });
    return {
      primary: layout
    };
  };

  LineUpLocalStorage.prototype = $.extend({}, {},
    /** @lends LineUpLocalStorage.prototype */
    {
      getRawColumns: function () {
        return this.rawcols;
      },
      getColumnLayout: function (key) {
        var _key = key || "primary";
        if (this.bundles[_key].needsLayout) {
          this.generateLayout(this.layout, _key);
          this.bundles[_key].needsLayout = false;
        }

        return this.bundles[_key].layoutColumns;
      },

      /**
       *  get the data
       *  @returns data
       */
      getData: function () {
        return this.data;
      },
      filterData: function (columns) {
        columns = columns || this.bundles["primary"].layoutColumns;

        var flat = [];
        columns.forEach(function (d) {
          d.flattenMe(flat);
        });
        flat = flat.filter(function (d) {
          return d.isFiltered();
        });
        if ($.isFunction(this.config.filter.filter)) {
          flat.push(this.config.filter.filter);
        }
        if (flat.length === 0) {
          this.data = this.rawdata;
        } else {
          this.data = this.rawdata.filter(function (row) {
            return flat.every(function (f) {
              return f.filterBy(row);
            });
          });
        }
      },
      resortData: function (spec) {

        var _key = spec.key || "primary", that = this;
        var bundle = this.bundles[_key];
        var asc = spec.asc || this.config.columnBundles[_key].sortingOrderAsc;
        var column = spec.column || this.config.columnBundles[_key].sortedColumn;

        //console.log("resort: ", spec);
        this.filterData(bundle.layoutColumns);
        if (spec.filteredChanged || this.initialSort) {
          //trigger column updates
          var flat = [];
          bundle.layoutColumns.forEach(function (d) {
            d.flattenMe(flat);
          });
          flat.forEach(function (col) {
            col.prepare(that.data, that.config.renderingOptions.histograms);
          });
          this.initialSort = false;
        }

        function sort(a,b) {
          var r = column.sortBy(a,b);
          return asc ? -r : r;
        }
        if (column) {
          this.data.sort(sort);
        }

        var start = this.config.filter.skip ? this.config.filter.skip : 0;
        if ((this.config.filter.limit && isFinite(this.config.filter.limit))) {<commit_msg>sort by primary key if the real sorting criteria is equal<commit_after>/**
 * Created by <NAME> (h<EMAIL>) on 8/18/14.
 */
/* global d3, jQuery, _ */
var LineUp;
(function (LineUp, d3, $, _, undefined) {

  function bundleSetter(bundle) {
    return function setBundle(col) {
      col.columnBundle = bundle;
      if (col instanceof LineUp.LayoutStackedColumn) {
        col.children.forEach(setBundle);
      }
    }
  }
  /**
   * An implementation of data storage for reading locally
   * @param tableId
   * @param data
   * @param columns
   * @param config
   * @class
   */
  function LineUpLocalStorage(data, columns, layout, primaryKey, storageConfig) {
    this.storageConfig = $.extend(true, {}, {
      colTypes: {
        'number': LineUp.LineUpNumberColumn,
        'string': LineUp.LineUpStringColumn,
        'categorical': LineUp.LineUpCategoricalColumn
      },
      layoutColumnTypes: {
        'number': LineUp.LayoutNumberColumn,
        'single': LineUp.LayoutStringColumn,
        'string': LineUp.LayoutStringColumn,
        'categorical': LineUp.LayoutCategoricalColumn,
        'stacked': LineUp.LayoutStackedColumn,
        'rank': LineUp.LayoutRankColumn,
        'actions': LineUp.LayoutActionColumn
      }
    }, storageConfig);
    this.config = null; //will be injected by lineup

    var colTypes = this.storageConfig.colTypes;
    var layoutColumnTypes = this.storageConfig.layoutColumnTypes;
    var that = this;

    function toColumn(desc) {
      return new colTypes[desc.type](desc, toColumn, data);
    }

    this.storageConfig.toColumn = toColumn;

    this.primaryKey = primaryKey;
    this.rawdata = data;
    this.data = data;
    this.initialSort = true;
    this.rawcols = columns.map(toColumn);
    this.layout = layout || LineUpLocalStorage.generateDefaultLayout(this.rawcols);

    var colLookup = d3.map();
    this.rawcols.forEach(function (col) {
      colLookup.set(col.column, col);
    });
    function toLayoutColumn(desc) {
      var type = desc.type || 'single';
      if (type === 'single') {
        var col = colLookup.get(desc.column);
        if (col instanceof LineUp.LineUpNumberColumn) {
          type = 'number';
        } else if (col instanceof LineUp.LineUpCategoricalColumn) {
          type = 'categorical';
        }
      }
      return new layoutColumnTypes[type](desc, colLookup, toLayoutColumn, that);
    }

    this.storageConfig.toLayoutColumn = toLayoutColumn;

    this.bundles = {
      "primary": {
        layoutColumns: [],
        needsLayout: true  // this triggers the layout generation at first access to "getColumnLayout"
      }
    };
  }

  LineUp.LineUpLocalStorage = LineUpLocalStorage;
  LineUp.createLocalStorage = function (data, columns, layout, primaryKey, storageConfig) {
    return new LineUpLocalStorage(data, columns, layout, primaryKey, storageConfig);
  };

  /**
   * generate a default layout by just showing all columns with 100 px
   * @param columns
   * @returns {{primary: (Array|*)}}
   */
  LineUpLocalStorage.generateDefaultLayout = function (columns) {
    var layout = columns.map(function (c) {
      return {
        column: c.column,
        width: c instanceof LineUp.LineUpStringColumn ? 200 : 100
      };
    });
    return {
      primary: layout
    };
  };

  LineUpLocalStorage.prototype = $.extend({}, {},
    /** @lends LineUpLocalStorage.prototype */
    {
      getRawColumns: function () {
        return this.rawcols;
      },
      getColumnLayout: function (key) {
        var _key = key || "primary";
        if (this.bundles[_key].needsLayout) {
          this.generateLayout(this.layout, _key);
          this.bundles[_key].needsLayout = false;
        }

        return this.bundles[_key].layoutColumns;
      },

      /**
       *  get the data
       *  @returns data
       */
      getData: function () {
        return this.data;
      },
      filterData: function (columns) {
        columns = columns || this.bundles["primary"].layoutColumns;

        var flat = [];
        columns.forEach(function (d) {
          d.flattenMe(flat);
        });
        flat = flat.filter(function (d) {
          return d.isFiltered();
        });
        if ($.isFunction(this.config.filter.filter)) {
          flat.push(this.config.filter.filter);
        }
        if (flat.length === 0) {
          this.data = this.rawdata;
        } else {
          this.data = this.rawdata.filter(function (row) {
            return flat.every(function (f) {
              return f.filterBy(row);
            });
          });
        }
      },
      resortData: function (spec) {

        var _key = spec.key || "primary", that = this;
        var bundle = this.bundles[_key];
        var asc = spec.asc || this.config.columnBundles[_key].sortingOrderAsc;
        var column = spec.column || this.config.columnBundles[_key].sortedColumn;

        //console.log("resort: ", spec);
        this.filterData(bundle.layoutColumns);
        if (spec.filteredChanged || this.initialSort) {
          //trigger column updates
          var flat = [];
          bundle.layoutColumns.forEach(function (d) {
            d.flattenMe(flat);
          });
          flat.forEach(function (col) {
            col.prepare(that.data, that.config.renderingOptions.histograms);
          });
          this.initialSort = false;
        }
        var primary = this.primaryKey;
        function sort(a,b) {
          var r = column.sortBy(a,b);
          if (r === 0 || isNaN(r)) { //by default sort by primary key
            return d3.ascending(a[primary], b[primary]);
          }
          return asc ? -r : r;
        }
        if (column) {
          this.data.sort(sort);
        }

        var start = this.config.filter.skip ? this.config.filter.skip : 0;
        if ((this.config.filter.limit && isFinite(this.config.filter.limit))) {
{'fraction_non_alphanumeric': 0.12390226936788105, 'fraction_numerical': 0.003477958438396661, 'mean_word_length': 2.2611284377658065, 'pattern_counts': {'":': 2, '<': 7, '<?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': '7422647', 'n_tokens_mistral': 3403, 'n_tokens_neox': 3327, 'n_words': 718}