Breaking the loop into a one more readable:
- rearranged loop parameters
- changed
(...)&&(...) with an if(...){(...)} - changed
l to len - moved
s = s.split(...) outside the len
.
var a, b, s = s.split(""), o = String.fromCharCode, c = "charCodeAt"; for(var i = -1, len = s.length; ++i < len;){ if((a = s[i][c](0)) & 0x80){ (s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ? o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = ""); } }
- changed
i initial value and how/where it increases - moved
a = s[i][c](0) outside
.
var a, b, s = s.split(""), o = String.fromCharCode, c = "charCodeAt"; for(var i = 0, len = s.length; i < len; i++){ a = s[i][c](0); if(a & 0x80){ s[i] = (a & 0xfc); (s[i] == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ? o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = ""); } }
- created
tmp to make things easier to read - stored the ternary operation result in
tmp - splitted
(s[i] == 0xc0 && tmp, s[++i] = ""); with an if(...){s[++i] = "";} - replaced the new loop inside the your example
.
decode: function(s){ var tmp, a, b, s = s.split(""), o = String.fromCharCode, c = "charCodeAt"; for(var i = 0, len = s.length; i < len; i++){ a = s[i][c](0); if(a & 0x80){ s[i] = (a & 0xfc); if(((b = s[i + 1][c](0)) & 0xc0) == 0x80){ tmp = o(((a & 0x03) << 6) + (b & 0x3f)); }else{ tmp = o(128); } if(s[i] == 0xc0 && tmp){ s[++i] = ""; } } } return s.join(""); }
Final result /\