Advertisement
Underworld1337

Untitled

Sep 8th, 2015
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Packet = {
  2.  
  3.   compress:function(patch) {
  4.     var op_keys = {'add':'0','remove':'1','replace':'2','move':'3','copy':'4','test':'5','_get':'6'};
  5.     var compressed = {};
  6.     for (var p = 0, len = patch.length, val; p < len; p++) {
  7.       if(typeof compressed[patch[p].path] === "undefined") {
  8.         compressed[patch[p].path] = [];
  9.       }
  10.       val = isFloat(patch[p].value) ? parseFloat(patch[p].value.toFixed(3)) : patch[p].value;
  11.       compressed[patch[p].path].push([op_keys[patch[p].op],patch[p].value]);
  12.     }
  13.     return compressed;
  14.   },
  15.  
  16.   decompress:function(patch) {
  17.     var op_keys = {'0':'add','1':'remove','2':'replace','3':'move','4':'copy','5':'test','6':'_get'};
  18.     var decompressed = [];
  19.     for (var path in patch) {
  20.       var l = patch[path].length;
  21.       while(l--) {
  22.         decompressed.push({"op":op_keys[patch[path][l][0]],"path":path,"value":patch[path][l][1]});  
  23.       }
  24.     }
  25.     return decompressed;
  26.   },
  27.  
  28. }
  29.  
  30. module.exports = Packet;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement