Merge pull request #27 from jbnicolai/optimize
Performance optimizations (ca. Factor 75)
This commit is contained in:
commit
135852fd92
1 changed files with 20 additions and 20 deletions
40
index.js
40
index.js
|
|
@ -26,32 +26,32 @@ var styles = (function () {
|
||||||
return ret;
|
return ret;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
function applyStyle() {
|
||||||
|
// support varags, but simply cast to string in case there's only one arg
|
||||||
|
var str = arguments.length === 1 ? arguments[0] + '' : [].slice.call(arguments).join(' ');
|
||||||
|
|
||||||
|
if (!chalk.enabled || !str) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
return applyStyle._styles.reduce(function (str, name) {
|
||||||
|
var code = ansiStyles[name];
|
||||||
|
// Replace any instances already present with a re-opening code
|
||||||
|
// otherwise only the part of the string until said closing code
|
||||||
|
// will be colored, and the rest will simply be 'plain'.
|
||||||
|
return code.open + str.replace(code.closeRe, code.open) + code.close;
|
||||||
|
}, str) ;
|
||||||
|
}
|
||||||
|
|
||||||
function init() {
|
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 () {
|
||||||
var obj = defineProps(function self() {
|
style._styles = [];
|
||||||
var str = [].slice.call(arguments).join(' ');
|
return style[name];
|
||||||
|
|
||||||
if (!chalk.enabled) {
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
return self._styles.reduce(function (str, name) {
|
|
||||||
var code = ansiStyles[name];
|
|
||||||
return str ? code.open +
|
|
||||||
// Replace any instances already present with a re-opening code
|
|
||||||
// otherwise only the part of the string until said closing code
|
|
||||||
// will be colored, and the rest will simply be 'plain'.
|
|
||||||
str.replace(code.closeRe, code.open) + code.close : '';
|
|
||||||
}, str);
|
|
||||||
}, styles);
|
|
||||||
|
|
||||||
obj._styles = [];
|
|
||||||
|
|
||||||
return obj[name];
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue