From e49356a4c25a65909f41c20556472f57d402e0b7 Mon Sep 17 00:00:00 2001 From: "Dustin Diaz (@ded)" Date: Mon, 7 Mar 2011 18:09:07 -0800 Subject: [PATCH] default to original string if you're not in a headless environment (like a browser). makes console.log('hello'.red) work. --- colors.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/colors.js b/colors.js index c56ff20..685dc9b 100644 --- a/colors.js +++ b/colors.js @@ -1,5 +1,5 @@ /* -colors.js +colors.js Copyright (c) 2010 Alexis Sellier (cloudhead) , Marak Squires @@ -24,10 +24,12 @@ THE SOFTWARE. */ // prototypes the string object to have additional method calls that add terminal colors +var isHeadless = (typeof module !== 'undefined'); ['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) { Object.defineProperty(String.prototype, style, { get: function () { - return stylize(this, style); + // default to 'this' for those running console.log() from a browser + return isHeadless ? stylize(this, style) : this; } }); }); @@ -36,13 +38,16 @@ THE SOFTWARE. // rainbow will apply a the color spectrum to a string, changing colors every letter Object.defineProperty(String.prototype, 'rainbow', { get: function () { + if (!isHeadless) { + return this; + } 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]); }