<commit_before>"use strict";
define(
	['underscore', 'jquery'],
	function (_, $) {

/**
 * @exports Userlist
 */

function Userlist(ul) {
	this.ul = $(ul);
	this._users = [];
}

function _userSort(user) {
	return user.user.nick;
}

Userlist.prototype.add = function(user) {
	/** @todo this looks terribly inefficient… */
	// create dom element
	var li = $('<li>')
		.text(user.nick)
		.css('color', user.color);
	// add new user
	this._users.push({user: user, li: li[0]});
	// rebuild sorted user view
	var users = _.sortBy(this._users, _userSort);
	this.removeAll();
	this._users = users;
	// render
	var self = this;
	_.each(users, function (user) {
		self.ul.append(user.li);
	});
	return li;
};

Userlist.prototype.remove = function(userToRemove) {
	// remove user from internal user-list
	var users = [];
	var lisToRemoved = [];
	_.each(this._users, function (e) {
		if (!userToRemove.equalTo(e.user)) {
			users.push(e);
		} else {
			lisToRemoved.push(e.li);
		}
	});
	this._users = users;
	// remove user from ul
	$(lisToRemoved).remove();
};

Userlist.prototype.removeAll = function() {
	this.ul.empty();
	this._users = [];
};

return Userlist;

});<commit_msg>better handling of multiple connections for a single user (= nick/sig pair)<commit_after>"use strict";
define(
	['underscore', 'jquery'],
	function (_, $) {

/**
 * @exports Userlist
 */

/**
 * Userlist manages a ul, adding to & removing users from the list.
 * @constructor
 * @param {HTMLEntity} ul
 */
function Userlist(ul) {
	/** @public */
	this.ul = $(ul);
	/** @private */
	this._db = [];
}

function _userNick(dbEntry) {
	return dbEntry.user.nick;
}

function _makeUserFilter(user) {
	return function (e) {
		return user.equalTo(e.user);
	};
}

function _updateLiWithEntry(dbEntry) {
	$(dbEntry.li).attr('title', dbEntry.count + ' connection' + (dbEntry.count > 1 ? 's' : ''));
}

/**
 * Add a user.
 * @param {User} user
 */
Userlist.prototype.add = function(user) {
	/** @todo this looks terribly inefficient… */
	// check if the user was already connected
	var dbEntry = _.find(this._db, _makeUserFilter(user));
	if (dbEntry) {
		dbEntry.count++;
		_updateLiWithEntry(dbEntry);
		return $(dbEntry.li);
	}
	// create dom element
	var li = $('<li>')
		.text(user.nick)
		.css('color', user.color);
	// add new user
	this._db.push({user: user, li: li[0], count: 1});
	// rebuild sorted user view
	var db = _.sortBy(this._db, _userNick);
	this.removeAll();
	this._db = db;
	// render
	var self = this;
	_.each(db, function (dbEntry) {
		self.ul.append(dbEntry.li);
		_updateLiWithEntry(dbEntry);
	});
	return li;
};

/**
 * Remove a user.
 * @param {User} user
 */
Userlist.prototype.remove = function(user) {
	var userFilter = _makeUserFilter(user);
	var dbEntry = _.find(this._db, userFilter);
	if (dbEntry && --dbEntry.count === 0) {
		$(dbEntry.li).remove();
		this._db = _.reject(this._db, userFilter);
	} else {
		_updateLiWithEntry(dbEntry);
	}
};

/**
 * Clear the ul & internal db.
 */
Userlist.prototype.removeAll = function() {
	this.ul.empty();
	this._db = [];
};

return Userlist;

});
{'fraction_non_alphanumeric': 0.18400520156046815, 'fraction_numerical': 0.001625487646293888, 'mean_word_length': 3.6480362537764353, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '2736075', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1119, 'n_words': 258}