Skip to content

Commit

Permalink
check parent builder object for enabled status (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Jan 30, 2017
1 parent 5a69476 commit 0d21449
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
14 changes: 13 additions & 1 deletion index.js
Expand Up @@ -36,8 +36,20 @@ function build(_styles) {
return applyStyle.apply(builder, arguments);
};

var self = this;

builder._styles = _styles;
builder.enabled = this.enabled;

Object.defineProperty(builder, 'enabled', {
enumerable: true,
get: function () {
return self.enabled;
},
set: function (v) {
self.enabled = v;
}
});

// __proto__ is used because we must return a function, but there is
// no way to create a function with a different prototype.
/* eslint-disable no-proto */
Expand Down
22 changes: 22 additions & 0 deletions test.js
Expand Up @@ -138,6 +138,28 @@ describe('chalk.enabled', function () {
assert.equal(chalk.red('foo'), 'foo');
chalk.enabled = true;
});

it('should enable/disable colors based on overall chalk enabled property, not individual instances', function () {
chalk.enabled = true;
var red = chalk.red;
assert.equal(red.enabled, true);
chalk.enabled = false;
assert.equal(red.enabled, chalk.enabled);
chalk.enabled = true;
});

it('should propagate enable/disable changes from child colors', function () {
chalk.enabled = true;
var red = chalk.red;
assert.equal(red.enabled, true);
assert.equal(chalk.enabled, true);
red.enabled = false;
assert.equal(red.enabled, false);
assert.equal(chalk.enabled, false);
chalk.enabled = true;
assert.equal(red.enabled, true);
assert.equal(chalk.enabled, true);
});
});

describe('chalk.constructor', function () {
Expand Down

0 comments on commit 0d21449

Please sign in to comment.