Skip to content

Instantly share code, notes, and snippets.

@line-o
Created May 25, 2011 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save line-o/990529 to your computer and use it in GitHub Desktop.
Save line-o/990529 to your computer and use it in GitHub Desktop.
tiny (nano) module for ASCII pattern output
/**
* node-ASCIIp
* @author: contact@line-o.de
*
* Example:
* var write = require('./node-ASCIIp');
* console.log(write(' _.*._ ',12,5));
*/
module.exports = ASCIIp;
/**
* Return ASCII pattern repeated for
* 'height' lines with
* 'width' characters per line.
*
* @param {String} pattern
* @param {Number} width
* @param {Number} height
* @returns {String} linebreak separated lines with pattern repeating
*/
function ASCIIp (pattern, width, height) {
var max = width * height, // maximum characters needed
out = [], // length including EOL chars
c = 0, // counter, index
p = pattern.split(''); // pattern, split into an array
while ( c < max ) {
out.push( p[ c % p.length ]); // add next character of pattern
if ( c % width == width - 1 ) { // last character of this line?
out.push("\n"); // add EOL
}
c++;
}
return out.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment