Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Packet = {
- compress:function(patch) {
- var op_keys = {'add':'0','remove':'1','replace':'2','move':'3','copy':'4','test':'5','_get':'6'};
- var compressed = {};
- for (var p = 0, len = patch.length, val; p < len; p++) {
- if(typeof compressed[patch[p].path] === "undefined") {
- compressed[patch[p].path] = [];
- }
- val = isFloat(patch[p].value) ? parseFloat(patch[p].value.toFixed(3)) : patch[p].value;
- compressed[patch[p].path].push([op_keys[patch[p].op],patch[p].value]);
- }
- return compressed;
- },
- decompress:function(patch) {
- var op_keys = {'0':'add','1':'remove','2':'replace','3':'move','4':'copy','5':'test','6':'_get'};
- var decompressed = [];
- for (var path in patch) {
- var l = patch[path].length;
- while(l--) {
- decompressed.push({"op":op_keys[patch[path][l][0]],"path":path,"value":patch[path][l][1]});
- }
- }
- return decompressed;
- },
- }
- module.exports = Packet;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement