<commit_before><!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->

<script>
  window.Vaadin = window.Vaadin || {};
  window.Vaadin.Grid = window.Vaadin.Grid || {};

  /**
   * @polymerMixin
   */
  Vaadin.Grid.ArrayDataProviderMixin = superClass => class ArrayDataProviderMixin extends superClass {

    static get properties() {

      return {
        /**
         * An array containing the items which will be stamped to the column template
         * instances.
         */
        items: Array

      };
    }

    static get observers() {
      return [
        '_itemsChanged(items, items.*)'
      ];
    }

    _itemsChanged(items, splices) {
      if (!Array.isArray(items)) {
        if (items === undefined || items === null) {
          this.size = 0;
        }
        if (this.dataProvider === this._arrayDataProvider) {
          this.dataProvider = undefined;
        }
        return;
      }

      this.size = items.length;
      this.dataProvider = this.dataProvider || this._arrayDataProvider;
      this.clearCache();
    }

    _arrayDataProvider(opts, cb) {
      let items = (Array.isArray(this.items) ? this.items : []).slice(0);

      if (this._filters && this._checkPaths(this._filters, 'filtering', items)) {
        items = this._filter(items);
      }

      this.size = items.length;

      if (opts.sortOrders.length && this._checkPaths(this._sorters, 'sorting', items)) {
        items = items.sort(this._multiSort.bind(this));
      }

      const start = opts.page * opts.pageSize;
      const end = start + opts.pageSize;
      const slice = items.slice(start, end);
      cb(slice, items.length);
    }

    /**
     * Check array of filters/sorters for paths validity, console.warn invalid items
     * @param {Array}  arrayToCheck The array of filters/sorters to check
     * @param {string} action       The name of action to include in warning (filtering, sorting)
     * @param {Array}  items
     */
    _checkPaths(arrayToCheck, action, items) {
      if (!items.length) {
        return false;
      }

      let result = true;

      for (var i in arrayToCheck) {
        const path = arrayToCheck[i].path;

        // skip simple paths
        if (!path || path.indexOf('.') === -1) {
          continue;
        }

        const parentProperty = path.replace(/\.[^\.]*$/, ''); // a.b.c -> a.b
        if (Polymer.Base.get(parentProperty, items[0]) === undefined) {
          console.warn(`Path "${path}" used for ${action} does not exist in all of the items, ${action} is disabled.`);
          result = false;
        }
      }

      return result;
    }

    _multiSort(a, b) {
      return this._sorters.map(sort => {
        if (sort.direction === 'asc') {
          return this._compare(Polymer.Base.get(sort.path, a), Polymer.Base.get(sort.path, b));
        } else if (sort.direction === 'desc') {
          return this._compare(Polymer.Base.get(sort.path, b), Polymer.Base.get(sort.path, a));
        }
        return 0;
      }).reduce((p, n) => {
        return p ? p : n;
      }, 0);
    }

    _normalizeEmptyValue(value) {
      if ([undefined, null].indexOf(value) >= 0) {
        return '';
      } else if (isNaN(value)) {
        return value.toString();
      } else {
        return value;
      }
    }

    _compare(a, b) {
      a = this._normalizeEmptyValue(a);
      b = this._normalizeEmptyValue(b);

      if (a < b) {
        return -1;
      }
      if (a > b) {
        return 1;
      }
      return 0;
    }

    _filter(items) {
      return items.filter((item, index) => {
        return this._filters.filter(filter => {
          const value = this._normalizeEmptyValue(Polymer.Base.get(filter.path, item));
          return value.toString().toLowerCase().indexOf(filter.value.toString().toLowerCase()) === -1;
        }).length === 0;
      });
    }

  };
</script>
<commit_msg>Don't initialize items array data source until attached<commit_after><!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->

