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 parseMessage = function (msg) {
var orig=msg,
res ={};
var res ={};
// two or more? use a for
['pass','user','channelId','content'].forEach(function(attr){
var len=msg.substring(0,msg.indexOf(':'));
msg=msg.substring(len.length+1);
var prop=msg.substring(0,Number(len));
msg = msg.substring(prop.length);
res[attr]=prop;
var len=msg.slice(0,msg.indexOf(':')),
// taking an offset lets us slice out the prop
// and saves us one string copy
o=len.length+1,
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);
return res;
};