From 6bb27b7759ace4ccb347a6f1ec8bf4620297aff2 Mon Sep 17 00:00:00 2001 From: Andrew Kennedy Date: Mon, 7 Oct 2013 00:34:32 -0400 Subject: [PATCH] handle the ability to pass in variable arguments (ex. chalk.green("it's", "over", 9000)). updated readme to show support of variable arguments. --- chalk.js | 4 +++- readme.md | 8 ++++++-- test.js | 8 ++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/chalk.js b/chalk.js index 17c6dba..06def35 100644 --- a/chalk.js +++ b/chalk.js @@ -27,7 +27,9 @@ function init() { Object.keys(styles).forEach(function (name) { ret[name] = { get: function () { - var obj = defineProps(function self(str) { + var obj = defineProps(function self() { + var str = [].slice.call(arguments).join(' '); + if (!chalk.enabled) { return str; } diff --git a/readme.md b/readme.md index d369d2e..32aa4a9 100644 --- a/readme.md +++ b/readme.md @@ -34,13 +34,16 @@ var chalk = require('chalk'); console.log(chalk.blue('Hello world!')); // 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 console.log(chalk.blue.bgRed.bold('Hello world!')); // 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. @@ -58,6 +61,7 @@ console.log(error('Error!')); 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 diff --git a/test.js b/test.js index 93256e3..2241744 100644 --- a/test.js +++ b/test.js @@ -29,6 +29,14 @@ describe('chalk', function () { it('should alias gray to grey', function () { 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 () {