Apr. (#5)
* Fix XO linting and update some dev dependencies Fixes #275 * Add some badges to the readme * Tiny travis.yml tweak * Require Node.js 6 * Validate the `level` option Fixes #248 * Add failing test for #234 (#235) * Add type definitions badge (#286) * Add Tidelift mention in the readme * Replace RawGit URL Fixes #305 * Fix ignore chars regex flags in rainbow example (#306) Use global matches rather than stopping after the first match. * Strict mode in Flow definition (#309) * Add security section * Add docs comments and tests for TypeScript definitions (#299) Fixes #293 * Update dependencies and meta tweaks * Type definition improvements * Enforce `chalk.constructor` to be called with `new` in TypeScript * Add extra level/enabled property info in the readme (#308) * Code style tweaks * Change tagged template literal argument type to accept `unknown` instead of just `string` (#316)
This commit is contained in:
parent
9776a2ae5b
commit
029b69e482
26 changed files with 760 additions and 479 deletions
42
templates.js
42
templates.js
|
|
@ -31,10 +31,11 @@ function parseArguments(name, args) {
|
|||
let matches;
|
||||
|
||||
for (const chunk of chunks) {
|
||||
if (!isNaN(chunk)) {
|
||||
results.push(Number(chunk));
|
||||
const number = Number(chunk);
|
||||
if (!Number.isNaN(number)) {
|
||||
results.push(number);
|
||||
} else if ((matches = chunk.match(STRING_REGEX))) {
|
||||
results.push(matches[2].replace(ESCAPE_REGEX, (m, escape, chr) => escape ? unescape(escape) : chr));
|
||||
results.push(matches[2].replace(ESCAPE_REGEX, (m, escape, character) => escape ? unescape(escape) : character));
|
||||
} else {
|
||||
throw new Error(`Invalid Chalk template style argument: ${chunk} (in style '${name}')`);
|
||||
}
|
||||
|
|
@ -50,7 +51,7 @@ function parseStyle(style) {
|
|||
let matches;
|
||||
|
||||
while ((matches = STYLE_REGEX.exec(style)) !== null) {
|
||||
const name = matches[1];
|
||||
const name = matches[1]; // eslint-disable-line prefer-destructuring
|
||||
|
||||
if (matches[2]) {
|
||||
const args = parseArguments(name, matches[2]);
|
||||
|
|
@ -73,17 +74,20 @@ function buildStyle(chalk, styles) {
|
|||
}
|
||||
|
||||
let current = chalk;
|
||||
// TODO: Use `Object.entries` when targeting Node.js 8
|
||||
for (const styleName of Object.keys(enabled)) {
|
||||
if (Array.isArray(enabled[styleName])) {
|
||||
if (!(styleName in current)) {
|
||||
throw new Error(`Unknown Chalk style: ${styleName}`);
|
||||
}
|
||||
if (!Array.isArray(enabled[styleName])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (enabled[styleName].length > 0) {
|
||||
current = current[styleName].apply(current, enabled[styleName]);
|
||||
} else {
|
||||
current = current[styleName];
|
||||
}
|
||||
if (!(styleName in current)) {
|
||||
throw new Error(`Unknown Chalk style: ${styleName}`);
|
||||
}
|
||||
|
||||
if (enabled[styleName].length > 0) {
|
||||
current = current[styleName](...enabled[styleName]);
|
||||
} else {
|
||||
current = current[styleName];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -96,13 +100,13 @@ module.exports = (chalk, tmp) => {
|
|||
let chunk = [];
|
||||
|
||||
// eslint-disable-next-line max-params
|
||||
tmp.replace(TEMPLATE_REGEX, (m, escapeChar, inverse, style, close, chr) => {
|
||||
if (escapeChar) {
|
||||
chunk.push(unescape(escapeChar));
|
||||
tmp.replace(TEMPLATE_REGEX, (m, escapeCharacter, inverse, style, close, character) => {
|
||||
if (escapeCharacter) {
|
||||
chunk.push(unescape(escapeCharacter));
|
||||
} else if (style) {
|
||||
const str = chunk.join('');
|
||||
const string = chunk.join('');
|
||||
chunk = [];
|
||||
chunks.push(styles.length === 0 ? str : buildStyle(chalk, styles)(str));
|
||||
chunks.push(styles.length === 0 ? string : buildStyle(chalk, styles)(string));
|
||||
styles.push({inverse, styles: parseStyle(style)});
|
||||
} else if (close) {
|
||||
if (styles.length === 0) {
|
||||
|
|
@ -113,7 +117,7 @@ module.exports = (chalk, tmp) => {
|
|||
chunk = [];
|
||||
styles.pop();
|
||||
} else {
|
||||
chunk.push(chr);
|
||||
chunk.push(character);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue