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,26 +48,39 @@ var isHeadless = (typeof module !== 'undefined');
});
});
// prototypes string with method "rainbow"
// rainbow will apply a the color spectrum to a string, changing colors every letter
addProperty('rainbow', function () {
if (!isHeadless) {
return this.replace(/( )/, '$1');
}
var rainbowcolors = ['red','yellow','green','blue','magenta']; //RoY G BiV
var exploded = this.split("");
var i=0;
exploded = exploded.map(function(letter) {
if (letter==" ") {
return letter;
}
else {
return stylize(letter,rainbowcolors[i++ % rainbowcolors.length]);
}
});
return exploded.join("");
function sequencer(map) {
return function() {
if (!isHeadless) {
return this.replace(/( )/, '$1');
}
var exploded = this.split("");
var i=0;
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==" ") {
return letter;
} else {
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;
});
function stylize(str, style) {
if (exports.mode == 'console') {
var styles = {

View File

@ -1,20 +1,30 @@
var util = require('util');
var colors = require('./colors');
//colors.mode = "browser";
var test = colors.red("hopefully colorless output");
util.puts('Rainbows are fun!'.rainbow);
util.puts('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
//util.puts('zalgo time!'.zalgo);
util.puts(test.stripColors);
util.puts("a".grey + " b".black);
console.log('Rainbows are fun!'.rainbow);
console.log('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse); // styles not widely supported
console.log('Chains are also cool.'.bold.italic.underline.red); // styles not widely supported
//console.log('zalgo time!'.zalgo);
console.log(test.stripColors);
console.log("a".grey + " b".black);
util.puts(colors.rainbow('Rainbows are fun!'));
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("Zebras are so fun!".zebra);
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);