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
|
node_modules
|
||||||
|
yarn.lock
|
||||||
coverage
|
coverage
|
||||||
.nyc_output
|
.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 chalk = require('..');
|
||||||
|
|
||||||
const ignoreChars = /[^!-~]/;
|
const ignoreChars = /[^!-~]/;
|
||||||
|
|
@ -23,15 +24,13 @@ function rainbow(str, offset) {
|
||||||
return chars.join('');
|
return chars.join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
function sleep(ms) {
|
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function animateString(str) {
|
async function animateString(str) {
|
||||||
console.log();
|
console.log();
|
||||||
for (let i = 0; i < 360 * 5; i++) {
|
for (let i = 0; i < 360 * 5; i++) {
|
||||||
console.log('\x1b[1F\x1b[G ', rainbow(str, i));
|
console.log('\u001B[1F\u001B[G ', rainbow(str, i));
|
||||||
await sleep(50); // eslint-disable-line no-await-in-loop
|
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) {
|
function Chalk(options) {
|
||||||
// We check for this.template here since calling chalk.constructor()
|
// We check for this.template here since calling `chalk.constructor()`
|
||||||
// by itself will have a `this` of a previously constructed chalk object.
|
// by itself will have a `this` of a previously constructed chalk object
|
||||||
if (!this || !(this instanceof Chalk) || this.template) {
|
if (!this || !(this instanceof Chalk) || this.template) {
|
||||||
const chalk = {};
|
const chalk = {};
|
||||||
applyOptions(chalk, options);
|
applyOptions(chalk, options);
|
||||||
|
|
@ -142,7 +142,7 @@ function build(_styles, key) {
|
||||||
builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey';
|
builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey';
|
||||||
|
|
||||||
// `__proto__` is used because we must return a function, but there is
|
// `__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
|
builder.__proto__ = proto; // eslint-disable-line no-proto
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
|
|
|
||||||
35
templates.js
35
templates.js
|
|
@ -27,9 +27,7 @@ const takeWhileReverse = (array, predicate, start) => {
|
||||||
return out;
|
return out;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
// Check if the character at position `i` in string is a normal character (non-control character)
|
||||||
* Checks if the character at position i in string is a normal character a.k.a a non control character.
|
|
||||||
* */
|
|
||||||
const isNormalCharacter = (string, i) => {
|
const isNormalCharacter = (string, i) => {
|
||||||
const char = string[i];
|
const char = string[i];
|
||||||
const backslash = '\\';
|
const backslash = '\\';
|
||||||
|
|
@ -45,10 +43,8 @@ const isNormalCharacter = (string, i) => {
|
||||||
|
|
||||||
const collectStyles = data => data ? collectStyles(data.parent).concat(data.styles) : ['reset'];
|
const collectStyles = data => data ? collectStyles(data.parent).concat(data.styles) : ['reset'];
|
||||||
|
|
||||||
/**
|
// Compute the style for a given data based on its style and the style of its parent.
|
||||||
* Computes the style for a given data based on it's style and the style of it's parent. Also accounts for !style styles
|
// Also accounts for `!style` styles which remove a style from the list if present.
|
||||||
* which remove a style from the list if present.
|
|
||||||
* */
|
|
||||||
const sumStyles = data => {
|
const sumStyles = data => {
|
||||||
const negateRegex = /^~.+/;
|
const negateRegex = /^~.+/;
|
||||||
let out = [];
|
let out = [];
|
||||||
|
|
@ -65,13 +61,10 @@ const sumStyles = data => {
|
||||||
return out;
|
return out;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
// Take a string and parse it into a tree of data objects which inherit styles from their parent
|
||||||
* Takes a string and parses it into a tree of data objects which inherit styles from their parent.
|
|
||||||
* */
|
|
||||||
function parse(string) {
|
function parse(string) {
|
||||||
const root = data(null);
|
const root = data(null);
|
||||||
let pushingStyle = false;
|
let pushingStyle = false;
|
||||||
|
|
||||||
let current = root;
|
let current = root;
|
||||||
|
|
||||||
for (let i = 0; i < string.length; i++) {
|
for (let i = 0; i < string.length; i++) {
|
||||||
|
|
@ -88,7 +81,7 @@ function parse(string) {
|
||||||
};
|
};
|
||||||
|
|
||||||
if (pushingStyle) {
|
if (pushingStyle) {
|
||||||
if (' \t'.indexOf(char) > -1) {
|
if (' \t'.includes(char)) {
|
||||||
pushingStyle = false;
|
pushingStyle = false;
|
||||||
} else if (char === '\n') {
|
} else if (char === '\n') {
|
||||||
pushingStyle = false;
|
pushingStyle = false;
|
||||||
|
|
@ -111,16 +104,14 @@ function parse(string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (current !== root) {
|
if (current !== root) {
|
||||||
throw new Error('literal template has an unclosed block');
|
throw new Error('Template literal has an unclosed block');
|
||||||
}
|
}
|
||||||
|
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Take a tree of data objects and flatten it to a list of data
|
||||||
* Takes a tree of data objects and flattens it to a list of data objects with the inherited and negations styles
|
// objects with the inherited and negations styles accounted for
|
||||||
* accounted for.
|
|
||||||
* */
|
|
||||||
function flatten(data) {
|
function flatten(data) {
|
||||||
let flat = [];
|
let flat = [];
|
||||||
|
|
||||||
|
|
@ -140,13 +131,11 @@ function flatten(data) {
|
||||||
|
|
||||||
function assertStyle(chalk, style) {
|
function assertStyle(chalk, style) {
|
||||||
if (!chalk[style]) {
|
if (!chalk[style]) {
|
||||||
throw new Error(`invalid Chalk style: ${style}`);
|
throw new Error(`Invalid Chalk style: ${style}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Check if a given style is valid and parse style functions
|
||||||
* Checks if a given style is valid and parses style functions.
|
|
||||||
* */
|
|
||||||
function parseStyle(chalk, style) {
|
function parseStyle(chalk, style) {
|
||||||
const fnMatch = style.match(/^\s*(\w+)\s*\(\s*([^)]*)\s*\)\s*/);
|
const fnMatch = style.match(/^\s*(\w+)\s*\(\s*([^)]*)\s*\)\s*/);
|
||||||
if (!fnMatch) {
|
if (!fnMatch) {
|
||||||
|
|
@ -162,9 +151,7 @@ function parseStyle(chalk, style) {
|
||||||
return chalk[name].apply(chalk, args);
|
return chalk[name].apply(chalk, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Perform the actual styling of the string
|
||||||
* Performs the actual styling of the string, essentially lifted from cli.js.
|
|
||||||
* */
|
|
||||||
function style(chalk, flat) {
|
function style(chalk, flat) {
|
||||||
return flat.map(data => {
|
return flat.map(data => {
|
||||||
const fn = data.styles.reduce(parseStyle, chalk);
|
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\}`);
|
console.log(ctx`{bold this shouldn't appear ever\}`);
|
||||||
assert.fail();
|
assert.fail();
|
||||||
} catch (err) {
|
} 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}`);
|
console.log(ctx`{abadstylethatdoesntexist this shouldn't appear ever}`);
|
||||||
assert.fail();
|
assert.fail();
|
||||||
} catch (err) {
|
} 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