Skip to content

Commit

Permalink
Feat(exporter-js): Normalize color string to short hex
Browse files Browse the repository at this point in the history
      * `normalizeColor` can now produce both short and long hex colors base on the flag
  • Loading branch information
literat committed Apr 19, 2024
1 parent becea7a commit 56a65f0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
2 changes: 1 addition & 1 deletion exporters/js/exporter.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"author": "Jan Kryšpín <[email protected]>",
"organization": "LMC s.r.o.",
"source_dir": "src",
"version": "1.0.0",
"version": "1.1.0",
"usesBrands": true,
"config": {
"sources": "sources.json",
Expand Down
40 changes: 20 additions & 20 deletions exporters/js/generated/functions.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions exporters/js/src/js/formatters/__tests__/color.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ describe('formatColor', () => {
it.each([
// name, expected
[{ a: 125, hex: '123456' }, '#123456'],
[{ a: 563, hex: 'fff' }, '#fff'],
])('should format color', (color, expected) => {
expect(formatColor(color)).toBe(expected);
});
Expand Down
3 changes: 3 additions & 0 deletions exporters/js/src/js/normalizers/__tests__/color.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ describe('normalizeColor', () => {
it.each([
// name, expected
['123456', '123456'],
['fff', 'fff'],
['ffffff', 'fff'],
['ffffff00', 'fff0'],
])('should normalize color', (color, expected) => {
expect(normalizeColor(color)).toBe(expected);
});
Expand Down
24 changes: 20 additions & 4 deletions exporters/js/src/js/normalizers/color.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
export function normalizeColor(color: string): string {
const COLOR_HEX_LENGTH = {
SHORT: 'short',
LONG: 'long',
};

type ColorHexLength = (typeof COLOR_HEX_LENGTH)[keyof typeof COLOR_HEX_LENGTH];

export function normalizeColor(color: string, colorHexLength: ColorHexLength = COLOR_HEX_LENGTH.SHORT): string {
const colorParts = color.match(/.{1,2}/g);
let shortHex = true;
let shortHex = colorHexLength === COLOR_HEX_LENGTH.SHORT;
colorParts &&
colorParts.forEach((part) => {
if (shortHex) {
if (colorHexLength === COLOR_HEX_LENGTH.SHORT) {
shortHex = /^(.)\1+$/.test(part);
}
});

if (shortHex) {
return `${color.substring(0, 1)}${color.substring(2, 3)}${color.substring(4, 5)}`;
return `${color.substring(0, 1)}${color.substring(2, 3)}${color.substring(4, 5)}${color.substring(6, 7)}`;
}

if (colorHexLength === COLOR_HEX_LENGTH.LONG && color.length === 3) {
// Convert short hex to long hex
// @see: https://www.30secondsofcode.org/js/s/extend-hex/
return color
.split('')
.map((x) => x + x)
.join('');
}

return color;
Expand Down

0 comments on commit 56a65f0

Please sign in to comment.