Skip to content

Commit 0412cdf

Browse files
committed
Minor code improvements
1 parent 249b9ac commit 0412cdf

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

index.js

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ const escapeStringRegexp = require('escape-string-regexp');
33
const ansiStyles = require('ansi-styles');
44
const supportsColor = require('supports-color');
55

6-
const defineProps = Object.defineProperties;
76
const isSimpleWindowsTerm = process.platform === 'win32' && !process.env.TERM.toLowerCase().startsWith('xterm');
87

98
// `supportsColor.level` → `ansiStyles.color[name]` mapping
109
const levelMapping = ['ansi', 'ansi', 'ansi256', 'ansi16m'];
10+
1111
// `color-convert` models to exclude from the Chalk API due to conflicts and such
12-
const skipModels = ['gray'];
12+
const skipModels = new Set(['gray']);
1313

1414
function Chalk(options) {
1515
// Detect level if not set manually
@@ -36,7 +36,7 @@ for (const key of Object.keys(ansiStyles)) {
3636

3737
ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g');
3838
for (const model of Object.keys(ansiStyles.color.ansi)) {
39-
if (skipModels.indexOf(model) !== -1) {
39+
if (skipModels.has(model)) {
4040
continue;
4141
}
4242

@@ -58,11 +58,11 @@ for (const model of Object.keys(ansiStyles.color.ansi)) {
5858

5959
ansiStyles.bgColor.closeRe = new RegExp(escapeStringRegexp(ansiStyles.bgColor.close), 'g');
6060
for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
61-
if (skipModels.indexOf(model) !== -1) {
61+
if (skipModels.has(model)) {
6262
continue;
6363
}
6464

65-
const bgModel = 'bg' + model.charAt(0).toUpperCase() + model.slice(1);
65+
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
6666
styles[bgModel] = {
6767
get() {
6868
const level = this.level;
@@ -79,8 +79,7 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
7979
};
8080
}
8181

82-
// eslint-disable-next-line func-names
83-
const proto = defineProps(() => {}, styles);
82+
const proto = Object.defineProperties(() => {}, styles);
8483

8584
function build(_styles, key) {
8685
const builder = function () {
@@ -127,9 +126,6 @@ function applyStyle() {
127126
return str;
128127
}
129128

130-
const nestedStyles = this._styles;
131-
let i = nestedStyles.length;
132-
133129
// Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
134130
// see https://github.com/chalk/chalk/issues/58
135131
// If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
@@ -138,9 +134,7 @@ function applyStyle() {
138134
ansiStyles.dim.open = '';
139135
}
140136

141-
while (i--) {
142-
const code = nestedStyles[i];
143-
137+
for (const code of this._styles.slice().reverse()) {
144138
// Replace any instances already present with a re-opening code
145139
// otherwise only the part of the string until said closing code
146140
// will be colored, and the rest will simply be 'plain'.
@@ -152,13 +146,13 @@ function applyStyle() {
152146
str = str.replace(/\r?\n/g, `${code.close}$&${code.open}`);
153147
}
154148

155-
// Reset the original 'dim' if we changed it to work around the Windows dimmed gray issue
149+
// Reset the original `dim` if we changed it to work around the Windows dimmed gray issue
156150
ansiStyles.dim.open = originalDim;
157151

158152
return str;
159153
}
160154

161-
defineProps(Chalk.prototype, styles);
155+
Object.defineProperties(Chalk.prototype, styles);
162156

163157
module.exports = new Chalk();
164158
module.exports.styles = ansiStyles;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy