forked from orbit-oss/chalk
Code style tweaks
This commit is contained in:
parent
5827081719
commit
3d10f8fad7
6 changed files with 22 additions and 34 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,3 +1,4 @@
|
|||
node_modules
|
||||
yarn.lock
|
||||
coverage
|
||||
.nyc_output
|
||||
|
|
|
|||
1
.npmrc
Normal file
1
.npmrc
Normal file
|
|
@ -0,0 +1 @@
|
|||
package-lock=false
|
||||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
6
index.js
6
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;
|
||||
|
|
|
|||
35
templates.js
35
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);
|
||||
|
|
|
|||
4
test.js
4
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');
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue