/*! * @link https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch * @license MIT */ (function(win) { function convert_compat_string(e) { if (e === undefined || e === null || typeof e === 'boolean') { e = e ? '1' : ''; } return String(e); } win.lzw = { compress: function (e) { e = convert_compat_string(e); if (e.length < 2) { return e; } e = e.split(''); var currChar, dict = {}, out = '', phrase = e[0], code = 256, i = 1; for (; i < e.length; i++) { currChar = e[i]; if (dict[phrase + currChar] !== undefined) { phrase += currChar; } else { out += (phrase.length > 1 ? String.fromCharCode(dict[phrase]) : phrase[0]); dict[phrase + currChar] = code; code++; phrase = currChar; } } e = null; dict = dict[phrase]; out += (dict !== undefined && phrase.length > 1 ? String.fromCharCode(dict) : phrase); return out; }, decompress: function (e) { e = convert_compat_string(e); if (e.length < 2) { return e; } var currCode, dict = {}, currChar = e[0], oldPhrase = currChar, out = currChar, code = 256, phrase, i = 1; for (; i < e.length; i++) { currCode = e[i].charCodeAt(0); if (currCode < 256) { phrase = e[i]; } else { phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar); } out += phrase; currChar = phrase.charAt(0); dict[code] = oldPhrase + currChar; code++; oldPhrase = phrase; } e = dict = null; return out; } }; })(self);