Skip to content

Instantly share code, notes, and snippets.

@rraallvv
Created May 25, 2018 21:39
Show Gist options
  • Save rraallvv/c4a00fb16da7dc9c98904d2791e91a9f to your computer and use it in GitHub Desktop.
Save rraallvv/c4a00fb16da7dc9c98904d2791e91a9f to your computer and use it in GitHub Desktop.
JSON compression
var lzma = require('lzma');
var data = {
'address': 'XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX XXXX',
'amount': 100,
'message': 'This is the long description that will be shown to the user once the user opens the requested transaction.\nThis is the long description that will be shown to the user once the user opens the requested transaction.\nThis is the long description that will be shown to the user once the user opens the requested transaction.\nThis is the long description that will be shown to the user once the user opens the requested transaction.',
'data': 'Data in the extended transaction',
'flags': 15
};
var Base64 = {
_keyStr: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789;:=',
encode: function(e) {
var t = '';
var n, r, i, s, o, u, a;
var f = 0;
while (f < e.length) {
n = e[f++] & 0xFF;
r = e[f++] & 0xFF;
i = e[f++] & 0xFF;
s = n >> 2;
o = (n & 3) << 4 | r >> 4;
u = (r & 15) << 2 | i >> 6;
a = i & 63;
if (isNaN(r)) {
u = a = 64
} else if (isNaN(i)) {
a = 64
}
t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a)
}
return t;
},
decode: function(e) {
var t = [];
var n, r, i;
var s, o, u, a;
var f = 0;
e = e.replace(/[^A-Za-z0-9;:=]/g, ''); // should remove those characters not included in _keyStr
while (f < e.length) {
s = this._keyStr.indexOf(e.charAt(f++));
o = this._keyStr.indexOf(e.charAt(f++));
u = this._keyStr.indexOf(e.charAt(f++));
a = this._keyStr.indexOf(e.charAt(f++));
n = s << 2 | o >> 4;
r = (o & 15) << 4 | u >> 2;
i = (u & 3) << 6 | a;
t.push(n);
if (u != 64) {
t.push(r)
}
if (a != 64) {
t.push(i)
}
}
return t
}
}
var string = JSON.stringify(data);
console.log(string);
console.log(string.length);
array = lzma.compress(string);
console.log(array.length);
var pack = Base64.encode(array);
console.log(pack);
console.log(pack.length);
var resultArray = Base64.decode(pack);
console.log(resultArray.length);
var result = lzma.decompress(resultArray);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment