mirror of
https://github.com/flynx/colors.js.git
synced 2025-10-30 03:20:10 +00:00
add better support for browser fallback. moaaar tests
This commit is contained in:
parent
e49356a4c2
commit
9310a89b24
46
colors.js
46
colors.js
@ -26,34 +26,36 @@ THE SOFTWARE.
|
|||||||
// prototypes the string object to have additional method calls that add terminal colors
|
// prototypes the string object to have additional method calls that add terminal colors
|
||||||
var isHeadless = (typeof module !== 'undefined');
|
var isHeadless = (typeof module !== 'undefined');
|
||||||
['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) {
|
['bold', 'underline', 'italic', 'inverse', 'grey', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) {
|
||||||
Object.defineProperty(String.prototype, style, {
|
|
||||||
get: function () {
|
// __defineGetter__ at the least works in more browsers
|
||||||
// default to 'this' for those running console.log() from a browser
|
// http://robertnyman.com/javascript/javascript-getters-setters.html
|
||||||
return isHeadless ? stylize(this, style) : this;
|
// 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"
|
// prototypes string with method "rainbow"
|
||||||
// rainbow will apply a the color spectrum to a string, changing colors every letter
|
// rainbow will apply a the color spectrum to a string, changing colors every letter
|
||||||
Object.defineProperty(String.prototype, 'rainbow', {
|
String.prototype.__defineGetter__('rainbow', function () {
|
||||||
get: function () {
|
if (!isHeadless) {
|
||||||
if (!isHeadless) {
|
return this.replace(/( )/, '$1');
|
||||||
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("");
|
|
||||||
}
|
}
|
||||||
|
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) {
|
function stylize(str, style) {
|
||||||
|
|||||||
20
example.html
Normal file
20
example.html
Normal 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>
|
||||||
@ -1,14 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "colors",
|
"name": "colors",
|
||||||
"description": "get colors in your node.js console like what",
|
"description": "get colors in your node.js console like what",
|
||||||
"version": "0.3.0",
|
"version": "0.3.1",
|
||||||
"author": "Marak Squires",
|
"author": "Marak Squires",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "http://github.com/Marak/colors.js.git"
|
"url": "http://github.com/Marak/colors.js.git"
|
||||||
},
|
},
|
||||||
"engine": [
|
"engine": [
|
||||||
"node >=0.1.90"
|
"node >=0.1.90"
|
||||||
],
|
],
|
||||||
"main": "colors"
|
"main": "colors.js"
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user