add .hasColor() method - fixes #23

This commit is contained in:
Sindre Sorhus 2014-06-24 21:34:11 +02:00
parent eff96c2c15
commit 90bd6477d6
4 changed files with 15 additions and 1 deletions

View file

@ -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;

View file

@ -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": {

View file

@ -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.

View file

@ -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');