Merge pull request #7 from L1fescape/master

Variable arguments and falsy values
This commit is contained in:
Sindre Sorhus 2013-10-19 08:57:13 -07:00
commit 5bbb44b5a8
3 changed files with 17 additions and 3 deletions

View file

@ -27,7 +27,9 @@ function init() {
Object.keys(styles).forEach(function (name) { Object.keys(styles).forEach(function (name) {
ret[name] = { ret[name] = {
get: function () { get: function () {
var obj = defineProps(function self(str) { var obj = defineProps(function self() {
var str = [].slice.call(arguments).join(' ');
if (!chalk.enabled) { if (!chalk.enabled) {
return str; return str;
} }

View file

@ -34,13 +34,16 @@ var chalk = require('chalk');
console.log(chalk.blue('Hello world!')); console.log(chalk.blue('Hello world!'));
// combine styled and normal strings // combine styled and normal strings
console.log(chalk.blue('Hello') + 'World' + chalk.red('!')); console.log(chalk.blue('Hello'), 'World' + chalk.red('!'));
// compose multiple styles using the chainable API // compose multiple styles using the chainable API
console.log(chalk.blue.bgRed.bold('Hello world!')); console.log(chalk.blue.bgRed.bold('Hello world!'));
// nest styles // nest styles
chalk.red('Hello' + chalk.underline.bgBlue('world') + '!'); chalk.red('Hello', chalk.underline.bgBlue('world') + '!');
// pass in multiple arguments
console.log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'))
``` ```
You can easily define your own themes. You can easily define your own themes.
@ -58,6 +61,7 @@ console.log(error('Error!'));
Chain [styles](#styles) and call the last one as a method with a string argument. Chain [styles](#styles) and call the last one as a method with a string argument.
Multiple arguments are also supported. Chalk will add a space between each one.
### chalk.enabled ### chalk.enabled

View file

@ -29,6 +29,14 @@ describe('chalk', function () {
it('should alias gray to grey', function () { it('should alias gray to grey', function () {
assert.equal(chalk.grey('foo'), '\x1b[90mfoo\x1b[39m'); assert.equal(chalk.grey('foo'), '\x1b[90mfoo\x1b[39m');
}); });
it('should support variable number of arguments', function () {
assert.equal(chalk.red('foo', 'bar'), '\x1b[31mfoo bar\x1b[39m');
});
it('should support falsy values', function () {
assert.equal(chalk.red(0), '\x1b[31m0\x1b[39m');
});
}); });
describe('chalk.enabled', function () { describe('chalk.enabled', function () {