<script>
  window.Vaadin = window.Vaadin || {};
  window.Vaadin.Grid = window.Vaadin.Grid || {};

  /**
   * @polymerMixin
   */
  Vaadin.Grid.ArrayDataProviderMixin = superClass => class ArrayDataProviderMixin extends superClass {

    static get properties() {

      return {
        /**
         * An array containing the items which will be stamped to the column template
         * instances.
         */
        items: Array

      };
    }

    static get observers() {
      return [
        '_itemsChanged(items, items.*, isAttached)'
      ];
    }

    _itemsChanged(items, splices, isAttached) {
      if (!isAttached) {
        return;
      }
      if (!Array.isArray(items)) {
        if (items === undefined || items === null) {
          this.size = 0;
        }
        if (this.dataProvider === this._arrayDataProvider) {
          this.dataProvider = undefined;
        }
        return;
      }

      this.size = items.length;
      this.dataProvider = this.dataProvider || this._arrayDataProvider;
      this.clearCache();
    }

    _arrayDataProvider(opts, cb) {
      let items = (Array.isArray(this.items) ? this.items : []).slice(0);

      if (this._filters && this._checkPaths(this._filters, 'filtering', items)) {
        items = this._filter(items);
      }

      this.size = items.length;

      if (opts.sortOrders.length && this._checkPaths(this._sorters, 'sorting', items)) {
        items = items.sort(this._multiSort.bind(this));
      }

      const start = opts.page * opts.pageSize;
      const end = start + opts.pageSize;
      const slice = items.slice(start, end);
      cb(slice, items.length);
    }

    /**
     * Check array of filters/sorters for paths validity, console.warn invalid items
     * @param {Array}  arrayToCheck The array of filters/sorters to check
     * @param {string} action       The name of action to include in warning (filtering, sorting)
     * @param {Array}  items
     */
    _checkPaths(arrayToCheck, action, items) {
      if (!items.length) {
        return false;
      }

      let result = true;

      for (var i in arrayToCheck) {
        const path = arrayToCheck[i].path;

        // skip simple paths
        if (!path || path.indexOf('.') === -1) {
          continue;
        }

        const parentProperty = path.replace(/\.[^\.]*$/, ''); // a.b.c -> a.b
        if (Polymer.Base.get(parentProperty, items[0]) === undefined) {
          console.warn(`Path "${path}" used for ${action} does not exist in all of the items, ${action} is disabled.`);
          result = false;
        }
      }

      return result;
    }

    _multiSort(a, b) {
      return this._sorters.map(sort => {
        if (sort.direction === 'asc') {
          return this._compare(Polymer.Base.get(sort.path, a), Polymer.Base.get(sort.path, b));
        } else if (sort.direction === 'desc') {
          return this._compare(Polymer.Base.get(sort.path, b), Polymer.Base.get(sort.path, a));
        }
        return 0;
      }).reduce((p, n) => {
        return p ? p : n;
      }, 0);
    }

    _normalizeEmptyValue(value) {
      if ([undefined, null].indexOf(value) >= 0) {
        return '';
      } else if (isNaN(value)) {
        return value.toString();
      } else {
        return value;
      }
    }

    _compare(a, b) {
      a = this._normalizeEmptyValue(a);
      b = this._normalizeEmptyValue(b);

      if (a < b) {
        return -1;
      }
      if (a > b) {
        return 1;
      }
      return 0;
    }

    _filter(items) {
      return items.filter((item, index) => {
        return this._filters.filter(filter => {
          const value = this._normalizeEmptyValue(Polymer.Base.get(filter.path, item));
          return value.toString().toLowerCase().indexOf(filter.value.toString().toLowerCase()) === -1;
        }).length === 0;
      });
    }

  };
</script>

{'fraction_non_alphanumeric': 0.1430346713060768, 'fraction_numerical': 0.00447371691313533, 'mean_word_length': 2.0565894417014814, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, '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': '3859157', 'n_tokens_mistral': 2497, 'n_tokens_neox': 2400, 'n_words': 582}