Skip to content

Instantly share code, notes, and snippets.

@andrewhodel
Created June 24, 2021 17:54
Show Gist options
  • Save andrewhodel/eda86ce3595b949a14034391d6ed213d to your computer and use it in GitHub Desktop.
Save andrewhodel/eda86ce3595b949a14034391d6ed213d to your computer and use it in GitHub Desktop.
diff two files by line with any type line character
var fs = require('fs');
var o = fs.readFileSync('./file1.txt').toString();
var oo = fs.readFileSync('./file2.txt').toString();
var get_lines_array = function(str) {
if (typeof(str) != 'string') {
console.log('must pass string to get_lines_array()');
process.exit(1);
}
var lines = [];
var c = 0;
var line = '';
var is_newline = false;
while (c < str.length) {
// \n \r or
if ((str[c] == '\n' || str[c] == '\r' || str.charCodeAt(c) == 13)) {
if (!is_newline) {
lines.push(line);
line = '';
}
is_newline = true;
} else {
is_newline = false;
}
if (!is_newline) {
line += str[c];
is_newline == false;
}
c++;
}
return lines;
}
var o_lines = get_lines_array(o);
var oo_lines = get_lines_array(oo);
var longest = o_lines;
var shortest = oo_lines;
if (oo_lines.length > o_lines.length) {
longest = oo_lines;
shortest = o_lines;
}
var c = 0;
while (c < shortest.length) {
if (longest[c] != shortest[c]) {
var l_buffer = Buffer.from(longest[c], 'utf8');
var s_buffer = Buffer.from(shortest[c], 'utf8');
console.log('### LINE ' + c + ' DIFFERS');
console.log(longest[c], l_buffer.toString('hex'));
console.log(shortest[c], s_buffer.toString('hex'));
}
c++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment