Skip to content

Instantly share code, notes, and snippets.

@mnmly
Created March 13, 2015 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mnmly/b1ead06b7924c6feebfb to your computer and use it in GitHub Desktop.
Save mnmly/b1ead06b7924c6feebfb 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' )
}
@mnmly
Copy link
Author

mnmly commented Mar 13, 2015

$ node mirror.js

@tchoi8
Copy link

tchoi8 commented Mar 14, 2015

this is so cool. how to I execute it?

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