diff options
| author | José Olórtegui <[email protected]> | 2024-02-11 20:06:33 -0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-02-11 23:06:33 +0000 |
| commit | 92207b7df15732a61e613a4376a9ce88bd4f7b0f (patch) | |
| tree | 74d96571872e3418674d9c44b84f587efe04efda /src/modules/image | |
| parent | 50897d927464e3ad2c54ae54b57f7d068e3a76a5 (diff) | |
| download | faker-92207b7df15732a61e613a4376a9ce88bd4f7b0f.tar.xz faker-92207b7df15732a61e613a4376a9ce88bd4f7b0f.zip | |
refactor(image)!: randomize defaults (#2472)
Co-authored-by: ST-DDT <[email protected]>
Diffstat (limited to 'src/modules/image')
| -rw-r--r-- | src/modules/image/index.ts | 75 |
1 files changed, 44 insertions, 31 deletions
diff --git a/src/modules/image/index.ts b/src/modules/image/index.ts index f2c3c9d0..1a26065d 100644 --- a/src/modules/image/index.ts +++ b/src/modules/image/index.ts @@ -102,8 +102,8 @@ export class ImageModule extends ModuleBase { * 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' @@ -115,22 +115,26 @@ export class ImageModule extends ModuleBase { /** * 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, - this.urlPicsumPhotos, + ({ width, height }: { width?: number; height?: number }) => + this.urlPicsumPhotos({ width, height, grayscale: false, blur: 0 }), ]); return urlMethod({ width, height }); @@ -140,8 +144,8 @@ export class ImageModule extends ModuleBase { * 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 @@ -157,13 +161,13 @@ export class ImageModule extends ModuleBase { /** * 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; /** @@ -172,7 +176,11 @@ export class ImageModule extends ModuleBase { 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}` @@ -183,10 +191,10 @@ export class ImageModule extends ModuleBase { * 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. `0` disables the blur. Defaults to a random integer from `0` to `10`. * * @example * faker.image.urlPicsumPhotos() // 'https://picsum.photos/seed/NWbJM2B/640/480' @@ -203,30 +211,35 @@ export class ImageModule extends ModuleBase { /** * 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. + * Whether the image should be blurred. `0` disables the blur. * - * @default false + * @default faker.number.int({ max: 10 }) */ - blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10; + blur?: 0 | 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.number.int({ max: 10 }), + } = options; let url = `https://picsum.photos/seed/${this.faker.string.alphanumeric({ length: { min: 5, max: 10 }, @@ -350,10 +363,10 @@ export class ImageModule extends ModuleBase { * 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...' @@ -366,13 +379,13 @@ export class ImageModule extends ModuleBase { /** * 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; /** @@ -385,16 +398,16 @@ export class ImageModule extends ModuleBase { * 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="${ |
