SWAG98

LZW-Encoding&Decoding module (half commentary) (nodejs/js)

Jan 18th, 2017
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Compress an String
  2. var lzw_encode = function lzw_encode(s,log) {
  3.     if(log !== false){log = true} //if log is undefined or true set it to true
  4.     var dict = {}; //initialize dictionary
  5.     var data = (s + "").split(""); //initialize data
  6.     var out = []; //initialize output array
  7.     var currChar; //define the first character
  8.     var phrase = data[0]; //initialize the first letter
  9.     var code = 0; //define the current code
  10.     var load = {now:0,max:data.length,before:0,character:'#'}; //define the loading bar object
  11.     //clear the current line and set the cursor to position 0 (initializes the current loading bar). then check if log is true. if its true write 'initializing...' to show the current state
  12.     process.stdout.clearLine()
  13.     process.stdout.cursorTo(0)
  14.     if(log){process.stdout.write('initializing...\n')}
  15.     //initialize the system and find the highest character code
  16.     for(var i=0;i<data.length;i++){
  17.         code = Math.max(code,parseInt(data[i].charCodeAt(0))+1); //compare the character codes and find the higher one to get the highest character
  18.         load.now = Math.round(100*(i/load.max)); //update the value of the loading bar
  19.         //if log is true display the loading bar and update the loading bar object with the current state
  20.         if(load.before !== load.now && log){
  21.             process.stdout.clearLine();
  22.             process.stdout.cursorTo(0);
  23.             process.stdout.write(load.now+'%');
  24.             process.stdout.cursorTo(5);
  25.             process.stdout.write((load.character).repeat(Math.round(load.now/4)));
  26.             load.before = load.now;
  27.         }
  28.     }
  29.     var ocode=code; //define the original encoding character
  30.     load = {now:0,max:data.length,before:0,character:'#'} //reset the loading object
  31.     //clear the current line and set the cursor to position 0 (initializes the current loading bar). then check if log is true. if its true write 'compressing...' to show the current state   
  32.     process.stdout.clearLine();
  33.     process.stdout.cursorTo(0);
  34.     if(log){process.stdout.write('compressing...\n')}
  35.     //cycle through the data and compress the data with an lzw-encoding:
  36.     for (var i=1; i<data.length; i++) {
  37.         currChar=data[i]; //select current character
  38.         if (dict[phrase + currChar] != null) { //if the selected character-combination was found in the dictionary
  39.             phrase += currChar; //add the current character to the phrase
  40.         } else { //if the selected character-combination was not found in the dictionary add it to the dictionary and add the character to the output
  41.             out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
  42.             dict[phrase + currChar] = code;
  43.             code++;
  44.             phrase=currChar;
  45.         }
  46.         load.now = Math.round(100*(i/load.max)); //update the value of the loading bar
  47.         //if log is true display the loading bar and update the loading bar object with the current state
  48.         if(load.before !== load.now && log){
  49.             process.stdout.clearLine();
  50.             process.stdout.cursorTo(0);
  51.             process.stdout.write(load.now+'%');
  52.             process.stdout.cursorTo(5);
  53.             process.stdout.write((load.character).repeat(Math.round(load.now/4)));
  54.             load.before = load.now;
  55.         }
  56.     }
  57.     out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); //write the output object with the characters from the dictionary
  58.     load = {now:0,max:data.length,before:0,character:'#'} //reset the loading object
  59.     //clear the current line and set the cursor to position 0 (initializes the current loading bar). then check if log is true. if its true write 'formating...' to show the current state
  60.     process.stdout.clearLine();
  61.     process.stdout.cursorTo(0);
  62.     if(log){process.stdout.write('formating...\n')}
  63.     //format the string codes in the output object to characters
  64.     for (var i=0; i<out.length; i++) {
  65.         out[i] = String.fromCharCode(out[i]);
  66.         load.now = Math.round(100*(i/load.max)); //update the value of the loading bar
  67.         //if log is true display the loading bar and update the loading bar object with the current state
  68.         if(load.before !== load.now && log){
  69.             process.stdout.clearLine();
  70.             process.stdout.cursorTo(0);
  71.             process.stdout.write(load.now+'%');
  72.             process.stdout.cursorTo(5);
  73.             process.stdout.write((load.character).repeat(Math.round(load.now/4)));
  74.             load.before = load.now;
  75.         }
  76.     }
  77.     //clear line and set the cursor position to 0
  78.     process.stdout.clearLine();
  79.     process.stdout.cursorTo(0);
  80.     out.unshift(String.fromCharCode(ocode)); //add the encoding character to the front of the output for easy decoding
  81.     return out.join(""); //return the output
  82. }
  83.  
  84. // Decompress an LZW-encoded string
  85. var lzw_decode = function lzw_decode(s,log) {
  86.     if(log !== false){log = true}
  87.     var dict = {};
  88.     var data = (s + "").split("");
  89.     var code = data[0].charCodeAt(0);
  90.     var ocode=code;
  91.     data.splice(0,1);
  92.     var currChar = data[0];
  93.     var oldPhrase = currChar;
  94.     var out = [currChar];
  95.     var phrase;
  96.     load = {now:0,max:data.length,before:0,character:'#'}
  97.     process.stdout.clearLine()
  98.     process.stdout.cursorTo(0)
  99.     if(log){process.stdout.write('decompressing...\n')}
  100.     for (var i=1; i<data.length; i++) {
  101.         load.now = Math.round(100*(i/load.max))
  102.         if(load.before !== load.now && log){
  103.             process.stdout.clearLine()
  104.             process.stdout.cursorTo(0)
  105.             process.stdout.write(load.now+'%')
  106.             process.stdout.cursorTo(5)
  107.             process.stdout.write((load.character).repeat(Math.round(load.now/4)))
  108.             load.before = load.now
  109.         }
  110.         var currCode = data[i].charCodeAt(0);
  111.         if (currCode < ocode) {
  112.             phrase = data[i];
  113.         }
  114.         else {
  115.            phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
  116.         }
  117.         out.push(phrase);
  118.         currChar = phrase.charAt(0);
  119.         dict[code] = oldPhrase + currChar;
  120.         code++;
  121.         oldPhrase = phrase;
  122.     }
  123.     process.stdout.clearLine()
  124.     process.stdout.cursorTo(0)
  125.     return out.join("");
  126. }
  127.  
  128. this.encode = lzw_encode
  129. this.decode = lzw_decode
Advertisement
Add Comment
Please, Sign In to add comment