breaks rainbow into sequencer and map function

This adds the ability to easily add new sequences. Adds two examples
(zebra, red, white, blue). Also exports the addSequencer method to
programs including colors can add new sequences on the fly.

Conflicts:

	example.js
This commit is contained in:
Morgan "ARR!" Allen 2011-09-16 08:50:37 -07:00 committed by Maciej Małecki
parent 0e5305709d
commit 158d165ad6
2 changed files with 54 additions and 31 deletions

View File

@ -48,25 +48,38 @@ var isHeadless = (typeof module !== 'undefined');
}); });
}); });
// prototypes string with method "rainbow" function sequencer(map) {
// rainbow will apply a the color spectrum to a string, changing colors every letter return function() {
addProperty('rainbow', function () {
if (!isHeadless) { if (!isHeadless) {
return this.replace(/( )/, '$1'); return this.replace(/( )/, '$1');
} }
var rainbowcolors = ['red','yellow','green','blue','magenta']; //RoY G BiV
var exploded = this.split(""); var exploded = this.split("");
var i=0; var i=0;
exploded = exploded.map(function(letter) { exploded = exploded.map(map);
return exploded.join("");
}
}
var rainbowMap = (function () {
var rainbowcolors = ['red','yellow','green','blue','magenta']; //RoY G BiV
return function(letter, i, exploded) {
if (letter==" ") { if (letter==" ") {
return letter; return letter;
} } else {
else {
return stylize(letter,rainbowcolors[i++ % rainbowcolors.length]); return stylize(letter,rainbowcolors[i++ % rainbowcolors.length]);
} }
}
})();
exports.addSequencer = function(name, map) {
addProperty(name, sequencer(map));
}
exports.addSequencer('rainbow', rainbowMap);
exports.addSequencer('zebra', function(letter, i, exploded) {
return i % 2 === 0 ? letter : letter.inverse;
}); });
return exploded.join("");
});
function stylize(str, style) { function stylize(str, style) {
if (exports.mode == 'console') { if (exports.mode == 'console') {

View File

@ -1,20 +1,30 @@
var util = require('util');
var colors = require('./colors'); var colors = require('./colors');
//colors.mode = "browser"; //colors.mode = "browser";
var test = colors.red("hopefully colorless output"); var test = colors.red("hopefully colorless output");
util.puts('Rainbows are fun!'.rainbow); console.log('Rainbows are fun!'.rainbow);
util.puts('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported console.log('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported
util.puts('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported console.log('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported
//util.puts('zalgo time!'.zalgo); //console.log('zalgo time!'.zalgo);
util.puts(test.stripColors); console.log(test.stripColors);
util.puts("a".grey + " b".black); console.log("a".grey + " b".black);
util.puts(colors.rainbow('Rainbows are fun!')); console.log("Zebras are so fun!".zebra);
util.puts(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported
util.puts(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported
//util.puts(colors.zalgo('zalgo time!'));
util.puts(colors.stripColors(test));
util.puts(colors.grey("a") + colors.black(" b"));
console.log(colors.rainbow('Rainbows are fun!'));
console.log(colors.italic('So ') + colors.underline('are') + colors.bold(' styles! ') + colors.inverse('inverse')); // styles not widely supported
console.log(colors.bold(colors.italic(colors.underline(colors.red('Chains are also cool.'))))); // styles not widely supported
//console.log(colors.zalgo('zalgo time!'));
console.log(colors.stripColors(test));
console.log(colors.grey("a") + colors.black(" b"));
colors.addSequencer("america", function(letter, i, exploded) {
switch(i%3) {
case 0: return letter.red;
case 1: return letter.white;
case 2: return letter.blue;
}
});
console.log("AMERICA! F--K YEAH!".america);