lz-string.js 2.59 KB
(function(root, factory) {
    var LZW = factory();

    if (typeof define !== "undefined" && define.amd) {
        define(function() {
            return LZW;
        });
    }

    root.LZW = LZW;
}(window, function () {
    return {
        compress: function (uncompressed) {
            "use strict";
            // Build the dictionary.
            var i,
                dictionary = {},
                c,
                wc,
                w = "",
                result = [],
                dictSize = Math.pow(2, 16),//Unicode字符数值最大值加一
                strLength = uncompressed.length;

            for (i = 0; i < strLength; i += 1) {
                c = uncompressed.charAt(i);
                wc = w + c;
                //Do not use dictionary[wc] because javascript arrays 
                //will return values for array['pop'], array['push'] etc
                if (dictionary.hasOwnProperty(wc)) {
                    w = wc;
                } else {
                    if (w && !dictionary[w]) {
                        dictionary[w] = w.charCodeAt();
                    }

                    if (dictionary[w]) {
                        result.push(dictionary[w]);
                        dictionary[wc] = dictSize++;
                    }
                    // Add wc to the dictionary.
                    w = String(c);
                }
            }

            // Output the code for w.
            if (w !== "") {
                result.push(dictionary[w] || w.charCodeAt());
            }
            return result.join(",");
        },
        decompress: function (compressed) {
            "use strict";
            // Build the dictionary.
            var i,
                dictionary = [],
                w,
                result,
                k,
                entry = "",
                dictSize = Math.pow(2, 16);//Unicode字符数值最大值加一

            w = String.fromCharCode(compressed[0]);
            result = w;
            for (i = 1; i < compressed.length; i += 1) {
                k = compressed[i];
                if (dictionary[k]) {
                    entry = dictionary[k];
                } else {
                    if (k === dictSize) {
                        entry = w + w.charAt(0);
                    } else {
                        entry = String.fromCharCode(k);
                    }
                }

                result += entry;

                // Add w+entry[0] to the dictionary.
                dictionary[dictSize++] = w + entry.charAt(0);

                w = entry;
            }
            return result;
        }
    };
}));