fix: menu-badge color not correct (#4089)

This commit is contained in:
Li Kui
2024-08-09 10:52:47 +08:00
committed by GitHub
parent 3f04f6b01f
commit de90b07b6f
3 changed files with 43 additions and 8 deletions

View File

@@ -1,6 +1,11 @@
import { describe, expect, it } from 'vitest';
import { convertToHsl, convertToHslCssVar, convertToRgb } from './convert';
import {
convertToHsl,
convertToHslCssVar,
convertToRgb,
isValidColor,
} from './convert';
describe('color conversion functions', () => {
it('should correctly convert color to HSL format', () => {
@@ -39,3 +44,15 @@ describe('color conversion functions', () => {
expect(convertToRgb(color)).toEqual(expectedRgba);
});
});
describe('isValidColor', () => {
it('isValidColor function', () => {
// 测试有效颜色
expect(isValidColor('blue')).toBe(true);
expect(isValidColor('#000000')).toBe(true);
// 测试无效颜色
expect(isValidColor('invalid color')).toBe(false);
expect(isValidColor()).toBe(false);
});
});

View File

@@ -41,4 +41,22 @@ function convertToRgb(str: string): string {
return new TinyColor(str.replaceAll(/deg|grad|rad|turn/g, '')).toRgbString();
}
export { convertToHsl, convertToHslCssVar, convertToRgb, TinyColor };
/**
* 检查颜色是否有效
* @param {string} color - 待检查的颜色
* 如果颜色有效返回true否则返回false
*/
function isValidColor(color?: string) {
if (!color) {
return false;
}
return new TinyColor(color).isValid;
}
export {
convertToHsl,
convertToHslCssVar,
convertToRgb,
isValidColor,
TinyColor,
};