Merge pull request #33 from seanmonstar/_styles
fix caching of styles, fixes #32
This commit is contained in:
commit
ca250abed2
4 changed files with 43 additions and 7 deletions
|
|
@ -9,6 +9,7 @@
|
||||||
"indent": 4,
|
"indent": 4,
|
||||||
"newcap": true,
|
"newcap": true,
|
||||||
"noarg": true,
|
"noarg": true,
|
||||||
|
"proto": true,
|
||||||
"quotmark": "single",
|
"quotmark": "single",
|
||||||
"undef": true,
|
"undef": true,
|
||||||
"unused": "vars",
|
"unused": "vars",
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,11 @@ suite('chalk', function () {
|
||||||
chalk.blue.bgRed.bold('the fox jumps over the lazy dog');
|
chalk.blue.bgRed.bold('the fox jumps over the lazy dog');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var cached = chalk.blue.bgRed.bold;
|
||||||
|
bench('cached styles', function () {
|
||||||
|
cached('the fox jumps over the lazy dog');
|
||||||
|
});
|
||||||
|
|
||||||
bench('nested styles', function () {
|
bench('nested styles', function () {
|
||||||
chalk.red('the fox jumps', chalk.underline.bgBlue('over the lazy dog') + '!');
|
chalk.red('the fox jumps', chalk.underline.bgBlue('over the lazy dog') + '!');
|
||||||
});
|
});
|
||||||
|
|
|
||||||
33
index.js
33
index.js
|
|
@ -7,6 +7,17 @@ var supportsColor = require('supports-color');
|
||||||
var defineProps = Object.defineProperties;
|
var defineProps = Object.defineProperties;
|
||||||
var chalk = module.exports;
|
var chalk = module.exports;
|
||||||
|
|
||||||
|
function build(_styles) {
|
||||||
|
var builder = function builder() {
|
||||||
|
return applyStyle.apply(builder, arguments);
|
||||||
|
};
|
||||||
|
builder._styles = _styles;
|
||||||
|
// __proto__ is used because we must return a function, but there is
|
||||||
|
// no way to create a function with a different prototype.
|
||||||
|
builder.__proto__ = proto;
|
||||||
|
return builder;
|
||||||
|
}
|
||||||
|
|
||||||
var styles = (function () {
|
var styles = (function () {
|
||||||
var ret = {};
|
var ret = {};
|
||||||
|
|
||||||
|
|
@ -17,8 +28,7 @@ var styles = (function () {
|
||||||
|
|
||||||
ret[key] = {
|
ret[key] = {
|
||||||
get: function () {
|
get: function () {
|
||||||
this._styles.push(key);
|
return build(this._styles.concat(key));
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
@ -26,15 +36,26 @@ var styles = (function () {
|
||||||
return ret;
|
return ret;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
var proto = defineProps(function chalk() {}, styles);
|
||||||
|
|
||||||
function applyStyle() {
|
function applyStyle() {
|
||||||
// support varags, but simply cast to string in case there's only one arg
|
// support varags, but simply cast to string in case there's only one arg
|
||||||
var str = arguments.length === 1 ? String(arguments[0]) : [].slice.call(arguments).join(' ');
|
var args = arguments;
|
||||||
|
var argsLen = args.length;
|
||||||
|
var str = argsLen !== 0 && String(arguments[0]);
|
||||||
|
if (argsLen > 1) {
|
||||||
|
// don't slice `arguments`, it prevents v8 optimizations
|
||||||
|
for (var a = 1; a < argsLen; a++) {
|
||||||
|
str += ' ' + args[a];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!chalk.enabled || !str) {
|
if (!chalk.enabled || !str) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
var nestedStyles = applyStyle._styles;
|
/*jshint validthis: true*/
|
||||||
|
var nestedStyles = this._styles;
|
||||||
|
|
||||||
for (var i = 0; i < nestedStyles.length; i++) {
|
for (var i = 0; i < nestedStyles.length; i++) {
|
||||||
var code = ansiStyles[nestedStyles[i]];
|
var code = ansiStyles[nestedStyles[i]];
|
||||||
|
|
@ -51,11 +72,9 @@ function init() {
|
||||||
var ret = {};
|
var ret = {};
|
||||||
|
|
||||||
Object.keys(styles).forEach(function (name) {
|
Object.keys(styles).forEach(function (name) {
|
||||||
var style = defineProps(applyStyle, styles);
|
|
||||||
ret[name] = {
|
ret[name] = {
|
||||||
get: function () {
|
get: function () {
|
||||||
style._styles = [];
|
return build([name]);
|
||||||
return style[name];
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
||||||
11
test.js
11
test.js
|
|
@ -32,6 +32,17 @@ describe('chalk', function () {
|
||||||
assert.equal(chalk.reset(chalk.red.bgGreen.underline('foo') + 'foo'), '\u001b[0m\u001b[4m\u001b[42m\u001b[31mfoo\u001b[39m\u001b[49m\u001b[24mfoo\u001b[0m');
|
assert.equal(chalk.reset(chalk.red.bgGreen.underline('foo') + 'foo'), '\u001b[0m\u001b[4m\u001b[42m\u001b[31mfoo\u001b[39m\u001b[49m\u001b[24mfoo\u001b[0m');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should be able to cache multiple styles', function() {
|
||||||
|
var red = chalk.red;
|
||||||
|
var blue = chalk.blue;
|
||||||
|
var redBold = red.bold;
|
||||||
|
var blueBold = blue.bold;
|
||||||
|
|
||||||
|
assert.notEqual(red('foo'), blue('foo'));
|
||||||
|
assert.notEqual(redBold('bar'), blueBold('bar'));
|
||||||
|
assert.notEqual(blue('baz'), blueBold('baz'));
|
||||||
|
});
|
||||||
|
|
||||||
it('should alias gray to grey', function () {
|
it('should alias gray to grey', function () {
|
||||||
assert.equal(chalk.grey('foo'), '\u001b[90mfoo\u001b[39m');
|
assert.equal(chalk.grey('foo'), '\u001b[90mfoo\u001b[39m');
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue