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 5 commits
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
70 changes: 42 additions & 28 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 random integer between `1` and `3999`.
* @param options.height The height of the image. Defaults to random integer between `1` and `3999`.
*
* @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 faker.number.int({ min: 1, max: 3999 })
*/
width?: number;
/**
* The height of the image.
*
* @default 480
* @default faker.number.int({ min: 1, max: 3999 })
*/
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 random integer between `1` and `3999`.
* @param options.height The height of the image. Defaults to random integer between `1` and `3999`.
* @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 faker.number.int({ min: 1, max: 3999 })
*/
width?: number;
/**
* The height of the image.
*
* @default 480
* @default faker.number.int({ min: 1, max: 3999 })
*/
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,10 +190,10 @@ 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.grayscale Whether the image should be grayscale. Defaults to `false`.
* @param options.blur Whether the image should be blurred. Defaults to `false`.
* @param options.width The width of the image. Defaults to random integer between `1` and `3999`.
* @param options.height The height of the image. Defaults to random integer between `1` and `3999`.
* @param options.grayscale Whether the image should be grayscale. Defaults to a random boolean value.
* @param options.blur Whether the image should be blurred. Defaults to a random integer from `1` to `10` or `undefined`.
*
* @example
* faker.image.urlPicsumPhotos() // 'https://picsum.photos/seed/NWbJM2B/640/480'
Expand All @@ -203,30 +210,37 @@ export class ImageModule {
/**
* The width of the image.
*
* @default 640
* @default faker.number.int({ min: 1, max: 3999 })
*/
width?: number;
/**
* The height of the image.
*
* @default 480
* @default faker.number.int({ min: 1, max: 3999 })
*/
height?: number;
/**
* Whether the image should be grayscale.
*
* @default false
* @default faker.datatype.boolean()
*/
grayscale?: boolean;
/**
* Whether the image should be blurred.
olrtg marked this conversation as resolved.
Show resolved Hide resolved
*
* @default false
* @default faker.helpers.maybe(() => faker.number.int({ min: 1, max: 10 }))
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*/
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 = this.faker.datatype.boolean(),
blur = this.faker.helpers.maybe(() =>
this.faker.number.int({ min: 1, max: 10 })
),
} = options;

let url = `https://picsum.photos/seed/${this.faker.string.alphanumeric({
length: { min: 5, max: 10 },
Expand Down Expand Up @@ -351,10 +365,10 @@ 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 random integer between `1` and `3999`.
* @param options.height The height of the image. Defaults to random integer between `1` and `3999`.
* @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'`.
* @param options.type The type of the image. Defaults to a random type.
*
* @example
* faker.image.dataUri() // 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http...'
Expand All @@ -367,13 +381,13 @@ export class ImageModule {
/**
* The width of the image.
*
* @default 640
* @default faker.number.int({ min: 1, max: 3999 })
*/
width?: number;
/**
* The height of the image.
*
* @default 480
* @default faker.number.int({ min: 1, max: 3999 })
*/
height?: number;
/**
Expand All @@ -386,16 +400,16 @@ export class ImageModule {
* The type of the image to return. Consisting of
* the file extension and the used encoding.
*
* @default 'svg-uri'
* @default faker.helpers.arrayElements(['svg-uri', 'svg-base64'])
*/
type?: 'svg-uri' | 'svg-base64';
} = {}
): 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',
type = this.faker.helpers.arrayElements(['svg-uri', 'svg-base64']),
} = options;

const svgString = `<svg xmlns="http://www.w3.org/2000/svg" version="1.1" baseProfile="full" width="${width}" height="${height}"><rect width="100%" height="100%" fill="${color}"/><text x="${
Expand Down
Loading
Loading