-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathasciiCompressor.js
151 lines (136 loc) · 3.75 KB
/
asciiCompressor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const fs = require('fs');
const path = require('path');
const MAX_WIDTH = 100;
main();
// Main system
function main(){
switch ( process.argv[2] ){
case 'encode':
case 'e':
encodeFile(process.argv[3], process.argv[4]);
break;
case 'decode':
case 'd':
decodeFile(process.argv[3], process.argv[4]);
break;
case 'analyze':
case 'a':
analyzeCompression(process.argv[3]);
break;
default:
console.log("Invalid command. See README for help.");
break;
}
}
// Given data, encode it with RLE
function encode(data) {
let rows = data.split('\n');
let allResults = [];
// run RLE on each row
rows.forEach((row) => {
let rowChars = row.split('');
let lastSeen = row[0];
let currCount = 0;
let result = '';
rowChars.forEach((c) => {
if ( c === lastSeen ){
currCount += 1;
} else {
// force each count to be two characters long
currCount = currCount.toString().length == 1 ? '0' + currCount : currCount.toString();
result += `${currCount}${lastSeen}`;
lastSeen = c;
currCount = 1;
}
});
// Check for edge case if row is only one character
if ( currCount === MAX_WIDTH){
result = `${lastSeen}`;
} else if ( currCount > 0 ){
currCount = currCount.toString().length == 1 ? '0' + currCount : currCount.toString();
result += `${currCount}${lastSeen}`;
}
allResults.push(result);
});
return allResults.join('\n');
}
// Encode a given file
function encodeFile(fileName, outputFileName){
const cb = (data) => {
const encodedData = encode(data);
if ( outputFileName ){
const outputFilePath = path.join(__dirname, outputFileName);
writeFile(outputFilePath, encodedData);
}
return data;
}
return readFile(fileName, cb)
}
// Decodes encoded data from RLE
function decode(data) {
let rows = data.split('\n');
let allResults = [];
rows.forEach((row) => {
let result = ''
// All chars in row are the same
if ( row.length === 1 ){
result = new Array(MAX_WIDTH + 1).join( row[0] );
} else {
for ( let i = 0; i < row.length - 2; i += 3 ){
const length = parseInt(row.slice(i, i + 2));
const c = row.slice(i + 2, i + 3);
result += new Array(length + 1).join( c );
}
}
allResults.push(result);
});
return allResults.join('\n');
}
// Decode a given file
function decodeFile(fileName, outputFileName){
const cb = (data) => {
const decodedData = decode(data);
if ( outputFileName ){
const outputFilePath = path.join(__dirname, outputFileName);
writeFile(outputFilePath, decodedData);
}
return data;
}
return readFile(fileName, cb)
}
/************************/
/*** HELPER FUNCTIONS ***/
/************************/
function readFile(fileName, callback){
const filePath = path.join(__dirname, fileName);
fs.readFile(filePath, {encoding: 'ascii'}, (err,data) => {
if (!err) {
return callback(data);
} else {
console.log(err);
}
});
}
function writeFile(fileName, data){
const filePath = path.join(__dirname, fileName);
fs.writeFile(fileName, data, function(err) {
if(err) {
return console.log(err);
}
});
}
function analyzeCompression(fileName){
if ( !fileName ){
console.log(`Cannot find file ${fileName}`);
}
const cb = (data) => {
console.log(`Rows: ${data.split('\n').length}`)
console.log(`Original: ${data.length}`)
const encodedData = encode(data);
console.log(`Compressed: ${encodedData.length}`);
const percentReduction = (encodedData.length - data.length) / data.length;
console.log(`Percent reduction: ${percentReduction}%`);
};
readFile(fileName, cb);
}
module.exports = { encode, decode };