Skip to content

Commit 92207b7

Browse files
olrtgST-DDT
andauthored
refactor(image)!: randomize defaults (#2472)
Co-authored-by: ST-DDT <[email protected]>
1 parent 50897d9 commit 92207b7

File tree

4 files changed

+111
-89
lines changed

4 files changed

+111
-89
lines changed

docs/guide/upgrading_v9/2472.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### Images now have random width, height and other options by default
2+
3+
`faker.image.url()` now returns an image url with a random width and height by default. To obtain the previous behavior, pass `{width: 640, height: 480}`.
4+
5+
`faker.image.urlLoremFlickr()` now returns an image url with a random width and height by default. To obtain the previous behavior, pass `{width: 640, height: 480}`.
6+
7+
`faker.image.urlPicsumPhotos()` now returns an image url with a random width and height by default, additionally images may be converted to grayscale and blurred at random. To obtain the previous behavior, pass `{width: 640, height: 480, blur: 0, grayscale: false}`
8+
9+
`faker.image.dataUri()` now returns an image url with a random width and height by default, additionally the type of the image is now random. To obtain the previous behavior, pass `{width: 640, height: 480, type: 'svg-uri'}`.

src/modules/image/index.ts

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ export class ImageModule extends ModuleBase {
102102
* Generates a random image url.
103103
*
104104
* @param options Options for generating a URL for an image.
105-
* @param options.width The width of the image. Defaults to `640`.
106-
* @param options.height The height of the image. Defaults to `480`.
105+
* @param options.width The width of the image. Defaults to random integer between `1` and `3999`.
106+
* @param options.height The height of the image. Defaults to random integer between `1` and `3999`.
107107
*
108108
* @example
109109
* faker.image.url() // 'https://loremflickr.com/640/480?lock=1234'
@@ -115,22 +115,26 @@ export class ImageModule extends ModuleBase {
115115
/**
116116
* The width of the image.
117117
*
118-
* @default 640
118+
* @default faker.number.int({ min: 1, max: 3999 })
119119
*/
120120
width?: number;
121121
/**
122122
* The height of the image.
123123
*
124-
* @default 480
124+
* @default faker.number.int({ min: 1, max: 3999 })
125125
*/
126126
height?: number;
127127
} = {}
128128
): string {
129-
const { width = 640, height = 480 } = options;
129+
const {
130+
width = this.faker.number.int({ min: 1, max: 3999 }),
131+
height = this.faker.number.int({ min: 1, max: 3999 }),
132+
} = options;
130133

131134
const urlMethod = this.faker.helpers.arrayElement([
132135
this.urlLoremFlickr,
133-
this.urlPicsumPhotos,
136+
({ width, height }: { width?: number; height?: number }) =>
137+
this.urlPicsumPhotos({ width, height, grayscale: false, blur: 0 }),
134138
]);
135139

136140
return urlMethod({ width, height });
@@ -140,8 +144,8 @@ export class ImageModule extends ModuleBase {
140144
* Generates a random image url provided via https://loremflickr.com.
141145
*
142146
* @param options Options for generating a URL for an image.
143-
* @param options.width The width of the image. Defaults to `640`.
144-
* @param options.height The height of the image. Defaults to `480`.
147+
* @param options.width The width of the image. Defaults to random integer between `1` and `3999`.
148+
* @param options.height The height of the image. Defaults to random integer between `1` and `3999`.
145149
* @param options.category Category to use for the image.
146150
*
147151
* @example
@@ -157,13 +161,13 @@ export class ImageModule extends ModuleBase {
157161
/**
158162
* The width of the image.
159163
*
160-
* @default 640
164+
* @default faker.number.int({ min: 1, max: 3999 })
161165
*/
162166
width?: number;
163167
/**
164168
* The height of the image.
165169
*
166-
* @default 480
170+
* @default faker.number.int({ min: 1, max: 3999 })
167171
*/
168172
height?: number;
169173
/**
@@ -172,7 +176,11 @@ export class ImageModule extends ModuleBase {
172176
category?: string;
173177
} = {}
174178
): string {
175-
const { width = 640, height = 480, category } = options;
179+
const {
180+
width = this.faker.number.int({ min: 1, max: 3999 }),
181+
height = this.faker.number.int({ min: 1, max: 3999 }),
182+
category,
183+
} = options;
176184

177185
return `https://loremflickr.com/${width}/${height}${
178186
category == null ? '' : `/${category}`
@@ -183,10 +191,10 @@ export class ImageModule extends ModuleBase {
183191
* Generates a random image url provided via https://picsum.photos.
184192
*
185193
* @param options Options for generating a URL for an image.
186-
* @param options.width The width of the image. Defaults to `640`.
187-
* @param options.height The height of the image. Defaults to `480`.
188-
* @param options.grayscale Whether the image should be grayscale. Defaults to `false`.
189-
* @param options.blur Whether the image should be blurred. Defaults to `false`.
194+
* @param options.width The width of the image. Defaults to random integer between `1` and `3999`.
195+
* @param options.height The height of the image. Defaults to random integer between `1` and `3999`.
196+
* @param options.grayscale Whether the image should be grayscale. Defaults to a random boolean value.
197+
* @param options.blur Whether the image should be blurred. `0` disables the blur. Defaults to a random integer from `0` to `10`.
190198
*
191199
* @example
192200
* faker.image.urlPicsumPhotos() // 'https://picsum.photos/seed/NWbJM2B/640/480'
@@ -203,30 +211,35 @@ export class ImageModule extends ModuleBase {
203211
/**
204212
* The width of the image.
205213
*
206-
* @default 640
214+
* @default faker.number.int({ min: 1, max: 3999 })
207215
*/
208216
width?: number;
209217
/**
210218
* The height of the image.
211219
*
212-
* @default 480
220+
* @default faker.number.int({ min: 1, max: 3999 })
213221
*/
214222
height?: number;
215223
/**
216224
* Whether the image should be grayscale.
217225
*
218-
* @default false
226+
* @default faker.datatype.boolean()
219227
*/
220228
grayscale?: boolean;
221229
/**
222-
* Whether the image should be blurred.
230+
* Whether the image should be blurred. `0` disables the blur.
223231
*
224-
* @default false
232+
* @default faker.number.int({ max: 10 })
225233
*/
226-
blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
234+
blur?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10;
227235
} = {}
228236
): string {
229-
const { width = 640, height = 480, grayscale = false, blur } = options;
237+
const {
238+
width = this.faker.number.int({ min: 1, max: 3999 }),
239+
height = this.faker.number.int({ min: 1, max: 3999 }),
240+
grayscale = this.faker.datatype.boolean(),
241+
blur = this.faker.number.int({ max: 10 }),
242+
} = options;
230243

231244
let url = `https://picsum.photos/seed/${this.faker.string.alphanumeric({
232245
length: { min: 5, max: 10 },
@@ -350,10 +363,10 @@ export class ImageModule extends ModuleBase {
350363
* Generates a random data uri containing an URL-encoded SVG image or a Base64-encoded SVG image.
351364
*
352365
* @param options Options for generating a data uri.
353-
* @param options.width The width of the image. Defaults to `640`.
354-
* @param options.height The height of the image. Defaults to `480`.
366+
* @param options.width The width of the image. Defaults to random integer between `1` and `3999`.
367+
* @param options.height The height of the image. Defaults to random integer between `1` and `3999`.
355368
* @param options.color The color of the image. Must be a color supported by svg. Defaults to a random color.
356-
* @param options.type The type of the image. Defaults to `'svg-uri'`.
369+
* @param options.type The type of the image. Defaults to a random type.
357370
*
358371
* @example
359372
* faker.image.dataUri() // 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http...'
@@ -366,13 +379,13 @@ export class ImageModule extends ModuleBase {
366379
/**
367380
* The width of the image.
368381
*
369-
* @default 640
382+
* @default faker.number.int({ min: 1, max: 3999 })
370383
*/
371384
width?: number;
372385
/**
373386
* The height of the image.
374387
*
375-
* @default 480
388+
* @default faker.number.int({ min: 1, max: 3999 })
376389
*/
377390
height?: number;
378391
/**
@@ -385,16 +398,16 @@ export class ImageModule extends ModuleBase {
385398
* The type of the image to return. Consisting of
386399
* the file extension and the used encoding.
387400
*
388-
* @default 'svg-uri'
401+
* @default faker.helpers.arrayElements(['svg-uri', 'svg-base64'])
389402
*/
390403
type?: 'svg-uri' | 'svg-base64';
391404
} = {}
392405
): string {
393406
const {
394-
width = 640,
395-
height = 480,
407+
width = this.faker.number.int({ min: 1, max: 3999 }),
408+
height = this.faker.number.int({ min: 1, max: 3999 }),
396409
color = this.faker.color.rgb(),
397-
type = 'svg-uri',
410+
type = this.faker.helpers.arrayElements(['svg-uri', 'svg-base64']),
398411
} = options;
399412

400413
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="${

0 commit comments

Comments
 (0)