diff --git a/colors.js b/colors.js index 685dc9b..977a636 100644 --- a/colors.js +++ b/colors.js @@ -26,34 +26,36 @@ 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 () { - // default to 'this' for those running console.log() from a browser - return isHeadless ? stylize(this, style) : this; - } + + // __defineGetter__ at the least works in more browsers + // http://robertnyman.com/javascript/javascript-getters-setters.html + // Object.defineProperty only works in Chrome + String.prototype.__defineGetter__(style, function () { + return isHeadless ? + stylize(this, style) : // for those running in node (headless environments) + this.replace(/( )/, '$1'); // and for those running in browsers: + // re: ^ you'd think 'return this' works (but doesn't) so replace coerces the string to be a real string }); }); // prototypes string with method "rainbow" // 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]); - } - }); - return exploded.join(""); +String.prototype.__defineGetter__('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 stylize(str, style) { diff --git a/example.html b/example.html new file mode 100644 index 0000000..c9cd68c --- /dev/null +++ b/example.html @@ -0,0 +1,20 @@ + + + + + Colors Example + + + + + + + \ No newline at end of file diff --git a/package.json b/package.json index 61e4a09..5c16a9c 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { "name": "colors", "description": "get colors in your node.js console like what", - "version": "0.3.0", + "version": "0.3.1", "author": "Marak Squires", "repository": { "type": "git", - "url": "http://github.com/Marak/colors.js.git" + "url": "http://github.com/Marak/colors.js.git" }, "engine": [ - "node >=0.1.90" + "node >=0.1.90" ], - "main": "colors" + "main": "colors.js" }