Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
LitoMore committed Oct 7, 2024
1 parent a7015ef commit 1ef2953
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 29 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
"homepage": "https://github.com/LitoMore/chalk-pipe#readme",
"devDependencies": {
"@sindresorhus/tsconfig": "^6.0.0",
"@types/node": "^22.4.0",
"@types/node": "^22.7.4",
"ava": "^6.1.3",
"c8": "^10.1.2",
"del-cli": "^5.1.0",
"typescript": "^5.5.4",
"typescript": "^5.6.2",
"xo": "^0.59.3"
},
"dependencies": {
Expand Down
31 changes: 8 additions & 23 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,10 @@ import chalk, {
modifierNames,
colorNames,
} from 'chalk';
import {type KeywordName, cssKeywords} from './styles.js';
import {cssKeywordsMap} from './styles.js';
import {normalizeHexColor} from './utils.js';

const isBuiltInStyle = (style: string) => {
return ([...modifierNames, ...colorNames] as string[]).includes(style);
};

const isBackground = (style: string) => {
return style.startsWith('bg');
};

const isHexColor = (style: string) => {
return /^#?[a-f\d]{3,8}$/i.test(style);
};

const isKeyword = (style: string) => {
return style in cssKeywords;
};
const builtInStyles = new Set<string>([...modifierNames, ...colorNames]);

const chalkPipe = (stylePipe?: string, customChalk?: ChalkInstance) => {
/* c8 ignore start */
Expand All @@ -40,28 +26,27 @@ const chalkPipe = (stylePipe?: string, customChalk?: ChalkInstance) => {
let isBg = false;

// Built-in styles
if (isBuiltInStyle(style)) {
if (builtInStyles.has(style)) {
paint = paint[style as ModifierName | ColorName];
continue;
}

// Background
if (isBackground(style)) {
if (style.startsWith('bg')) {
style = style.slice(2);
style = style.slice(0, 1).toLowerCase() + style.slice(1);
isBg = true;
}

// Keyword
if (isKeyword(style)) {
paint = isBg
? paint.bgHex(cssKeywords[style as KeywordName])
: paint.hex(cssKeywords[style as KeywordName]);
if (cssKeywordsMap.has(style)) {
const colorHex = cssKeywordsMap.get(style)!;
paint = isBg ? paint.bgHex(colorHex) : paint.hex(colorHex);
continue;
}

// Hex
if (isHexColor(style)) {
if (/^#?[a-f\d]{3,8}$/i.test(style)) {
style = normalizeHexColor(style);
paint = isBg ? paint.bgHex(style) : paint.hex(style);
continue;
Expand Down
4 changes: 3 additions & 1 deletion source/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ export const cssKeywords = {
whitesmoke: '#f5f5f5',
yellow: '#ffff00',
yellowgreen: '#9acd32',
} as const;
};

export const cssKeywordsMap = new Map(Object.entries(cssKeywords));

/**
[CSS keyword](https://www.w3.org/wiki/CSS/Properties/color/keywords) names.
Expand Down
6 changes: 3 additions & 3 deletions source/test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import {cssKeywords} from './styles.js';
import {cssKeywordsMap} from './styles.js';
import {normalizeHexColor} from './utils.js';
import chalkPipe, {
Chalk,
Expand Down Expand Up @@ -67,7 +67,7 @@ test('Built-in background colors', (t) => {
test('Keyword styles', (t) => {
const scheme = chalkPipe('hotpink', chalk);
const text = scheme('hug');
const expected = chalk.hex(cssKeywords.hotpink)('hug');
const expected = chalk.hex(cssKeywordsMap.get('hotpink')!)('hug');
t.is(text, expected);
});

Expand All @@ -76,7 +76,7 @@ test('Background styles', (t) => {
const text = scheme('unicorn');
const expected = chalk.bgRed
.bgHex('#ff99cc')
.bgHex(cssKeywords.pink)
.bgHex(cssKeywordsMap.get('pink')!)
.bgBlackBright('unicorn');
t.is(text, expected);
});
Expand Down

0 comments on commit 1ef2953

Please sign in to comment.