Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created March 25, 2015 19:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/6f1203d70fea16f32f03 to your computer and use it in GitHub Desktop.
Save xeoncross/6f1203d70fea16f32f03 to your computer and use it in GitHub Desktop.
Simple functions to compress/uncompress javascript unicode strings using repeating patterns
// Compress/Uncompress javascript unicode strings using repeating patterns
// Copyright 2015 davidpennington.me
function compress(str) {
var original = str;
// We don't allow newlines
str = str.replace(/(\r\n|\n|\r)/gm,"");
// Quote all digits (we don't want to parse them)
str = str.replace(/(\d)/g, "\\$1");
// Prepare our regex matcher
var regex = RegExp(/(.+)(?=.*\1)/g);
var matches = str.match(regex).sort(function(a, b){
return (str.split(b).length * b.length) - (str.split(a).length * a.length);
});
var dict = [];
for (index = 0; index < matches.length; ++index) {
var word = matches[index];
var len = word.length;
// Only store a dictonary of 10 words 0-9
if(word.length === 1) { continue; }
if(dict.length === 10) { break; }
var parts = str.split(word);
// This word must exist at least twice in the string
if(parts.length < 3) { continue; }
dict.push(word);
str = parts.join(dict.length - 1);
}
str = dict.join("\n") + "\n" + str;
if((str.length + (str.length / 10)) < original.length) {
return str;
}
return original;
}
function uncompress(str) {
var dict = str.split("\n");
if(dict.length === 1) return str;
var str = dict.pop();
str = str.replace(/\d/g, function(v, i) {
if (str[i - 1] !== '\\') {
return dict[v];
}
return v;
});
str = str.replace(/\\(\d)/g, "$1");
return str;
}
var words = [
'_send_T_send_send_ack-new_amend_pending-cancel-replace_replaced_cancel_pending-cancel-replace_replaced',
'the quick brown fox jumped over the lazy dog',
'This property returns the number of code units in the string.',
'Not the result of the voodoo that replace will do that is apparently substituting $1 for the first capture group.',
'What would your life look like if you were totally forgiven, completely justified, and eternally secure? Not a trick question.'
];
for (i = 0; i < words.length; ++i) {
var str = words[i];
console.log(str.length, str);
console.log('compress', compress(str).length);
console.log('uncom', uncompress(compress(str)).length, uncompress(compress(str)));
}
@xeoncross
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment