chalk-pipe
Version:
Create chalk style schemes with simpler style strings
46 lines (45 loc) • 1.52 kB
JavaScript
import chalk, { modifierNames, colorNames, } from 'chalk';
import { cssKeywordsMap } from './styles.js';
import { normalizeHexColor } from './utils.js';
const builtInStyles = new Set([...modifierNames, ...colorNames]);
const chalkPipe = (stylePipe, customChalk) => {
/* c8 ignore start */
// eslint-disable-next-line n/no-unsupported-features/es-syntax
let paint = customChalk ?? chalk;
/* c8 ignore stop */
if (!stylePipe || stylePipe.length === 0) {
return paint;
}
const styles = stylePipe.split('.');
for (let style of styles) {
let isBg = false;
// Built-in styles
if (builtInStyles.has(style)) {
paint = paint[style];
continue;
}
// Background
if (style.startsWith('bg')) {
style = style.slice(2);
style = style.slice(0, 1).toLowerCase() + style.slice(1);
isBg = true;
}
// Keyword
if (cssKeywordsMap.has(style)) {
const colorHex = cssKeywordsMap.get(style);
paint = isBg ? paint.bgHex(colorHex) : paint.hex(colorHex);
continue;
}
// Hex
if (/^#?[a-f\d]{3,8}$/i.test(style)) {
style = normalizeHexColor(style);
paint = isBg ? paint.bgHex(style) : paint.hex(style);
continue;
}
}
return paint;
};
export { default as chalk } from 'chalk';
export * from 'chalk';
export { keywordNames } from './styles.js';
export default chalkPipe;