Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(image)!: randomize defaults #2472

Merged
merged 18 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions src/modules/image/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ export class ImageModule {
* Generates a random image url.
*
* @param options Options for generating a URL for an image.
* @param options.width The width of the image. Defaults to `640`.
* @param options.height The height of the image. Defaults to `480`.
* @param options.width The width of the image. Defaults to `this.faker.number.int({ min: 1, max: 3999 })`.
* @param options.height The height of the image. Defaults to `this.faker.number.int({ min: 1, max: 3999 })`.
olrtg marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* faker.image.url() // 'https://loremflickr.com/640/480?lock=1234'
Expand All @@ -115,18 +115,21 @@ export class ImageModule {
/**
* The width of the image.
*
* @default 640
* @default this.faker.number.int({ min: 1, max: 3999 })
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*/
width?: number;
/**
* The height of the image.
*
* @default 480
* @default this.faker.number.int({ min: 1, max: 3999 })
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*/
height?: number;
} = {}
): string {
const { width = 640, height = 480 } = options;
const {
width = this.faker.number.int({ min: 1, max: 3999 }),
height = this.faker.number.int({ min: 1, max: 3999 }),
} = options;

const urlMethod = this.faker.helpers.arrayElement([
this.urlLoremFlickr,
Expand All @@ -140,8 +143,8 @@ export class ImageModule {
* Generates a random image url provided via https://loremflickr.com.
*
* @param options Options for generating a URL for an image.
* @param options.width The width of the image. Defaults to `640`.
* @param options.height The height of the image. Defaults to `480`.
* @param options.width The width of the image. Defaults to `this.faker.number.int({ min: 1, max: 3999 })`.
* @param options.height The height of the image. Defaults to `this.faker.number.int({ min: 1, max: 3999 })`.
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
* @param options.category Category to use for the image.
*
* @example
Expand All @@ -157,13 +160,13 @@ export class ImageModule {
/**
* The width of the image.
*
* @default 640
* @default this.faker.number.int({ min: 1, max: 3999 })
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*/
width?: number;
/**
* The height of the image.
*
* @default 480
* @default this.faker.number.int({ min: 1, max: 3999 })
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*/
height?: number;
/**
Expand All @@ -172,7 +175,11 @@ export class ImageModule {
category?: string;
} = {}
): string {
const { width = 640, height = 480, category } = options;
const {
width = this.faker.number.int({ min: 1, max: 3999 }),
height = this.faker.number.int({ min: 1, max: 3999 }),
category,
} = options;

return `https://loremflickr.com/${width}/${height}${
category != null ? `/${category}` : ''
Expand All @@ -183,8 +190,8 @@ export class ImageModule {
* Generates a random image url provided via https://picsum.photos.
*
* @param options Options for generating a URL for an image.
* @param options.width The width of the image. Defaults to `640`.
* @param options.height The height of the image. Defaults to `480`.
* @param options.width The width of the image. Defaults to `this.faker.number.int({ min: 1, max: 3999 })`.
* @param options.height The height of the image. Defaults to `this.faker.number.int({ min: 1, max: 3999 })`.
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
* @param options.grayscale Whether the image should be grayscale. Defaults to `false`.
* @param options.blur Whether the image should be blurred. Defaults to `false`.
*
Expand All @@ -203,13 +210,13 @@ export class ImageModule {
/**
* The width of the image.
*
* @default 640
* @default this.faker.number.int({ min: 1, max: 3999 })
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*/
width?: number;
/**
* The height of the image.
*
* @default 480
* @default this.faker.number.int({ min: 1, max: 3999 })
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*/
height?: number;
/**
Expand All @@ -226,7 +233,12 @@ export class ImageModule {
blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
} = {}
): string {
const { width = 640, height = 480, grayscale = false, blur } = options;
const {
width = this.faker.number.int({ min: 1, max: 3999 }),
height = this.faker.number.int({ min: 1, max: 3999 }),
grayscale = false,
blur,
olrtg marked this conversation as resolved.
Show resolved Hide resolved
} = options;

let url = `https://picsum.photos/seed/${this.faker.string.alphanumeric({
length: { min: 5, max: 10 },
Expand Down Expand Up @@ -351,8 +363,8 @@ export class ImageModule {
* Generates a random data uri containing an URL-encoded SVG image or a Base64-encoded SVG image.
*
* @param options Options for generating a data uri.
* @param options.width The width of the image. Defaults to `640`.
* @param options.height The height of the image. Defaults to `480`.
* @param options.width The width of the image. Defaults to `this.faker.number.int({ min: 1, max: 3999 })`.
* @param options.height The height of the image. Defaults to `this.faker.number.int({ min: 1, max: 3999 })`.
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
* @param options.color The color of the image. Must be a color supported by svg. Defaults to a random color.
* @param options.type The type of the image. Defaults to `'svg-uri'`.
*
Expand All @@ -367,13 +379,13 @@ export class ImageModule {
/**
* The width of the image.
*
* @default 640
* @default this.faker.number.int({ min: 1, max: 3999 })
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*/
width?: number;
/**
* The height of the image.
*
* @default 480
* @default this.faker.number.int({ min: 1, max: 3999 })
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*/
height?: number;
/**
Expand All @@ -392,8 +404,8 @@ export class ImageModule {
} = {}
): string {
const {
width = 640,
height = 480,
width = this.faker.number.int({ min: 1, max: 3999 }),
height = this.faker.number.int({ min: 1, max: 3999 }),
color = this.faker.color.rgb(),
type = 'svg-uri',
olrtg marked this conversation as resolved.
Show resolved Hide resolved
} = options;
Expand Down
Loading