[UPDATE] simplify some process

This commit is contained in:
Maxime Allanic 2020-04-27 15:10:48 +02:00
parent 6e1f707083
commit 510873c4fd
No known key found for this signature in database
GPG key ID: AA17A3D7C7B6F14D
2 changed files with 8 additions and 9 deletions

View file

@ -58,7 +58,7 @@ function Chalk(options) {
for (const [styleName, style] of Object.entries(ansiStyles)) { for (const [styleName, style] of Object.entries(ansiStyles)) {
styles[styleName] = { styles[styleName] = {
get() { get() {
const builder = createBuilder(this, createStyler(style.open, style.close, this._styler), this._isEmpty, styleName); const builder = createBuilder(this, createStyler(style.open, style.close, this._styler), this._isEmpty);
Object.defineProperty(this, styleName, {value: builder}); Object.defineProperty(this, styleName, {value: builder});
return builder; return builder;
} }
@ -81,7 +81,7 @@ for (const model of usedModels) {
const {level} = this; const {level} = this;
return function (...arguments_) { return function (...arguments_) {
const styler = createStyler(ansiStyles.color[levelMapping[level]][model](...arguments_), ansiStyles.color.close, this._styler); const styler = createStyler(ansiStyles.color[levelMapping[level]][model](...arguments_), ansiStyles.color.close, this._styler);
return createBuilder(this, styler, this._isEmpty, model); return createBuilder(this, styler, this._isEmpty);
}; };
} }
}; };
@ -94,7 +94,7 @@ for (const model of usedModels) {
const {level} = this; const {level} = this;
return function (...arguments_) { return function (...arguments_) {
const styler = createStyler(ansiStyles.bgColor[levelMapping[level]][model](...arguments_), ansiStyles.bgColor.close, this._styler); const styler = createStyler(ansiStyles.bgColor[levelMapping[level]][model](...arguments_), ansiStyles.bgColor.close, this._styler);
return createBuilder(this, styler, this._isEmpty, bgModel); return createBuilder(this, styler, this._isEmpty);
}; };
} }
}; };
@ -133,7 +133,7 @@ const createStyler = (open, close, parent) => {
}; };
}; };
const createBuilder = (self, _styler, _isEmpty, styleName) => { const createBuilder = (self, _styler, _isEmpty) => {
const builder = (...arguments_) => { const builder = (...arguments_) => {
if (arguments_.length === 1) { if (arguments_.length === 1) {
// Single argument is hot path, implicit coercion is faster than anything // Single argument is hot path, implicit coercion is faster than anything
@ -150,7 +150,7 @@ const createBuilder = (self, _styler, _isEmpty, styleName) => {
} }
arguments_ = arguments_.slice(1); arguments_ = arguments_.slice(1);
const parts = ['{' + styleName + ' ', firstString.raw[0]]; const parts = [firstString.raw[0]];
for (let i = 1; i < firstString.length; i++) { for (let i = 1; i < firstString.length; i++) {
parts.push( parts.push(
@ -159,13 +159,11 @@ const createBuilder = (self, _styler, _isEmpty, styleName) => {
); );
} }
parts.push('}');
if (template === undefined) { if (template === undefined) {
template = require('./templates'); template = require('./templates');
} }
return template(chalk, parts.join('')); return _styler.openAll + template(chalk, parts.join('')) + _styler.closeAll;
}; };
// We alter the prototype because we must return a function, but there is // We alter the prototype because we must return a function, but there is

View file

@ -181,5 +181,6 @@ test('should support nested calls', t => {
const name = 'Sindre'; const name = 'Sindre';
const exclamation = 'Neat'; const exclamation = 'Neat';
const result = instance.bold`Hello, {cyan.inverse ${name}!} This is a test. {green ${exclamation}!}`; const result = instance.bold`Hello, {cyan.inverse ${name}!} This is a test. {green ${exclamation}!}`;
t.is(result, '\u001B[1mHello, \u001B[22m\u001B[1m\u001B[36m\u001B[7mSindre!\u001B[27m\u001B[39m\u001B[22m\u001B[1m This is a test. \u001B[22m\u001B[1m\u001B[32mNeat!\u001B[39m\u001B[22m');
t.is(result, '\u001B[1mHello, \u001B[36m\u001B[7mSindre!\u001B[27m\u001B[39m This is a test. \u001B[32mNeat!\u001B[39m\u001B[22m');
}); });