Skip to content

Commit 3d10f8f

Browse files
committed
Code style tweaks
1 parent 5827081 commit 3d10f8f

File tree

6 files changed

+22
-34
lines changed

6 files changed

+22
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
2+
yarn.lock
23
coverage
34
.nyc_output

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock=false

examples/rainbow.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
'use strict';
12
const chalk = require('..');
23

34
const ignoreChars = /[^!-~]/;
@@ -23,15 +24,13 @@ function rainbow(str, offset) {
2324
return chars.join('');
2425
}
2526

26-
function sleep(ms) {
27-
return new Promise(resolve => setTimeout(resolve, ms));
28-
}
27+
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
2928

3029
async function animateString(str) {
3130
console.log();
3231
for (let i = 0; i < 360 * 5; i++) {
33-
console.log('\x1b[1F\x1b[G ', rainbow(str, i));
34-
await sleep(50); // eslint-disable-line no-await-in-loop
32+
console.log('\u001B[1F\u001B[G ', rainbow(str, i));
33+
await sleep(2); // eslint-disable-line no-await-in-loop
3534
}
3635
}
3736

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ function applyOptions(obj, options) {
2424
}
2525

2626
function Chalk(options) {
27-
// We check for this.template here since calling chalk.constructor()
28-
// by itself will have a `this` of a previously constructed chalk object.
27+
// We check for this.template here since calling `chalk.constructor()`
28+
// by itself will have a `this` of a previously constructed chalk object
2929
if (!this || !(this instanceof Chalk) || this.template) {
3030
const chalk = {};
3131
applyOptions(chalk, options);
@@ -142,7 +142,7 @@ function build(_styles, key) {
142142
builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey';
143143

144144
// `__proto__` is used because we must return a function, but there is
145-
// no way to create a function with a different prototype.
145+
// no way to create a function with a different prototype
146146
builder.__proto__ = proto; // eslint-disable-line no-proto
147147

148148
return builder;

templates.js

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ const takeWhileReverse = (array, predicate, start) => {
2727
return out;
2828
};
2929

30-
/**
31-
* Checks if the character at position i in string is a normal character a.k.a a non control character.
32-
* */
30+
// Check if the character at position `i` in string is a normal character (non-control character)
3331
const isNormalCharacter = (string, i) => {
3432
const char = string[i];
3533
const backslash = '\\';
@@ -45,10 +43,8 @@ const isNormalCharacter = (string, i) => {
4543

4644
const collectStyles = data => data ? collectStyles(data.parent).concat(data.styles) : ['reset'];
4745

48-
/**
49-
* Computes the style for a given data based on it's style and the style of it's parent. Also accounts for !style styles
50-
* which remove a style from the list if present.
51-
* */
46+
// Compute the style for a given data based on its style and the style of its parent.
47+
// Also accounts for `!style` styles which remove a style from the list if present.
5248
const sumStyles = data => {
5349
const negateRegex = /^~.+/;
5450
let out = [];
@@ -65,13 +61,10 @@ const sumStyles = data => {
6561
return out;
6662
};
6763

68-
/**
69-
* Takes a string and parses it into a tree of data objects which inherit styles from their parent.
70-
* */
64+
// Take a string and parse it into a tree of data objects which inherit styles from their parent
7165
function parse(string) {
7266
const root = data(null);
7367
let pushingStyle = false;
74-
7568
let current = root;
7669

7770
for (let i = 0; i < string.length; i++) {
@@ -88,7 +81,7 @@ function parse(string) {
8881
};
8982

9083
if (pushingStyle) {
91-
if (' \t'.indexOf(char) > -1) {
84+
if (' \t'.includes(char)) {
9285
pushingStyle = false;
9386
} else if (char === '\n') {
9487
pushingStyle = false;
@@ -111,16 +104,14 @@ function parse(string) {
111104
}
112105

113106
if (current !== root) {
114-
throw new Error('literal template has an unclosed block');
107+
throw new Error('Template literal has an unclosed block');
115108
}
116109

117110
return root;
118111
}
119112

120-
/**
121-
* Takes a tree of data objects and flattens it to a list of data objects with the inherited and negations styles
122-
* accounted for.
123-
* */
113+
// Take a tree of data objects and flatten it to a list of data
114+
// objects with the inherited and negations styles accounted for
124115
function flatten(data) {
125116
let flat = [];
126117

@@ -140,13 +131,11 @@ function flatten(data) {
140131

141132
function assertStyle(chalk, style) {
142133
if (!chalk[style]) {
143-
throw new Error(`invalid Chalk style: ${style}`);
134+
throw new Error(`Invalid Chalk style: ${style}`);
144135
}
145136
}
146137

147-
/**
148-
* Checks if a given style is valid and parses style functions.
149-
* */
138+
// Check if a given style is valid and parse style functions
150139
function parseStyle(chalk, style) {
151140
const fnMatch = style.match(/^\s*(\w+)\s*\(\s*([^)]*)\s*\)\s*/);
152141
if (!fnMatch) {
@@ -162,9 +151,7 @@ function parseStyle(chalk, style) {
162151
return chalk[name].apply(chalk, args);
163152
}
164153

165-
/**
166-
* Performs the actual styling of the string, essentially lifted from cli.js.
167-
* */
154+
// Perform the actual styling of the string
168155
function style(chalk, flat) {
169156
return flat.map(data => {
170157
const fn = data.styles.reduce(parseStyle, chalk);

test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ describe('tagged template literal', () => {
303303
console.log(ctx`{bold this shouldn't appear ever\}`);
304304
assert.fail();
305305
} catch (err) {
306-
assert.equal(err.message, 'literal template has an unclosed block');
306+
assert.equal(err.message, 'Template literal has an unclosed block');
307307
}
308308
});
309309

@@ -313,7 +313,7 @@ describe('tagged template literal', () => {
313313
console.log(ctx`{abadstylethatdoesntexist this shouldn't appear ever}`);
314314
assert.fail();
315315
} catch (err) {
316-
assert.equal(err.message, 'invalid Chalk style: abadstylethatdoesntexist');
316+
assert.equal(err.message, 'Invalid Chalk style: abadstylethatdoesntexist');
317317
}
318318
});
319319

0 commit comments

Comments
 (0)