Commits

Philip Pryce committed a63dae3

Made the packet parser and creator faster and leaner

Comments (0)

Files changed (1)

javascript/joules.js

 	 **/
 	function create_packet (data)
 	{
-		var command = data.command || "";
-		var subcommand = data.subcommand || false;
-		var parameters = data.parameters || {};
-		var content = data.content || false;
-		
-		var packet = command + (subcommand ? " " + subcommand : "") + "\n";
-		for (key in parameters)
-		{
-			var value = parameters[key];
-			if (value) { packet += key + "=" + value + "\n"; }
-		}
-		if (content) { packet += "\n" + content; }
-		return $.trim(packet);
+	    var command = data.command || "";
+	    var subcommand = data.subcommand || false;
+	    var parameters = data.parameters || {};
+	    var content = data.content || false;
+	    
+	    var packet = [command + (subcommand ? " " + subcommand : "")];
+	    for (var key in parameters)
+	    {
+	    	var value = parameters[key];
+	    	if (!!value) { packet.push(key + "=" + value); }
+	    }
+	    if (!!content) { packet.push("\n" + content); }
+	    return $.trim(packet.join("\n"));
 	}
+
 	
 	/**
 	 * Parse a raw packet into a Javascript Object
 	    var command = header.shift().split(" ");
 	    var parameters = {};
 	    
-	    $.each(header, function()
+	    for (var i = 0, len = header.length; i < len; i++)
 	    {
-	    	var i = this.split('=');
-	    	if (i[1]) { parameters[i.shift()] = i.join('='); }
-	    });
+	    	var item = header[i].split('=');
+	    	if (!!item[1]) { parameters[item.shift()] = item.join('='); }
+	    }
 	    
 	    return {
 	    	command: command[0],