Skip to content

Commit

Permalink
refactor(color): use signature level defaults (#2703)
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT authored Mar 4, 2024
1 parent 2b15f2e commit 82cdf0f
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions src/modules/color/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,12 @@ export class ColorModule extends ModuleBase {
*/
format?: ColorFormat;
}): string | number[];
cmyk(options?: { format?: ColorFormat }): string | number[] {
cmyk(options: { format?: ColorFormat } = {}): string | number[] {
const { format = 'decimal' } = options;
const color: string | number[] = Array.from({ length: 4 }, () =>
this.faker.number.float({ multipleOf: 0.01 })
);
return toColorFormat(color, options?.format || 'decimal', 'cmyk');
return toColorFormat(color, format, 'cmyk');
}

/**
Expand Down Expand Up @@ -572,20 +573,19 @@ export class ColorModule extends ModuleBase {
*/
includeAlpha?: boolean;
}): string | number[];
hsl(options?: {
format?: ColorFormat;
includeAlpha?: boolean;
}): string | number[] {
hsl(
options: {
format?: ColorFormat;
includeAlpha?: boolean;
} = {}
): string | number[] {
const { format = 'decimal', includeAlpha = false } = options;
const hsl: number[] = [this.faker.number.int(360)];
for (let i = 0; i < (options?.includeAlpha ? 3 : 2); i++) {
hsl.push(this.faker.number.float({ multipleOf: 0.01 }));
}

return toColorFormat(
hsl,
options?.format || 'decimal',
options?.includeAlpha ? 'hsla' : 'hsl'
);
return toColorFormat(hsl, format, includeAlpha ? 'hsla' : 'hsl');
}

/**
Expand Down Expand Up @@ -674,20 +674,23 @@ export class ColorModule extends ModuleBase {
*
* @since 7.0.0
*/
hwb(options?: {
/**
* Format of generated RGB color.
*
* @default 'decimal'
*/
format?: ColorFormat;
}): string | number[] {
hwb(
options: {
/**
* Format of generated RGB color.
*
* @default 'decimal'
*/
format?: ColorFormat;
} = {}
): string | number[] {
const { format = 'decimal' } = options;
const hsl: number[] = [this.faker.number.int(360)];
for (let i = 0; i < 2; i++) {
hsl.push(this.faker.number.float({ multipleOf: 0.01 }));
}

return toColorFormat(hsl, options?.format || 'decimal', 'hwb');
return toColorFormat(hsl, format, 'hwb');
}

/**
Expand Down Expand Up @@ -762,15 +765,16 @@ export class ColorModule extends ModuleBase {
*/
format?: ColorFormat;
}): string | number[];
lab(options?: { format?: ColorFormat }): string | number[] {
lab(options: { format?: ColorFormat } = {}): string | number[] {
const { format = 'decimal' } = options;
const lab = [this.faker.number.float({ multipleOf: 0.000001 })];
for (let i = 0; i < 2; i++) {
lab.push(
this.faker.number.float({ min: -100, max: 100, multipleOf: 0.0001 })
);
}

return toColorFormat(lab, options?.format || 'decimal', 'lab');
return toColorFormat(lab, format, 'lab');
}

/**
Expand Down Expand Up @@ -857,13 +861,14 @@ export class ColorModule extends ModuleBase {
*/
format?: ColorFormat;
}): string | number[];
lch(options?: { format?: ColorFormat }): string | number[] {
lch(options: { format?: ColorFormat } = {}): string | number[] {
const { format = 'decimal' } = options;
const lch = [this.faker.number.float({ multipleOf: 0.000001 })];
for (let i = 0; i < 2; i++) {
lch.push(this.faker.number.float({ max: 230, multipleOf: 0.1 }));
}

return toColorFormat(lch, options?.format || 'decimal', 'lch');
return toColorFormat(lch, format, 'lch');
}

/**
Expand Down Expand Up @@ -959,22 +964,17 @@ export class ColorModule extends ModuleBase {
*/
space?: CssSpaceType;
}): string | number[];
colorByCSSColorSpace(options?: {
format?: ColorFormat;
space?: CssSpaceType;
}): string | number[] {
if (options?.format === 'css' && !options?.space) {
options = { ...options, space: 'sRGB' };
}
colorByCSSColorSpace(
options: {
format?: ColorFormat;
space?: CssSpaceType;
} = {}
): string | number[] {
const { format = 'decimal', space = 'sRGB' } = options;

const color = Array.from({ length: 3 }, () =>
this.faker.number.float({ multipleOf: 0.0001 })
);
return toColorFormat(
color,
options?.format || 'decimal',
'color',
options?.space
);
return toColorFormat(color, format, 'color', space);
}
}

0 comments on commit 82cdf0f

Please sign in to comment.