diff --git a/colors.js b/colors.js index 8c1d706..e31e5a2 100644 --- a/colors.js +++ b/colors.js @@ -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 = { diff --git a/example.js b/example.js index 12d8b1a..639949f 100644 --- a/example.js +++ b/example.js @@ -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);