<commit_before>(function (factory) {
    if (typeof define === 'function' && define.amd) {
        define(factory);
    } else if (typeof exports === 'object') {
        module.exports = factory();
    } else {
        var oldFuzz = window.Fuzz;
        var api = window.Fuzz = factory();
        api.noConflict = function () {
            window.Fuzz = oldFuzz;
            return api;
        };
    }
}(function () {
    // Internal functions

    var prepareCollection = function (collection) {
        var _this = this;
        var newCollection = [];
        var l = collection.length;

        for (var i = 0; i < l; i++) {
            newCollection.push(new Item(collection[i], i));
        }

        return newCollection;
    };

    var indexIsSectionStart = function (string, index) {
        return  index === 0 ||
                (string[index].toUpperCase() === string[index] &&
                    (index === 1 ||
                     string[index - 1].toUpperCase() !== string[index - 1]));
    };

    var prepareItem = function (nameArray) {
        var detailedItem = [];
        var detailedChar;
        var l = nameArray.length;
        for (var i = 0; i < l; i++) {
            detailedChar = {
                index: i,
                c: nameArray[i],
                beginSection: indexIsSectionStart(nameArray, i),
                w: 0
            };

            detailedItem.push(detailedChar);
        }

        return detailedItem;
    };


    // Main obj
    var Fuzz = function (collection, options) {
        options || (options = {});

        this.main = collection;
        this.collection = prepareCollection.call(this, this.main);

        return this;
    };

    Fuzz.prototype.match = function (string) {
        var input = string;
        var query = input.replace(/\s+/g, '').toLowerCase();

        var l = this.collection.length;
        var results = [];

        for (var i = 0; i < l; i++) {
            var match = this.collection[i].match(query);

            if (match) results.push(this.collection[i]);
        }

        return results;
    };


    // Item obj
    var Item = function(name, index) {
        this.name = this.parse(name);
        this.nameArray = this.name.split('');
        this.detailed = prepareItem(this.nameArray);

        return this;
    };

    Item.prototype.parse = function(item) {
        return item;
    };

    Item.prototype.clearWeight = function () {
        var l = this.detailed.length;
        for (var i = 0; i < l; i++) {
            this.detailed[i].w = 0;
        }
    };

    Item.prototype.match = function (string) {
        var matchIndex = [];
        var searchIndex = -1;
        var lookUpper = true;
        var testName = this.name;

        for (var i = 0; i < string.length; i++) {
            var currentChar = string.charAt(i);

            if (lookUpper) {
                testName = this.name;
                currentChar = currentChar.toUpperCase();
            }

            if (!lookUpper) testName = testName.toLowerCase();

            if (searchIndex >= this.name.length) return false;

            searchIndex = testName.indexOf(currentChar, searchIndex + 1);

            if (searchIndex > -1) {
                matchIndex.push(searchIndex);
            } else {
                if (lookUpper === false) {
                    matchIndex = [];
                    break;
                }

                if (lookUpper === true) {
                    i -= 1;
                    searchIndex = matchIndex[matchIndex.length -1] || -1;
                }

                lookUpper = !lookUpper;
            }
        }

        this.matched = matchIndex.length ? matchIndex : false;

        return !!(this.matched.length);
    };

    return Fuzz;
}));<commit_msg>Starts conversion to ES6<commit_after>class Item {
  constructor(name, index) {
    this.name = name;
    this.mainIndex = index;
    this.weight = 0;
    this.nameArray = this.name.split('');
    this.detailedArray = this._prepareItem();
  }

  _prepareItem() {
    function indexIsSectionStart(string, index) {
      return index === 0 ||
        (string[index].toUpperCase() === string[index] &&
          (index === 1 ||
            string[index - 1].toUpperCase() !== string[index - 1]));
    }

    return this.nameArray.map((char, index) => {
      return {
        index, char,
        beginSection: indexIsSectionStart(this.nameArray, index),
        weight: 0
      }
    });
  }

  calcMatch(string) {
    var matchIndex = [];
    var searchIndex = -1;
    var lookUpper = true;
    var testName = this.name;
    var lowerTestName = this.name.toLowerCase();

    for (var i = 0; i < string.length; i++) {
        var currentChar = string.charAt(i);

        if (lookUpper) {
            testName = this.name;
            currentChar = currentChar.toUpperCase();
        }

        if (!lookUpper) testName = lowerTestName;

        if (searchIndex >= this.name.length) return false;

        searchIndex = testName.indexOf(currentChar, searchIndex + 1);

        if (searchIndex > -1) {
            matchIndex.push(searchIndex);
        } else {
            if (lookUpper === false) {
                matchIndex = [];
                break;
            }

            if (lookUpper === true) {
                i -= 1;
                searchIndex = matchIndex[matchIndex.length -1] || -1;
            }

            lookUpper = !lookUpper;
        }
    }

    this.matched = matchIndex.length ? matchIndex : false;

    // Calculate rank
    this.calculateWeight();

    return !!(this.matched.length);
  }

  calculateWeight() {
    let l = this.matched.length;
    let index;
    let substringSize = 0;

    for (let i = 0; i < l; i++) {
        index = this.matched[i];

        if (this.set[index].beginSection === true) {
            this.detailedArray[index].weight = (80 - index);
            this.weight += this.detailedArray[index].weight;

            if (this.matched[index - 1] == index - 1) {
                substringSize++;
                this.detailedArray[index].weight += 15 * Math.pow(2, substringSize) - index;
                this.weight += this.detailedArray[index].weight;
            }
        } else if (this.matched[index - 1] == index - 1) {
            substringSize++;
            this.detailedArray[index].weight = 15 * Math.pow(2, substringSize) - index;
            this.weight += this.detailedArray[index].weight;
        } else {
            this.detailedArray[index].weight = 10 - index;
            this.weight += this.detailedArray[index].weight;
        }

        this.weight -= ((this.length - 1) - this.matched[this.matched.length - 1]);
    }

    return this;
  }
}

class Fuzz extends Array {
  constructor(collection, options = {}) {
    super();
    this.main = collection;
    this.push.apply(this, this._prepareCollection());
  }

  parse(item) {
    return item;
  }

  _prepareCollection() {
    return this.main.map((item, i) => {
      return new Item(this.parse(item), i);
    });
  }

  match(string) {
    let query = string.replace(/\s+/g, '').toLowerCase();
    let results = this.filter(item => item.calcMatch(query));
    return results.sort((a, b) => {
      if (a.weight > b.weight)
        return -1;
      if (a.weight < b.weight)
        return 1;
      return 0;
    });
  }
}

export default Fuzz;
{'fraction_non_alphanumeric': 0.1341479972844535, 'fraction_numerical': 0.007060420909708078, 'mean_word_length': 1.6883211678832117, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, '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': '2284422', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2084, 'n_words': 485}