-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (55 loc) · 1.25 KB
/
index.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
"use strict";
var fs = require('fs');
// map of characters with opposites
var opposites = {
"b": "d",
"d": "b",
"z": "s",
"(": ")",
")": "(",
"[": "]",
"]": "[",
"{": "}",
"}": "{",
"<": ">",
">": "<",
"/": "\\",
"\\": "/"
};
// Read a file, and return the reveresed and original
function reverseArtFile(file, cb) {
fs.readFile(file, function(err, data) {
if (err) return cb(err);
var orig = data.toString();
var art = reverseArt(orig);
cb(null, art, orig);
});
}
// Reverse a string of ascii art
function reverseArt(art) {
art = art.replace('\r\n', '\n')
.split('\n');
var maxLineLen = art.reduce(function(max, line) {
return Math.max(line.length, max);
}, 0);
var art = art.map(function(line) {
var pad = (new Array(1 + maxLineLen - line.length)).join(' ');
line = reverseLine(line + pad);
return line.replace(/\s\+$/, '');
}).join('\n');
return art;
}
// Reverse a line of ascii art
function reverseLine(art) {
var rev = '';
for (var i = art.length - 1; i >= 0; --i) {
if (opposites.hasOwnProperty(art[i])) {
rev += opposites[art[i]];
} else {
rev += art[i];
}
}
return rev;
}
module.exports = reverseArt;
reverseArt.reverseArtFile = reverseArtFile;