From 3d10f8fad76737fd9443219997d83a24e88dffb0 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 30 Jun 2017 12:42:24 +0200 Subject: [PATCH] Code style tweaks --- .gitignore | 1 + .npmrc | 1 + examples/rainbow.js | 9 ++++----- index.js | 6 +++--- templates.js | 35 +++++++++++------------------------ test.js | 4 ++-- 6 files changed, 22 insertions(+), 34 deletions(-) create mode 100644 .npmrc diff --git a/.gitignore b/.gitignore index 1fd04da..6bff314 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules +yarn.lock coverage .nyc_output diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/examples/rainbow.js b/examples/rainbow.js index 3da1e11..a4a2e0a 100644 --- a/examples/rainbow.js +++ b/examples/rainbow.js @@ -1,3 +1,4 @@ +'use strict'; const chalk = require('..'); const ignoreChars = /[^!-~]/; @@ -23,15 +24,13 @@ function rainbow(str, offset) { return chars.join(''); } -function sleep(ms) { - return new Promise(resolve => setTimeout(resolve, ms)); -} +const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); async function animateString(str) { console.log(); for (let i = 0; i < 360 * 5; i++) { - console.log('\x1b[1F\x1b[G ', rainbow(str, i)); - await sleep(50); // eslint-disable-line no-await-in-loop + console.log('\u001B[1F\u001B[G ', rainbow(str, i)); + await sleep(2); // eslint-disable-line no-await-in-loop } } diff --git a/index.js b/index.js index 7553089..f98f65a 100644 --- a/index.js +++ b/index.js @@ -24,8 +24,8 @@ function applyOptions(obj, options) { } function Chalk(options) { - // We check for this.template here since calling chalk.constructor() - // by itself will have a `this` of a previously constructed chalk object. + // We check for this.template here since calling `chalk.constructor()` + // by itself will have a `this` of a previously constructed chalk object if (!this || !(this instanceof Chalk) || this.template) { const chalk = {}; applyOptions(chalk, options); @@ -142,7 +142,7 @@ function build(_styles, key) { builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey'; // `__proto__` is used because we must return a function, but there is - // no way to create a function with a different prototype. + // no way to create a function with a different prototype builder.__proto__ = proto; // eslint-disable-line no-proto return builder; diff --git a/templates.js b/templates.js index afc6696..6c0a912 100644 --- a/templates.js +++ b/templates.js @@ -27,9 +27,7 @@ const takeWhileReverse = (array, predicate, start) => { return out; }; -/** - * Checks if the character at position i in string is a normal character a.k.a a non control character. - * */ +// Check if the character at position `i` in string is a normal character (non-control character) const isNormalCharacter = (string, i) => { const char = string[i]; const backslash = '\\'; @@ -45,10 +43,8 @@ const isNormalCharacter = (string, i) => { const collectStyles = data => data ? collectStyles(data.parent).concat(data.styles) : ['reset']; -/** - * Computes the style for a given data based on it's style and the style of it's parent. Also accounts for !style styles - * which remove a style from the list if present. - * */ +// Compute the style for a given data based on its style and the style of its parent. +// Also accounts for `!style` styles which remove a style from the list if present. const sumStyles = data => { const negateRegex = /^~.+/; let out = []; @@ -65,13 +61,10 @@ const sumStyles = data => { return out; }; -/** - * Takes a string and parses it into a tree of data objects which inherit styles from their parent. - * */ +// Take a string and parse it into a tree of data objects which inherit styles from their parent function parse(string) { const root = data(null); let pushingStyle = false; - let current = root; for (let i = 0; i < string.length; i++) { @@ -88,7 +81,7 @@ function parse(string) { }; if (pushingStyle) { - if (' \t'.indexOf(char) > -1) { + if (' \t'.includes(char)) { pushingStyle = false; } else if (char === '\n') { pushingStyle = false; @@ -111,16 +104,14 @@ function parse(string) { } if (current !== root) { - throw new Error('literal template has an unclosed block'); + throw new Error('Template literal has an unclosed block'); } return root; } -/** - * Takes a tree of data objects and flattens it to a list of data objects with the inherited and negations styles - * accounted for. - * */ +// Take a tree of data objects and flatten it to a list of data +// objects with the inherited and negations styles accounted for function flatten(data) { let flat = []; @@ -140,13 +131,11 @@ function flatten(data) { function assertStyle(chalk, style) { if (!chalk[style]) { - throw new Error(`invalid Chalk style: ${style}`); + throw new Error(`Invalid Chalk style: ${style}`); } } -/** - * Checks if a given style is valid and parses style functions. - * */ +// Check if a given style is valid and parse style functions function parseStyle(chalk, style) { const fnMatch = style.match(/^\s*(\w+)\s*\(\s*([^)]*)\s*\)\s*/); if (!fnMatch) { @@ -162,9 +151,7 @@ function parseStyle(chalk, style) { return chalk[name].apply(chalk, args); } -/** - * Performs the actual styling of the string, essentially lifted from cli.js. - * */ +// Perform the actual styling of the string function style(chalk, flat) { return flat.map(data => { const fn = data.styles.reduce(parseStyle, chalk); diff --git a/test.js b/test.js index 237e3f3..325950b 100644 --- a/test.js +++ b/test.js @@ -303,7 +303,7 @@ describe('tagged template literal', () => { console.log(ctx`{bold this shouldn't appear ever\}`); assert.fail(); } catch (err) { - assert.equal(err.message, 'literal template has an unclosed block'); + assert.equal(err.message, 'Template literal has an unclosed block'); } }); @@ -313,7 +313,7 @@ describe('tagged template literal', () => { console.log(ctx`{abadstylethatdoesntexist this shouldn't appear ever}`); assert.fail(); } catch (err) { - assert.equal(err.message, 'invalid Chalk style: abadstylethatdoesntexist'); + assert.equal(err.message, 'Invalid Chalk style: abadstylethatdoesntexist'); } });