From 90bd6477d6d3e2d31dee9b2ee3e721026521bd47 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 24 Jun 2014 21:34:11 +0200 Subject: [PATCH] add `.hasColor()` method - fixes #23 --- index.js | 2 ++ package.json | 3 ++- readme.md | 4 ++++ test.js | 7 +++++++ 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 9854660..057ac62 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,7 @@ var escapeStringRegexp = require('escape-string-regexp'); var ansiStyles = require('ansi-styles'); var stripAnsi = require('strip-ansi'); +var hasAnsi = require('has-ansi'); var supportsColor = require('supports-color'); var defineProps = Object.defineProperties; var chalk = module.exports; @@ -61,6 +62,7 @@ function init() { defineProps(chalk, init()); chalk.styles = ansiStyles; +chalk.hasColor = hasAnsi; chalk.stripColor = stripAnsi; chalk.supportsColor = supportsColor; diff --git a/package.json b/package.json index effe6e8..5a21291 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,8 @@ "dependencies": { "ansi-styles": "^1.1.0", "escape-string-regexp": "^1.0.0", - "strip-ansi": "^0.2.0", + "has-ansi": "^0.1.0", + "strip-ansi": "^0.2.2", "supports-color": "^0.2.0" }, "devDependencies": { diff --git a/readme.md b/readme.md index d28f99d..942ba51 100644 --- a/readme.md +++ b/readme.md @@ -109,6 +109,10 @@ console.log(chalk.styles.red); console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close); ``` +### chalk.hasColor(string) + +Check whether a string [has color](https://github.com/sindresorhus/has-ansi). + ### chalk.stripColor(string) [Strip color](https://github.com/sindresorhus/strip-ansi) from a string. diff --git a/test.js b/test.js index 8c3da0c..5d588d4 100644 --- a/test.js +++ b/test.js @@ -63,6 +63,13 @@ describe('chalk.styles', function () { }); }); +describe('chalk.hasColor()', function () { + it('should detect whether a string has color', function () { + assert(chalk.hasColor(chalk.blue('foo'))); + assert(!chalk.hasColor(chalk.stripColor(chalk.blue('foo')))); + }); +}); + describe('chalk.stripColor()', function () { it('should strip color from string', function () { assert.equal(chalk.stripColor(chalk.underline.red.bgGreen('foo')), 'foo');