Skip to content

Instantly share code, notes, and snippets.

@ROBERT-MCDOWELL
Forked from fliptopbox/string.compress.js
Created October 30, 2021 12:59
Show Gist options
  • Save ROBERT-MCDOWELL/cf7e822a67e39d28fc9bb8388b4cd57f to your computer and use it in GitHub Desktop.
Save ROBERT-MCDOWELL/cf7e822a67e39d28fc9bb8388b4cd57f to your computer and use it in GitHub Desktop.

Revisions

  1. @fliptopbox fliptopbox revised this gist Oct 21, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions string.compress.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    /*
    @fliptopbox
    LZW Compression/Decompression for Strings
    Implementation of LZW algorithms from:
    http://rosettacode.org/wiki/LZW_compression#JavaScript
  2. @fliptopbox fliptopbox revised this gist Oct 21, 2013. No changes.
  3. @fliptopbox fliptopbox revised this gist Oct 15, 2013. 1 changed file with 92 additions and 78 deletions.
    170 changes: 92 additions & 78 deletions string.compress.js
    Original file line number Diff line number Diff line change
    @@ -1,91 +1,105 @@
    //LZW Compression/Decompression for Strings
    /*
    LZW Compression/Decompression for Strings
    Implementation of LZW algorithms from:
    http://rosettacode.org/wiki/LZW_compression#JavaScript
    Usage:
    var a = 'a very very long string to be squashed';
    var b = a.compress(); // 'a veryāăąlong striċ to bečquashed'
    var c = b.uncompress(); // 'a very very long string to be squashed'
    console.log(a === c); // True
    var d = a.compress(true); // return as Array
    console.log(d); // [97, 32, 118 .... 101, 100] an Array of ASCII codes
    */

    String.prototype.compress = function (asArray) {
    "use strict";
    // Build the dictionary.
    asArray = (asArray === true);
    var i,
    dictionary = {},
    uncompressed = this,
    c,
    wc,
    w = "",
    result = [],
    ASCII = '',
    dictSize = 256;
    for (i = 0; i < 256; i += 1) {
    dictionary[String.fromCharCode(i)] = i;
    }
    "use strict";
    // Build the dictionary.
    asArray = (asArray === true);
    var i,
    dictionary = {},
    uncompressed = this,
    c,
    wc,
    w = "",
    result = [],
    ASCII = '',
    dictSize = 256;
    for (i = 0; i < 256; i += 1) {
    dictionary[String.fromCharCode(i)] = i;
    }

    for (i = 0; i < uncompressed.length; 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[wc]) {
    if (dictionary.hasOwnProperty(wc)) {
    w = wc;
    } else {
    result.push(dictionary[w]);
    ASCII += String.fromCharCode(dictionary[w]);
    // Add wc to the dictionary.
    dictionary[wc] = dictSize++;
    w = String(c);
    }
    }
    for (i = 0; i < uncompressed.length; 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[wc]) {
    if (dictionary.hasOwnProperty(wc)) {
    w = wc;
    } else {
    result.push(dictionary[w]);
    ASCII += String.fromCharCode(dictionary[w]);
    // Add wc to the dictionary.
    dictionary[wc] = dictSize++;
    w = String(c);
    }
    }

    // Output the code for w.
    if (w !== "") {
    result.push(dictionary[w]);
    ASCII += String.fromCharCode(dictionary[w]);
    }
    return asArray ? result : ASCII;
    // Output the code for w.
    if (w !== "") {
    result.push(dictionary[w]);
    ASCII += String.fromCharCode(dictionary[w]);
    }
    return asArray ? result : ASCII;
    };

    String.prototype.decompress = function () {
    "use strict";
    // Build the dictionary.
    var i, tmp = [],
    dictionary = [],
    compressed = this,
    w,
    result,
    k,
    entry = "",
    dictSize = 256;
    for (i = 0; i < 256; i += 1) {
    dictionary[i] = String.fromCharCode(i);
    }
    "use strict";
    // Build the dictionary.
    var i, tmp = [],
    dictionary = [],
    compressed = this,
    w,
    result,
    k,
    entry = "",
    dictSize = 256;
    for (i = 0; i < 256; i += 1) {
    dictionary[i] = String.fromCharCode(i);
    }

    if(compressed && typeof compressed === 'string') {
    // convert string into Array.
    for(i = 0; i < compressed.length; i += 1) {
    tmp.push(compressed[i].charCodeAt(0));
    }
    compressed = tmp;
    tmp = null;
    }
    if(compressed && typeof compressed === 'string') {
    // convert string into Array.
    for(i = 0; i < compressed.length; i += 1) {
    tmp.push(compressed[i].charCodeAt(0));
    }
    compressed = tmp;
    tmp = null;
    }

    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 {
    return null;
    }
    }
    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 {
    return null;
    }
    }

    result += entry;
    result += entry;

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

    w = entry;
    }
    return result;
    w = entry;
    }
    return result;
    };
  4. @fliptopbox fliptopbox created this gist Oct 15, 2013.
    91 changes: 91 additions & 0 deletions string.compress.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    //LZW Compression/Decompression for Strings

    String.prototype.compress = function (asArray) {
    "use strict";
    // Build the dictionary.
    asArray = (asArray === true);
    var i,
    dictionary = {},
    uncompressed = this,
    c,
    wc,
    w = "",
    result = [],
    ASCII = '',
    dictSize = 256;
    for (i = 0; i < 256; i += 1) {
    dictionary[String.fromCharCode(i)] = i;
    }

    for (i = 0; i < uncompressed.length; 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[wc]) {
    if (dictionary.hasOwnProperty(wc)) {
    w = wc;
    } else {
    result.push(dictionary[w]);
    ASCII += String.fromCharCode(dictionary[w]);
    // Add wc to the dictionary.
    dictionary[wc] = dictSize++;
    w = String(c);
    }
    }

    // Output the code for w.
    if (w !== "") {
    result.push(dictionary[w]);
    ASCII += String.fromCharCode(dictionary[w]);
    }
    return asArray ? result : ASCII;
    };

    String.prototype.decompress = function () {
    "use strict";
    // Build the dictionary.
    var i, tmp = [],
    dictionary = [],
    compressed = this,
    w,
    result,
    k,
    entry = "",
    dictSize = 256;
    for (i = 0; i < 256; i += 1) {
    dictionary[i] = String.fromCharCode(i);
    }

    if(compressed && typeof compressed === 'string') {
    // convert string into Array.
    for(i = 0; i < compressed.length; i += 1) {
    tmp.push(compressed[i].charCodeAt(0));
    }
    compressed = tmp;
    tmp = null;
    }

    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 {
    return null;
    }
    }

    result += entry;

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

    w = entry;
    }
    return result;
    };