// Compress an String var lzw_encode = function lzw_encode(s,log) { if(log !== false){log = true} //if log is undefined or true set it to true var dict = {}; //initialize dictionary var data = (s + "").split(""); //initialize data var out = []; //initialize output array var currChar; //define the first character var phrase = data[0]; //initialize the first letter var code = 0; //define the current code var load = {now:0,max:data.length,before:0,character:'#'}; //define the loading bar object //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 process.stdout.clearLine() process.stdout.cursorTo(0) if(log){process.stdout.write('initializing...\n')} //initialize the system and find the highest character code for(var i=0;i 1 ? dict[phrase] : phrase.charCodeAt(0)); dict[phrase + currChar] = code; code++; phrase=currChar; } load.now = Math.round(100*(i/load.max)); //update the value of the loading bar //if log is true display the loading bar and update the loading bar object with the current state if(load.before !== load.now && log){ process.stdout.clearLine(); process.stdout.cursorTo(0); process.stdout.write(load.now+'%'); process.stdout.cursorTo(5); process.stdout.write((load.character).repeat(Math.round(load.now/4))); load.before = load.now; } } out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); //write the output object with the characters from the dictionary load = {now:0,max:data.length,before:0,character:'#'} //reset the loading object //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 process.stdout.clearLine(); process.stdout.cursorTo(0); if(log){process.stdout.write('formating...\n')} //format the string codes in the output object to characters for (var i=0; i