From fb3aa8e234c99dc34031a16b224f4f8ebdde5aaf Mon Sep 17 00:00:00 2001 From: Steve Mao Date: Tue, 5 Jan 2016 21:00:49 +1100 Subject: [PATCH 1/2] remove iffy to gain performance --- index.js | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index d2ec92d..5714447 100644 --- a/index.js +++ b/index.js @@ -15,21 +15,17 @@ if (isSimpleWindowsTerm) { ansiStyles.blue.open = '\u001b[94m'; } -var styles = (function () { - var ret = {}; +var styles = {}; - Object.keys(ansiStyles).forEach(function (key) { - ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g'); +Object.keys(ansiStyles).forEach(function (key) { + ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g'); - ret[key] = { - get: function () { - return build.call(this, this._styles.concat(key)); - } - }; - }); - - return ret; -})(); + styles[key] = { + get: function () { + return build.call(this, this._styles.concat(key)); + } + }; +}); var proto = defineProps(function chalk() {}, styles); From 943a283fe7279a79bf7076225198a7b7987e1a43 Mon Sep 17 00:00:00 2001 From: Steve Mao Date: Tue, 5 Jan 2016 23:03:55 +1100 Subject: [PATCH 2/2] merge two almost the same snippets --- index.js | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index 5714447..f2377a2 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,7 @@ Object.keys(ansiStyles).forEach(function (key) { styles[key] = { get: function () { - return build.call(this, this._styles.concat(key)); + return build.call(this, this._styles ? this._styles.concat(key) : [key]); } }; }); @@ -87,21 +87,7 @@ function applyStyle() { return str; } -function init() { - var ret = {}; - - Object.keys(styles).forEach(function (name) { - ret[name] = { - get: function () { - return build.call(this, [name]); - } - }; - }); - - return ret; -} - -defineProps(Chalk.prototype, init()); +defineProps(Chalk.prototype, styles); module.exports = new Chalk(); module.exports.styles = ansiStyles;