<commit_before>function keys(obj) {
    var result = [];
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            result.push(key);
        }
    }
    return result;
}

function Model(twitterScreenName)
{
	this.twitter = new Twitter(twitterScreenName);
    this.items = [];
    this.itemsLoaded = function(){};
    this.itemRemoved = function(item){};
}

Model.prototype.load = function(twitterScreenName)
{
    var model = this;
    this.twitter.getUserTimeline(twitterScreenName, function(data)
    {
        model._parseTweets(data);
    });
};

Model.prototype._parseTweets = function(data)
{
    var relevant = [];
    $.each(data, function(index, item)
    {
        var text = item.text.replace(/\s+/g, ' ');
        var matches = /OFF: *(.*) ON: *(.*)/.exec(text);
        if (matches !== null)
        {
            relevant.push({off:matches[1],on:matches[2],date:item.created_at});
        }
    });
    relevant.reverse(); // chronological order please

    var itemSet = {};
    relevant.forEach(function(r)
    {
        delete itemSet[r.off];
        itemSet[r.on] = r.date;
    });

    var items = [];
    keys(itemSet).sort().forEach(function(key)
    {
        items.push({name:key, date:new Date(itemSet[key])});
    });

    this.items = items;
    this.itemsLoaded();
};

Model.prototype.add = function(name, tweet)
{
    var i = 0;
    if (tweet) this.twitter.tweet("ON: " + name);
    while (i < this.items.length && this.items[i].name < name) ++i;
    this.items.splice(i, 0, {name:name, date:new Date()});
    this.itemsLoaded();
};

Model.prototype.remove = function(name)
{
    var index = this.findIndex(name);
    if (index >= 0)
    {
        var item = this.items[index];
        this.twitter.tweet("OFF: " + name);
        this.items.splice(index,1);
        this.itemRemoved(item);
    }
};

Model.prototype.change = function(oldname, newname)
{
    if (oldname != newname)
    {
        var index = this.findIndex(oldname);
        if (index >= 0)
        {
            this.twitter.tweet("OFF: " + oldname + "\nON: " + newname);
            this.items.splice(index,1);
            this.add(newname, false);
        }
    }
};

Model.prototype.findIndex = function(name)
{
    var i = 0;
    while (i < this.items.length && this.items[i].name != name) ++i;
    if (i == this.items.length) i = -1;
    return i;
};
<commit_msg>Don't add items with duplicate names<commit_after>function keys(obj) {
    var result = [];
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            result.push(key);
        }
    }
    return result;
}

function Model(twitterScreenName)
{
	this.twitter = new Twitter(twitterScreenName);
    this.items = [];
    this.itemsLoaded = function(){};
    this.itemRemoved = function(item){};
}

Model.prototype.load = function(twitterScreenName)
{
    var model = this;
    this.twitter.getUserTimeline(twitterScreenName, function(data)
    {
        model._parseTweets(data);
    });
};

Model.prototype._parseTweets = function(data)
{
    var relevant = [];
    $.each(data, function(index, item)
    {
        var text = item.text.replace(/\s+/g, ' ');
        var matches = /OFF: *(.*) ON: *(.*)/.exec(text);
        if (matches !== null)
        {
            relevant.push({off:matches[1],on:matches[2],date:item.created_at});
        }
    });
    relevant.reverse(); // chronological order please

    var itemSet = {};
    relevant.forEach(function(r)
    {
        delete itemSet[r.off];
        itemSet[r.on] = r.date;
    });

    var items = [];
    keys(itemSet).sort().forEach(function(key)
    {
        items.push({name:key, date:new Date(itemSet[key])});
    });

    this.items = items;
    this.itemsLoaded();
};

Model.prototype.add = function(name, tweet)
{
    var index = this.findIndex(name);
    if (index < 0)
    {
        var i = 0;
        if (tweet) this.twitter.tweet("ON: " + name);
        while (i < this.items.length && this.items[i].name < name) ++i;
        this.items.splice(i, 0, {name:name, date:new Date()});
        this.itemsLoaded();
    }
};

Model.prototype.remove = function(name)
{
    var index = this.findIndex(name);
    if (index >= 0)
    {
        var item = this.items[index];
        this.twitter.tweet("OFF: " + name);
        this.items.splice(index,1);
        this.itemRemoved(item);
    }
};

Model.prototype.change = function(oldname, newname)
{
    if (oldname != newname)
    {
        var index = this.findIndex(oldname);
        if (index >= 0)
        {
            this.twitter.tweet("OFF: " + oldname + "\nON: " + newname);
            this.items.splice(index,1);
            this.add(newname, false);
        }
    }
};

Model.prototype.findIndex = function(name)
{
    var i = 0;
    while (i < this.items.length && this.items[i].name != name) ++i;
    if (i == this.items.length) i = -1;
    return i;
};

{'fraction_non_alphanumeric': 0.16953669536695368, 'fraction_numerical': 0.004305043050430504, 'mean_word_length': 2.34866163349348, 'pattern_counts': {'":': 0, '<': 10, '<?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': '6162996', 'n_tokens_mistral': 1657, 'n_tokens_neox': 1640, 'n_words': 301}