ChainPadSrv.js : come back and clean up parseMessage just a little bit

This commit is contained in:
ansuz 2015-10-26 22:11:20 -04:00
parent 7c2d9e061f
commit db2fcda655
1 changed files with 9 additions and 7 deletions

View File

@ -24,16 +24,18 @@ var PING = 4;
var PONG = 5; var PONG = 5;
var parseMessage = function (msg) { var parseMessage = function (msg) {
var orig=msg, var res ={};
res ={};
// two or more? use a for // two or more? use a for
['pass','user','channelId','content'].forEach(function(attr){ ['pass','user','channelId','content'].forEach(function(attr){
var len=msg.substring(0,msg.indexOf(':')); var len=msg.slice(0,msg.indexOf(':')),
msg=msg.substring(len.length+1); // taking an offset lets us slice out the prop
var prop=msg.substring(0,Number(len)); // and saves us one string copy
msg = msg.substring(prop.length); o=len.length+1,
res[attr]=prop; prop=res[attr]=msg.slice(o,Number(len)+o);
// slice off the property and its descriptor
msg = msg.slice(prop.length+o);
}); });
// content is the only attribute that's not a string
res.content=JSON.parse(res.content); res.content=JSON.parse(res.content);
return res; return res;
}; };