Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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<data.length;i++){
- code = Math.max(code,parseInt(data[i].charCodeAt(0))+1); //compare the character codes and find the higher one to get the highest character
- 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;
- }
- }
- var ocode=code; //define the original encoding character
- 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 'compressing...' to show the current state
- process.stdout.clearLine();
- process.stdout.cursorTo(0);
- if(log){process.stdout.write('compressing...\n')}
- //cycle through the data and compress the data with an lzw-encoding:
- for (var i=1; i<data.length; i++) {
- currChar=data[i]; //select current character
- if (dict[phrase + currChar] != null) { //if the selected character-combination was found in the dictionary
- phrase += currChar; //add the current character to the phrase
- } else { //if the selected character-combination was not found in the dictionary add it to the dictionary and add the character to the output
- out.push(phrase.length > 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<out.length; i++) {
- out[i] = String.fromCharCode(out[i]);
- 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;
- }
- }
- //clear line and set the cursor position to 0
- process.stdout.clearLine();
- process.stdout.cursorTo(0);
- out.unshift(String.fromCharCode(ocode)); //add the encoding character to the front of the output for easy decoding
- return out.join(""); //return the output
- }
Advertisement
Add Comment
Please, Sign In to add comment