Skip to content

Commit

Permalink
feat(helper): add getColorType func
Browse files Browse the repository at this point in the history
  • Loading branch information
nmsn committed Sep 7, 2022
1 parent 2bce4be commit 1f8798f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/__tests__/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { removeRgbaBlank } from "../utils/helper";
import { removeRgbaBlank, getColorType } from "../utils/helper";

describe("helper", () => {
it("helper", () => {
expect(removeRgbaBlank('rgba(0, 0, 0, 1)')).toBe('rgba(0,0,0,1)');
});
});

it("getColorType", () => {
expect(getColorType("rgba(255,255,255,1)")).toBe('rgba');
expect(getColorType("rgba(0,0,0,0)")).toBe('rgba');
expect(getColorType("rgb(255,255,255)")).toBe('rgb');
expect(getColorType("rgb(0,0,0)")).toBe('rgb');
expect(getColorType('#ffffffff')).toBe('hex');
expect(getColorType('#ffffff')).toBe('hex');
expect(getColorType('#fff')).toBe('hex');
})
});
10 changes: 10 additions & 0 deletions src/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import { isHex, isRgba, isRgb } from "./validator";

export const removeRgbaBlank = (color: string) => {
return color.replace(/\s+/g, "");
};

export const getColorType = (color: string) => {
if (isHex(color)) return "hex";
if (isRgb(color)) return "rgb";
if (isRgba(color)) return "rgba";

return "";
};

0 comments on commit 1f8798f

Please sign in to comment.