From f8a3642a8107f6029c6923b72a43c35a1065a336 Mon Sep 17 00:00:00 2001 From: Richie Bendall Date: Thu, 22 Apr 2021 20:03:48 +1200 Subject: [PATCH] Minor tweaks (#437) Co-authored-by: Qix --- index.d.ts | 2 +- package.json | 2 +- source/index.js | 30 +++++++++++++++++------------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/index.d.ts b/index.d.ts index a21fa89..6f7ff2e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -223,7 +223,7 @@ export interface ChalkInstance extends ChalkFunction { readonly bold: this; /** - Modifier: Emitting only a small amount of light. + Modifier: Make text slightly darker. (Inconsistent across terminals; might do nothing) */ readonly dim: this; diff --git a/package.json b/package.json index 2509b62..05cbf72 100644 --- a/package.json +++ b/package.json @@ -54,7 +54,7 @@ "matcha": "^0.7.0", "nyc": "^15.1.0", "tsd": "^0.14.0", - "xo": "^0.38.2", + "xo": "^0.39.1", "yoctodelay": "^1.2.0" }, "xo": { diff --git a/source/index.js b/source/index.js index 70f7a5d..8a3785b 100644 --- a/source/index.js +++ b/source/index.js @@ -9,6 +9,10 @@ import template from './templates.js'; const {stdout: stdoutColor, stderr: stderrColor} = supportsColor; const {isArray} = Array; +const GENERATOR = Symbol('GENERATOR'); +const STYLER = Symbol('STYLER'); +const IS_EMPTY = Symbol('IS_EMPTY'); + // `supportsColor.level` → `ansiStyles.color[name]` mapping const levelMapping = [ 'ansi', @@ -59,7 +63,7 @@ Object.setPrototypeOf(createChalk.prototype, Function.prototype); for (const [styleName, style] of Object.entries(ansiStyles)) { styles[styleName] = { get() { - const builder = createBuilder(this, createStyler(style.open, style.close, this._styler), this._isEmpty); + const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]); Object.defineProperty(this, styleName, {value: builder}); return builder; } @@ -68,7 +72,7 @@ for (const [styleName, style] of Object.entries(ansiStyles)) { styles.visible = { get() { - const builder = createBuilder(this, this._styler, true); + const builder = createBuilder(this, this[STYLER], true); Object.defineProperty(this, 'visible', {value: builder}); return builder; } @@ -101,8 +105,8 @@ for (const model of usedModels) { get() { const {level} = this; return function (...arguments_) { - const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this._styler); - return createBuilder(this, styler, this._isEmpty); + const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this[STYLER]); + return createBuilder(this, styler, this[IS_EMPTY]); }; } }; @@ -112,8 +116,8 @@ for (const model of usedModels) { get() { const {level} = this; return function (...arguments_) { - const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this._styler); - return createBuilder(this, styler, this._isEmpty); + const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this[STYLER]); + return createBuilder(this, styler, this[IS_EMPTY]); }; } }; @@ -124,10 +128,10 @@ const proto = Object.defineProperties(() => {}, { level: { enumerable: true, get() { - return this._generator.level; + return this[GENERATOR].level; }, set(level) { - this._generator.level = level; + this[GENERATOR].level = level; } } }); @@ -168,19 +172,19 @@ const createBuilder = (self, _styler, _isEmpty) => { // no way to create a function with a different prototype Object.setPrototypeOf(builder, proto); - builder._generator = self; - builder._styler = _styler; - builder._isEmpty = _isEmpty; + builder[GENERATOR] = self; + builder[STYLER] = _styler; + builder[IS_EMPTY] = _isEmpty; return builder; }; const applyStyle = (self, string) => { if (self.level <= 0 || !string) { - return self._isEmpty ? '' : string; + return self[IS_EMPTY] ? '' : string; } - let styler = self._styler; + let styler = self[STYLER]; if (styler === undefined) { return string;