Skip to content

Commit a001090

Browse files
authored
feat: provide enums for color values (#1910)
1 parent b584038 commit a001090

File tree

4 files changed

+52
-41
lines changed

4 files changed

+52
-41
lines changed

src/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,17 @@ export * as allLocales from './locales';
4040
export { Aircraft, AircraftType } from './modules/airline';
4141
export type { AirlineModule } from './modules/airline';
4242
export type { AnimalModule } from './modules/animal';
43+
export { CssFunction, CssSpace } from './modules/color';
4344
export type {
4445
Casing,
4546
ColorFormat,
4647
ColorModule,
47-
CSSFunction,
48-
CSSSpace,
48+
/** @deprecated Use CssFunctionType instead */
49+
CssFunctionType as CSSFunction,
50+
CssFunctionType,
51+
/** @deprecated Use CssSpaceType instead */
52+
CssSpaceType as CSSSpace,
53+
CssSpaceType,
4954
NumberColorFormat,
5055
StringColorFormat,
5156
} from './modules/color';

src/modules/color/index.ts

+40-33
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,39 @@ import type { Faker } from '../../faker';
33
/**
44
* Color space names supported by CSS.
55
*/
6-
export const CSS_SPACES = [
7-
'sRGB',
8-
'display-p3',
9-
'rec2020',
10-
'a98-rgb',
11-
'prophoto-rgb',
12-
'rec2020',
13-
] as const;
6+
export enum CssSpace {
7+
SRGB = 'sRGB',
8+
DisplayP3 = 'display-p3',
9+
REC2020 = 'rec2020',
10+
A98RGB = 'a98-rgb',
11+
ProphotoRGB = 'prophoto-rgb',
12+
}
13+
14+
/**
15+
* Color space names supported by CSS.
16+
*/
17+
export type CssSpaceType = `${CssSpace}`;
1418

1519
/**
1620
* Functions supported by CSS to produce color.
1721
*/
18-
export const CSS_FUNCTIONS = [
19-
'rgb',
20-
'rgba',
21-
'hsl',
22-
'hsla',
23-
'hwb',
24-
'cmyk',
25-
'lab',
26-
'lch',
27-
'color',
28-
] as const;
29-
30-
export type CSSFunction = (typeof CSS_FUNCTIONS)[number];
31-
export type CSSSpace = (typeof CSS_SPACES)[number];
22+
export enum CssFunction {
23+
RGB = 'rgb',
24+
RGBA = 'rgba',
25+
HSL = 'hsl',
26+
HSLA = 'hsla',
27+
HWB = 'hwb',
28+
CMYK = 'cmyk',
29+
LAB = 'lab',
30+
LCH = 'lch',
31+
COLOR = 'color',
32+
}
33+
34+
/**
35+
* Functions supported by CSS to produce color.
36+
*/
37+
export type CssFunctionType = `${CssFunction}`;
38+
3239
export type StringColorFormat = 'css' | 'binary';
3340
export type NumberColorFormat = 'decimal';
3441
export type ColorFormat = StringColorFormat | NumberColorFormat;
@@ -95,8 +102,8 @@ function toBinary(values: number[]): string {
95102
*/
96103
function toCSS(
97104
values: number[],
98-
cssFunction: CSSFunction = 'rgb',
99-
space: CSSSpace = 'sRGB'
105+
cssFunction: CssFunctionType = 'rgb',
106+
space: CssSpaceType = 'sRGB'
100107
): string {
101108
const percentage = (value: number) => Math.round(value * 100);
102109
switch (cssFunction) {
@@ -141,8 +148,8 @@ function toCSS(
141148
function toColorFormat(
142149
values: number[],
143150
format: ColorFormat,
144-
cssFunction: CSSFunction = 'rgb',
145-
space: CSSSpace = 'sRGB'
151+
cssFunction: CssFunctionType = 'rgb',
152+
space: CssSpaceType = 'sRGB'
146153
): string | number[] {
147154
switch (format) {
148155
case 'css':
@@ -205,7 +212,7 @@ export class ColorModule {
205212
* @since 7.0.0
206213
*/
207214
cssSupportedFunction(): string {
208-
return this.faker.helpers.arrayElement(CSS_FUNCTIONS);
215+
return this.faker.helpers.objectValue(CssFunction);
209216
}
210217

211218
/**
@@ -217,7 +224,7 @@ export class ColorModule {
217224
* @since 7.0.0
218225
*/
219226
cssSupportedSpace(): string {
220-
return this.faker.helpers.arrayElement(CSS_SPACES);
227+
return this.faker.helpers.objectValue(CssSpace);
221228
}
222229

223230
/**
@@ -367,7 +374,7 @@ export class ColorModule {
367374
} = options || {};
368375
options = { format, includeAlpha, prefix, casing };
369376
let color: string | number[];
370-
let cssFunction: CSSFunction = 'rgb';
377+
let cssFunction: CssFunctionType = 'rgb';
371378
if (format === 'hex') {
372379
color = this.faker.string.hexadecimal({
373380
length: includeAlpha ? 8 : 6,
@@ -893,7 +900,7 @@ export class ColorModule {
893900
*
894901
* @default 'sRGB'
895902
*/
896-
space?: CSSSpace;
903+
space?: CssSpaceType;
897904
}): string;
898905
/**
899906
* Returns a random color based on CSS color space specified.
@@ -920,7 +927,7 @@ export class ColorModule {
920927
*
921928
* @default 'sRGB'
922929
*/
923-
space?: CSSSpace;
930+
space?: CssSpaceType;
924931
}): number[];
925932
/**
926933
* Returns a random color based on CSS color space specified.
@@ -949,11 +956,11 @@ export class ColorModule {
949956
*
950957
* @default 'sRGB'
951958
*/
952-
space?: CSSSpace;
959+
space?: CssSpaceType;
953960
}): string | number[];
954961
colorByCSSColorSpace(options?: {
955962
format?: ColorFormat;
956-
space?: CSSSpace;
963+
space?: CssSpaceType;
957964
}): string | number[] {
958965
if (options?.format === 'css' && !options?.space) {
959966
options = { ...options, space: 'sRGB' };

test/__snapshots__/color.spec.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ exports[`color > 42 > colorByCSSColorSpace 1`] = `
1919

2020
exports[`color > 42 > cssSupportedFunction 1`] = `"hsla"`;
2121

22-
exports[`color > 42 > cssSupportedSpace 1`] = `"rec2020"`;
22+
exports[`color > 42 > cssSupportedSpace 1`] = `"display-p3"`;
2323

2424
exports[`color > 42 > hsl 1`] = `
2525
[
@@ -78,7 +78,7 @@ exports[`color > 1211 > colorByCSSColorSpace 1`] = `
7878

7979
exports[`color > 1211 > cssSupportedFunction 1`] = `"color"`;
8080

81-
exports[`color > 1211 > cssSupportedSpace 1`] = `"rec2020"`;
81+
exports[`color > 1211 > cssSupportedSpace 1`] = `"prophoto-rgb"`;
8282

8383
exports[`color > 1211 > hsl 1`] = `
8484
[

test/color.spec.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, expect, it } from 'vitest';
2-
import { faker } from '../src';
3-
import { CSS_FUNCTIONS, CSS_SPACES } from '../src/modules/color';
2+
import { CssFunction, CssSpace, faker } from '../src';
43
import { seededTests } from './support/seededRuns';
54

65
const NON_SEEDED_BASED_RUN = 5;
@@ -44,14 +43,14 @@ describe('color', () => {
4443
describe(`cssSupportedFunction()`, () => {
4544
it('should return random css supported color function from css functions array', () => {
4645
const func = faker.color.cssSupportedFunction();
47-
expect(CSS_FUNCTIONS).toContain(func);
46+
expect(Object.values(CssFunction)).toContain(func);
4847
});
4948
});
5049

5150
describe(`cssSupportedSpace()`, () => {
5251
it('should return random css supported color space from css spaces array', () => {
5352
const space = faker.color.cssSupportedSpace();
54-
expect(CSS_SPACES).toContain(space);
53+
expect(Object.values(CssSpace)).toContain(space);
5554
});
5655
});
5756

0 commit comments

Comments
 (0)