Skip to content

Instantly share code, notes, and snippets.

@JavaScript-Packer
Created July 31, 2015 05:45
Show Gist options
  • Save JavaScript-Packer/6eb1dbb94f8c511a0a06 to your computer and use it in GitHub Desktop.
Save JavaScript-Packer/6eb1dbb94f8c511a0a06 to your computer and use it in GitHub Desktop.
LZW encoder, old compression used in GIF images. Also the function is used on http://www.whak.ca/packer/LZW(HAK).htm
function lzw_encode(u) {
var r, a, n = {}, t = (u + "").split(""), e = [], i = t[0], o = 256;
for (a = 1; a < t.length; a++) r = t[a], null != n[i + r] ? i += r :(e.push(i.length > 1 ? n[i] :i.charCodeAt(0)),
n[i + r] = o, o++, i = r);
for (e.push(i.length > 1 ? n[i] :i.charCodeAt(0)), a = 0; a < e.length; a++) e[a] = String.fromCharCode(e[a]);
return e.join("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment