<commit_before>(function(){

// http://jsfiddle.net/k5Ywe/31/
function merge(o1, o2) {
	for (var p in o2) {
		o1[p] = (
			o1[p] != null
			&& o2[p] != null
			&& !Array.isArray(o1[p])
			&& !Array.isArray(o2[p])
			&& typeof o1[p] == 'object'
			&& typeof o2[p] == 'object'
		) ? merge(o1[p], o2[p]) : o2[p];
	}
	return o1;
}

// from https://github.com/csuwldcat/mootools-core/blob/2b9703c3bbd467020a39e56e383e08033ba310d9/Source/Types/Object.js#L23
function get(object, path){
	if (typeof path == 'string') path = path.split('.');
	for (var i = 0, l = path.length; i < l; i++){
		if (hasOwnProperty.call(object, path[i])) object = object[path[i]];
		else return object[path[i]];
	}
	return object;
}

function set(object, path, value){
	path = (typeof path == 'string') ? path.split('.') : path.slice(0);
	var key = path.pop(),
		len = path.length,
		i = 0,
		current;
	while (len--){
		current = path[i++];
		object = current in object ? object[current] : (object[current] = {});
	}
	object[key] = value;
	return object;
}

exports.State = {

	register: function(server){
		server.$state = {};
	},

	onClientConnect: function(server, conn){
		conn.on('post', this.onPost.bind(this, server, conn));
		conn.on('put', this.onPut.bind(this, server, conn));
		conn.on('delete', this.onDelete.bind(this, server, conn));
		conn.emit('initial state', server.$state);
	},

	onPost: function(server, conn, key, value){
		if (key == null || value == null){
			return conn.emit('error', 'post error', key);
		}
		if (typeof key == 'string') server.$state[key] = value;
		else set(server.$state, key, value);

		server.send('post', key, value);
	},

	onPut: function(server, conn, data){
		merge(server.$state, data);
		server.send('put', data);
	},

	onDelete: function(server, conn, key){
		if (server.$state[key] == null){
			return conn.emit('error', 'delete error', key);
		}
		delete server.$state[key];
		server.send('delete', key);
	}

};

})();<commit_msg>cleanup<commit_after>exports.State = {

	register: function(server){
		server.$state = {};
	},

	onClientConnect: function(server, conn){
		conn.on('post', this.onPost.bind(this, server, conn));
		conn.on('put', this.onPut.bind(this, server, conn));
		conn.on('delete', this.onDelete.bind(this, server, conn));
		conn.emit('initial state', server.$state);
	},

	onPost: function(server, conn, key, value){
		if (key == null || value == null){
			return conn.emit('error', 'post error', key);
		}
		if (typeof key == 'string') server.$state[key] = value;
		else this.set(server.$state, key, value);

		server.send('post', key, value);
	},

	onPut: function(server, conn, data){
		this.merge(server.$state, data);
		server.send('put', data);
	},

	onDelete: function(server, conn, key){
		if (server.$state[key] == null){
			return conn.emit('error', 'delete error', key);
		}
		delete server.$state[key];
		server.send('delete', key);
	},

	merge: function(o1, o2) {
		// http://jsfiddle.net/k5Ywe/31/
		for (var p in o2) {
			o1[p] = (
				o1[p] != null
				&& o2[p] != null
				&& !Array.isArray(o1[p])
				&& !Array.isArray(o2[p])
				&& typeof o1[p] == 'object'
				&& typeof o2[p] == 'object'
			) ? this.merge(o1[p], o2[p]) : o2[p];
		}
		return o1;
	},

	// from https://github.com/csuwldcat/mootools-core/blob/2b9703c3bbd467020a39e56e383e08033ba310d9/Source/Types/Object.js#L23

	get: function(object, path){
		if (typeof path == 'string') path = path.split('.');
		for (var i = 0, l = path.length; i < l; i++){
			if (hasOwnProperty.call(object, path[i])) object = object[path[i]];
			else return object[path[i]];
		}
		return object;
	},

	set: function(object, path, value){
		path = (typeof path == 'string') ? path.split('.') : path.slice(0);
		var key = path.pop(),
			len = path.length,
			i = 0,
			current;

		while (len--){
			current = path[i++];
			object = current in object ? object[current] : (object[current] = {});
		}
		object[key] = value;
		return object;
	}

};
{'fraction_non_alphanumeric': 0.20887198986058303, 'fraction_numerical': 0.025348542458808618, 'mean_word_length': 3.783030303030303, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '1524541', 'n_tokens_mistral': 1667, 'n_tokens_neox': 1504, 'n_words': 327}