add better support for browser fallback. moaaar tests

This commit is contained in:
Dustin Diaz 2011-03-14 21:23:06 -07:00
parent e49356a4c2
commit 9310a89b24
3 changed files with 48 additions and 26 deletions

View File

@ -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) {

20
example.html Normal file
View File

@ -0,0 +1,20 @@
<!DOCTYPE HTML>
<html lang="en-us">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Colors Example</title>
<script src="colors.js"></script>
<script type="text/javascript">
console.log('Rainbows are fun!'.rainbow);
console.log('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse);
console.log('Chains are also cool.'.bold.italic.underline.red);
</script>
</head>
<body>
<script>
document.write('Rainbows are fun!'.rainbow + '<br>');
document.write('So '.italic + 'are'.underline + ' styles! '.bold + 'inverse'.inverse + '<br>');
document.write('Chains are also cool.'.bold.italic.underline.red);
</script>
</body>
</html>

View File

@ -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"
}