Created
May 25, 2011 07:48
-
-
Save line-o/990529 to your computer and use it in GitHub Desktop.
tiny (nano) module for ASCII pattern output
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
/** | |
* 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