Skip to content

Instantly share code, notes, and snippets.

@tchoi8
Forked from mnmly/mirror.js
Last active August 29, 2015 14:17
Show Gist options
  • Save tchoi8/c4a3bbdc102580f45b5e to your computer and use it in GitHub Desktop.
Save tchoi8/c4a3bbdc102580f45b5e to your computer and use it in GitHub Desktop.
var fs = require( 'fs' )
var struggle = fs.readFileSync( __dirname + '/struggle.txt', 'utf-8' )
console.log( mirror(struggle, 45, true ) );
console.log( '--------------------' );
console.log( mirror(struggle, 90, false ) );
function mirror( str, gap, keep ) {
var lines = str.split( '\n' )
var longest = 0
lines.forEach(function( line ) {
if ( longest < line.length ) longest = line.length
})
lines.forEach( function( line, i ) {
var delta = longest - line.length + gap
var fill = ' '
var mirrored
while( delta ) { line += fill; delta--; }
mirrored = line.split( '' ).reverse().join( '' )
if ( keep ) {
lines[ i ] = line + mirrored
} else {
delta = line.length;
while( delta ) { line = fill + mirrored; delta--; }
lines[ i ] = mirrored
}
} )
return lines.join( '\n' )
}
@tchoi8
Copy link
Author

tchoi8 commented Mar 14, 2015

hahaha so cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment