From a0010900205ed8b1d1ba2adefe222c1cb41c1a70 Mon Sep 17 00:00:00 2001 From: Shinigami Date: Tue, 14 Mar 2023 18:37:05 +0100 Subject: [PATCH] feat: provide enums for color values (#1910) --- src/index.ts | 9 +++- src/modules/color/index.ts | 73 +++++++++++++++------------ test/__snapshots__/color.spec.ts.snap | 4 +- test/color.spec.ts | 7 ++- 4 files changed, 52 insertions(+), 41 deletions(-) diff --git a/src/index.ts b/src/index.ts index 27923560bdc..dc67c9d501f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -40,12 +40,17 @@ export * as allLocales from './locales'; export { Aircraft, AircraftType } from './modules/airline'; export type { AirlineModule } from './modules/airline'; export type { AnimalModule } from './modules/animal'; +export { CssFunction, CssSpace } from './modules/color'; export type { Casing, ColorFormat, ColorModule, - CSSFunction, - CSSSpace, + /** @deprecated Use CssFunctionType instead */ + CssFunctionType as CSSFunction, + CssFunctionType, + /** @deprecated Use CssSpaceType instead */ + CssSpaceType as CSSSpace, + CssSpaceType, NumberColorFormat, StringColorFormat, } from './modules/color'; diff --git a/src/modules/color/index.ts b/src/modules/color/index.ts index 67fa9b9a1c3..5100776acaa 100644 --- a/src/modules/color/index.ts +++ b/src/modules/color/index.ts @@ -3,32 +3,39 @@ import type { Faker } from '../../faker'; /** * Color space names supported by CSS. */ -export const CSS_SPACES = [ - 'sRGB', - 'display-p3', - 'rec2020', - 'a98-rgb', - 'prophoto-rgb', - 'rec2020', -] as const; +export enum CssSpace { + SRGB = 'sRGB', + DisplayP3 = 'display-p3', + REC2020 = 'rec2020', + A98RGB = 'a98-rgb', + ProphotoRGB = 'prophoto-rgb', +} + +/** + * Color space names supported by CSS. + */ +export type CssSpaceType = `${CssSpace}`; /** * Functions supported by CSS to produce color. */ -export const CSS_FUNCTIONS = [ - 'rgb', - 'rgba', - 'hsl', - 'hsla', - 'hwb', - 'cmyk', - 'lab', - 'lch', - 'color', -] as const; - -export type CSSFunction = (typeof CSS_FUNCTIONS)[number]; -export type CSSSpace = (typeof CSS_SPACES)[number]; +export enum CssFunction { + RGB = 'rgb', + RGBA = 'rgba', + HSL = 'hsl', + HSLA = 'hsla', + HWB = 'hwb', + CMYK = 'cmyk', + LAB = 'lab', + LCH = 'lch', + COLOR = 'color', +} + +/** + * Functions supported by CSS to produce color. + */ +export type CssFunctionType = `${CssFunction}`; + export type StringColorFormat = 'css' | 'binary'; export type NumberColorFormat = 'decimal'; export type ColorFormat = StringColorFormat | NumberColorFormat; @@ -95,8 +102,8 @@ function toBinary(values: number[]): string { */ function toCSS( values: number[], - cssFunction: CSSFunction = 'rgb', - space: CSSSpace = 'sRGB' + cssFunction: CssFunctionType = 'rgb', + space: CssSpaceType = 'sRGB' ): string { const percentage = (value: number) => Math.round(value * 100); switch (cssFunction) { @@ -141,8 +148,8 @@ function toCSS( function toColorFormat( values: number[], format: ColorFormat, - cssFunction: CSSFunction = 'rgb', - space: CSSSpace = 'sRGB' + cssFunction: CssFunctionType = 'rgb', + space: CssSpaceType = 'sRGB' ): string | number[] { switch (format) { case 'css': @@ -205,7 +212,7 @@ export class ColorModule { * @since 7.0.0 */ cssSupportedFunction(): string { - return this.faker.helpers.arrayElement(CSS_FUNCTIONS); + return this.faker.helpers.objectValue(CssFunction); } /** @@ -217,7 +224,7 @@ export class ColorModule { * @since 7.0.0 */ cssSupportedSpace(): string { - return this.faker.helpers.arrayElement(CSS_SPACES); + return this.faker.helpers.objectValue(CssSpace); } /** @@ -367,7 +374,7 @@ export class ColorModule { } = options || {}; options = { format, includeAlpha, prefix, casing }; let color: string | number[]; - let cssFunction: CSSFunction = 'rgb'; + let cssFunction: CssFunctionType = 'rgb'; if (format === 'hex') { color = this.faker.string.hexadecimal({ length: includeAlpha ? 8 : 6, @@ -893,7 +900,7 @@ export class ColorModule { * * @default 'sRGB' */ - space?: CSSSpace; + space?: CssSpaceType; }): string; /** * Returns a random color based on CSS color space specified. @@ -920,7 +927,7 @@ export class ColorModule { * * @default 'sRGB' */ - space?: CSSSpace; + space?: CssSpaceType; }): number[]; /** * Returns a random color based on CSS color space specified. @@ -949,11 +956,11 @@ export class ColorModule { * * @default 'sRGB' */ - space?: CSSSpace; + space?: CssSpaceType; }): string | number[]; colorByCSSColorSpace(options?: { format?: ColorFormat; - space?: CSSSpace; + space?: CssSpaceType; }): string | number[] { if (options?.format === 'css' && !options?.space) { options = { ...options, space: 'sRGB' }; diff --git a/test/__snapshots__/color.spec.ts.snap b/test/__snapshots__/color.spec.ts.snap index 2d3ce303041..a39ce0f3558 100644 --- a/test/__snapshots__/color.spec.ts.snap +++ b/test/__snapshots__/color.spec.ts.snap @@ -19,7 +19,7 @@ exports[`color > 42 > colorByCSSColorSpace 1`] = ` exports[`color > 42 > cssSupportedFunction 1`] = `"hsla"`; -exports[`color > 42 > cssSupportedSpace 1`] = `"rec2020"`; +exports[`color > 42 > cssSupportedSpace 1`] = `"display-p3"`; exports[`color > 42 > hsl 1`] = ` [ @@ -78,7 +78,7 @@ exports[`color > 1211 > colorByCSSColorSpace 1`] = ` exports[`color > 1211 > cssSupportedFunction 1`] = `"color"`; -exports[`color > 1211 > cssSupportedSpace 1`] = `"rec2020"`; +exports[`color > 1211 > cssSupportedSpace 1`] = `"prophoto-rgb"`; exports[`color > 1211 > hsl 1`] = ` [ diff --git a/test/color.spec.ts b/test/color.spec.ts index 29ce63ed0c7..3f4f04ddf11 100644 --- a/test/color.spec.ts +++ b/test/color.spec.ts @@ -1,6 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { faker } from '../src'; -import { CSS_FUNCTIONS, CSS_SPACES } from '../src/modules/color'; +import { CssFunction, CssSpace, faker } from '../src'; import { seededTests } from './support/seededRuns'; const NON_SEEDED_BASED_RUN = 5; @@ -44,14 +43,14 @@ describe('color', () => { describe(`cssSupportedFunction()`, () => { it('should return random css supported color function from css functions array', () => { const func = faker.color.cssSupportedFunction(); - expect(CSS_FUNCTIONS).toContain(func); + expect(Object.values(CssFunction)).toContain(func); }); }); describe(`cssSupportedSpace()`, () => { it('should return random css supported color space from css spaces array', () => { const space = faker.color.cssSupportedSpace(); - expect(CSS_SPACES).toContain(space); + expect(Object.values(CssSpace)).toContain(space); }); });