Created
May 1, 2014 16:46
-
-
Save tancredi/aec570a723043eaf9ff3 to your computer and use it in GitHub Desktop.
Quick node.js script to mirror ASCII art horizontally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var charsMap = [ | |
[ '/', '(', '>' ], | |
[ '\\', ')', '<' ] | |
]; | |
function run () { | |
var args = parseArgs(), | |
text = normalise(fs.readFileSync(args.src, 'utf8')); | |
console.log(flipText(text)); | |
} | |
function parseArgs () { | |
var out = { | |
src: process.argv[2] | |
}; | |
if (!out.src) { | |
console.error('Arguments: source_file'); | |
process.exit(1); | |
} | |
if (!fs.existsSync(out.src)) { | |
console.error('Source file not fond'); | |
process.exit(1); | |
} | |
return out; | |
} | |
function flipChar (char) { | |
if (charsMap[0].indexOf(char) !== -1) { | |
return charsMap[1][charsMap[0].indexOf(char)]; | |
} else if (charsMap[1].indexOf(char) !== -1) { | |
return charsMap[0][charsMap[1].indexOf(char)]; | |
} | |
return char; | |
} | |
function getWidth (str) { | |
var out = 0; | |
str.split('\n').forEach(function (line) { | |
out = Math.max(out, line.length); | |
}); | |
return out; | |
} | |
function normalise (str) { | |
var out = [], | |
width = getWidth(str); | |
str.split('\n').forEach(function (line) { | |
var newLine = ''; | |
for (var i = 0; i < width; i += 1) { | |
newLine += line[i] || ' '; | |
} | |
out.push(newLine); | |
}); | |
return out.join('\n'); | |
} | |
function flipText (text) { | |
var out = ''; | |
text.split('\n').forEach(function (line) { | |
for (var i = line.length - 1; i >= 0; i -= 1) { | |
out += flipChar(line[i]); | |
} | |
out += '\n'; | |
}); | |
return out; | |
} | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment