<commit_before>var codebrag = codebrag || {};

/**
 *
 * Collection of object backed by async operations
 * Maintains immutable reference to collection of elements
 * Loading and removing elements is based on promises.
 *
 * Used to commits and followups to keep server state in sync with local view
 *
 */
codebrag.AsyncCollection = function() {

    // initialize with empty elements list
    this.elements = [];

};

codebrag.AsyncCollection.prototype = {

    loadElements: function (promise) {
        var self = this;
        return promise.then(function(receivedCollection) {
            self._replaceElementsWith(receivedCollection);
            return self.elements;
        })
    },

    _replaceElementsWith: function(source) {
        var self = this;
        self.elements.length = 0;
        _.forEach(source, function(el) {
            self.elements.push(el);
        })
    },

    removeElement: function(matchFn, promise) {
        var self = this;
        var found = _.find(self.elements, function(current) {
            return matchFn(current);
        });
        var indexToRemove = self.elements.indexOf(found);
        return promise.then(function() {
            self.elements.splice(indexToRemove, 1);
            return indexToRemove;
        });
    },

    removeElementAndGetNext: function(matchFn, promise) {
        var self = this;
        return self.removeElement(matchFn, promise).then(function(indexRemoved) {
            if(_.isEmpty(self.elements)) {
                return null;
            }
            if(indexRemoved === 0) {
                return self.elements[0];
            }
            return self.elements[indexRemoved - 1];
        });
    }
};<commit_msg>Refactor some internals in AsyncCollection<commit_after>var codebrag = codebrag || {};

/**
 *
 * Collection of object backed by async operations
 * Maintains immutable reference to collection of elements
 * Loading and removing elements is based on promises.
 *
 * Used to commits and followups to keep server state in sync with local view
 *
 */
codebrag.AsyncCollection = function() {

    // initialize with empty elements list
    this.elements = [];

};

codebrag.AsyncCollection.prototype = {

    loadElements: function (promise) {
        var self = this;
        return promise.then(function(receivedCollection) {
            self._replaceElementsWith(receivedCollection);
            return self.elements;
        })
    },

    _replaceElementsWith: function(source) {
        var self = this;
        self.elements.length = 0;
        _.forEach(source, function(el) {
            self.elements.push(el);
        })
    },

    removeElement: function(matchFn, promise) {
        var self = this;
        var indexToRemove = self._indexOf(matchFn);
        return promise.then(function() {
            self.elements.splice(indexToRemove, 1);
            return indexToRemove;
        });
    },

    removeElementAndGetNext: function(matchFn, promise) {
        var self = this;
        return self.removeElement(matchFn, promise).then(self._getNext.bind(self));
    },

    _indexOf: function(matchFn) {
        var self = this;
        var found = _.find(self.elements, function(current) {
            return matchFn(current);
        });
        return self.elements.indexOf(found);
    },

    _getNext: function (index) {
        var self = this;
        if (_.isEmpty(self.elements)) {
            return null;
        }
        if (index === 0) {
            return self.elements[0];
        }
        return self.elements[index - 1];
    }
};
{'fraction_non_alphanumeric': 0.10467394346487545, 'fraction_numerical': 0.00279876854184159, 'mean_word_length': 2.2169216921692168, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6718901', 'n_tokens_mistral': 967, 'n_tokens_neox': 936, 'n_words': 237}