diff options
| author | Shinigami <[email protected]> | 2022-12-05 08:15:39 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-12-05 08:15:39 +0100 |
| commit | 616b34def1781ad3c34b30f8f44fc9a8e7052a38 (patch) | |
| tree | 5735d14b4d73e67a6be9ed125d3e65af353b70b8 /src/modules | |
| parent | 4ed45fa33f80c59625a285d06abe31ce2f524357 (diff) | |
| download | faker-616b34def1781ad3c34b30f8f44fc9a8e7052a38.tar.xz faker-616b34def1781ad3c34b30f8f44fc9a8e7052a38.zip | |
refactor(image)!: rewrite module (#1477)
Diffstat (limited to 'src/modules')
| -rw-r--r-- | src/modules/image/index.ts | 433 | ||||
| -rw-r--r-- | src/modules/image/providers/lorempicsum.ts | 56 | ||||
| -rw-r--r-- | src/modules/image/providers/lorempixel.ts | 123 | ||||
| -rw-r--r-- | src/modules/image/providers/placeholder.ts | 18 | ||||
| -rw-r--r-- | src/modules/image/providers/unsplash.ts | 67 |
5 files changed, 652 insertions, 45 deletions
diff --git a/src/modules/image/index.ts b/src/modules/image/index.ts index e0dd9d22..6beae600 100644 --- a/src/modules/image/index.ts +++ b/src/modules/image/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { deprecated } from '../../internal/deprecated'; import type { MethodsOf } from '../../utils/types'; import { LoremPicsum } from './providers/lorempicsum'; import { Lorempixel } from './providers/lorempixel'; @@ -6,14 +7,27 @@ import { Placeholder } from './providers/placeholder'; import { Unsplash } from './providers/unsplash'; /** - * Module to generate placeholder images. - * - * Default provider is unsplash image provider. + * Module to generate images. */ export class ImageModule { + /** + * @deprecated Use `faker.image` instead. + */ readonly lorempixel: Lorempixel; + + /** + * @deprecated Use `faker.image` instead. + */ readonly unsplash: Unsplash; + + /** + * @deprecated Use `faker.image` instead. + */ readonly lorempicsum: LoremPicsum; + + /** + * @deprecated Use `faker.image.urlPlaceholder` instead. + */ readonly placeholder: Placeholder; constructor(private readonly faker: Faker) { @@ -32,6 +46,263 @@ export class ImageModule { } /** + * Generates a random avatar image url. + * + * @example + * faker.image.avatar() + * // 'https://avatars.githubusercontent.com/u/97165289' + * + * @since 2.0.1 + */ + avatar(): string { + const avatarMethod = this.faker.helpers.arrayElement([ + this.avatarLegacy, + this.avatarGitHub, + ]); + + return avatarMethod(); + } + + /** + * Generates a random avatar from GitHub. + * + * @example + * faker.image.avatarGitHub() + * // 'https://avatars.githubusercontent.com/u/97165289' + * + * @since 8.0.0 + */ + avatarGitHub(): string { + return `https://avatars.githubusercontent.com/u/${this.faker.number.int( + 100000000 + )}`; + } + + /** + * Generates a random avatar from `https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar`. + * + * @example + * faker.image.avatarLegacy() + * // 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/170.jpg' + * + * @since 8.0.0 + */ + // This implementation will change in the future when we tackle https://github.com/faker-js/faker/issues/465. + avatarLegacy(): string { + return `https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/${this.faker.number.int( + 1249 + )}.jpg`; + } + + /** + * 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`. + * + * @example + * faker.image.url() // 'https://loremflickr.com/640/480?lock=1234' + * + * @since 8.0.0 + */ + url( + options: { + width?: number; + height?: number; + } = {} + ): string { + const { width = 640, height = 480 } = options; + + const urlMethod = this.faker.helpers.arrayElement([ + this.urlLoremFlickr, + this.urlPicsumPhotos, + ]); + + return urlMethod({ width, height }); + } + + /** + * 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.category Category to use for the image. + * + * @example + * faker.image.urlLoremFlickr() // 'https://loremflickr.com/640/480?lock=1234' + * faker.image.urlLoremFlickr({ width: 128 }) // 'https://loremflickr.com/128/480?lock=1234' + * faker.image.urlLoremFlickr({ height: 128 }) // 'https://loremflickr.com/640/128?lock=1234' + * faker.image.urlLoremFlickr({ category: 'nature' }) // 'https://loremflickr.com/640/480/nature?lock=1234' + * + * @since 8.0.0 + */ + urlLoremFlickr( + options: { + width?: number; + height?: number; + category?: string; + } = {} + ): string { + const { width = 640, height = 480, category } = options; + + return `https://loremflickr.com/${width}/${height}${ + category != null ? `/${category}` : '' + }?lock=${this.faker.number.int()}`; + } + + /** + * 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`. + * + * @example + * faker.image.urlPicsumPhotos() // 'https://picsum.photos/id/241/640/480' + * faker.image.urlPicsumPhotos({ width: 128 }) // 'https://picsum.photos/id/241/128/480' + * faker.image.urlPicsumPhotos({ height: 128 }) // 'https://picsum.photos/id/241/640/128' + * faker.image.urlPicsumPhotos({ grayscale: true }) // 'https://picsum.photos/id/241/640/480?grayscale' + * faker.image.urlPicsumPhotos({ blur: 4 }) // 'https://picsum.photos/id/241/640/480?blur=4' + * faker.image.urlPicsumPhotos({ blur: 4, grayscale: true }) // 'https://picsum.photos/id/241/640/480?grayscale&blur=4' + * + * @since 8.0.0 + */ + urlPicsumPhotos( + options: { + width?: number; + height?: number; + grayscale?: boolean; + blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10; + } = {} + ): string { + const { width = 640, height = 480, grayscale = false, blur } = options; + + let url = `https://picsum.photos/id/${this.faker.number.int( + 1000 + )}/${width}/${height}`; + + const hasValidGrayscale = grayscale === true; + const hasValidBlur = typeof blur === 'number' && blur >= 1 && blur <= 10; + + if (hasValidGrayscale || hasValidBlur) { + url += '?'; + + if (hasValidGrayscale) { + url += `grayscale`; + } + + if (hasValidGrayscale && hasValidBlur) { + url += '&'; + } + + if (hasValidBlur) { + url += `blur=${blur}`; + } + } + + return url; + } + + /** + * Generates a random image url provided via https://via.placeholder.com/. + * + * @param options Options for generating a URL for an image. + * @param options.width The width of the image. Defaults to random number between 1 and 3999. + * @param options.height The height of the image. Defaults to random number between 1 and 3999. + * @param options.backgroundColor The background color of the image. Defaults to random hex color. + * @param options.textColor The text color of the image. Defaults to random hex color. + * @param options.format The format of the image. Defaults to random format. + * @param options.text The text to display on the image. Defaults to string. + * + * @example + * faker.image.urlPlaceholder() // 'https://via.placeholder.com/150x180/FF0000/FFFFFF.webp?text=lorem' + * faker.image.urlPlaceholder({ width: 128 }) // 'https://via.placeholder.com/128x180/FF0000/FFFFFF.webp?text=lorem' + * faker.image.urlPlaceholder({ height: 128 }) // 'https://via.placeholder.com/150x128/FF0000/FFFFFF.webp?text=lorem' + * faker.image.urlPlaceholder({ backgroundColor: '000000' }) // 'https://via.placeholder.com/150x180/000000/FFFFFF.webp?text=lorem' + * faker.image.urlPlaceholder({ textColor: '000000' }) // 'https://via.placeholder.com/150x180/FF0000/000000.webp?text=lorem' + * faker.image.urlPlaceholder({ format: 'png' }) // 'https://via.placeholder.com/150x180/FF0000/FFFFFF.png?text=lorem' + * faker.image.urlPlaceholder({ text: 'lorem ipsum' }) // 'https://via.placeholder.com/150x180/FF0000/FFFFFF.webp?text=lorem+ipsum' + * faker.image.urlPlaceholder({ width: 128, height: 128, backgroundColor: '000000', textColor: 'FF0000', format: 'png', text: 'lorem ipsum' }) // 'https://via.placeholder.com/128x128/000000/FF0000.png?text=lorem+ipsum' + * + * @since 8.0.0 + */ + urlPlaceholder( + options: { + width?: number; + height?: number; + backgroundColor?: string; + textColor?: string; + format?: 'gif' | 'jpeg' | 'jpg' | 'png' | 'webp'; + text?: string; + } = {} + ): string { + const { + width = this.faker.number.int({ min: 1, max: 3999 }), + height = this.faker.number.int({ min: 1, max: 3999 }), + backgroundColor = this.faker.color.rgb({ format: 'hex', prefix: '' }), + textColor = this.faker.color.rgb({ format: 'hex', prefix: '' }), + format = this.faker.helpers.arrayElement([ + 'gif', + 'jpeg', + 'jpg', + 'png', + 'webp', + ]), + text = this.faker.lorem.words(), + } = options; + + let url = `https://via.placeholder.com`; + + url += `/${width}`; + url += `x${height}`; + + url += `/${backgroundColor}`; + url += `/${textColor}`; + + url += `.${format}`; + + url += `?text=${encodeURIComponent(text)}`; + + return url; + } + + /** + * Generates a random data uri containing an 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.color The color of the image. Defaults to `grey`. + * + * @example + * faker.image.dataUri() // 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http...' + * + * @since 4.0.0 + */ + dataUri( + options: { + width?: number; + height?: number; + color?: string; + } = {} + ): string { + const { width = 640, height = 480, color = 'grey' } = 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="${ + width / 2 + }" y="${ + height / 2 + }" font-size="20" alignment-baseline="middle" text-anchor="middle" fill="white">${width}x${height}</text></svg>`; + + const rawPrefix = 'data:image/svg+xml;charset=UTF-8,'; + return rawPrefix + encodeURIComponent(svgString); + } + + /** * Generates a random image url from one of the supported categories. * * @param width The width of the image. Defaults to `640`. @@ -44,8 +315,16 @@ export class ImageModule { * faker.image.image(1234, 2345, true) // 'https://loremflickr.com/1234/2345/nature?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ image(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.image', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); const categories: MethodsOf<ImageModule, ImageModule['image']> = [ 'abstract', 'animals', @@ -69,19 +348,6 @@ export class ImageModule { } /** - * Generates a random avatar image url. - * - * @example - * faker.image.avatar() - * // 'https://cloudflare-ipfs.com/ipfs/Qmd3W5DuhgHirLHGVixi6V76LhCkZUz6pnFt5AJBiyvHye/avatar/170.jpg' - * - * @since 2.0.1 - */ - avatar(): string { - return this.faker.internet.avatar(); - } - - /** * Generates a random image url. * * @param width The width of the image. Defaults to `640`. @@ -96,6 +362,8 @@ export class ImageModule { * faker.image.imageUrl(1234, 2345, 'cat', true) // 'https://loremflickr.com/1234/2345/cat?lock=6849' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ imageUrl( width?: number, @@ -103,6 +371,13 @@ export class ImageModule { category?: string, randomize?: boolean ): string { + deprecated({ + deprecated: 'faker.image.imageUrl', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); + width = width || 640; height = height || 480; let url = `https://loremflickr.com/${width}/${height}`; @@ -130,8 +405,16 @@ export class ImageModule { * faker.image.abstract(1234, 2345, true) // 'https://loremflickr.com/1234/2345/abstract?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ abstract(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.abstract', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, 'abstract', randomize); } @@ -148,8 +431,16 @@ export class ImageModule { * faker.image.animals(1234, 2345, true) // 'https://loremflickr.com/1234/2345/animals?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ animals(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.animals', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, 'animals', randomize); } @@ -166,8 +457,16 @@ export class ImageModule { * faker.image.business(1234, 2345, true) // 'https://loremflickr.com/1234/2345/business?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ business(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.business', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, 'business', randomize); } @@ -184,8 +483,16 @@ export class ImageModule { * faker.image.cats(1234, 2345, true) // 'https://loremflickr.com/1234/2345/cats?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ cats(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.cats', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, 'cats', randomize); } @@ -202,8 +509,16 @@ export class ImageModule { * faker.image.city(1234, 2345, true) // 'https://loremflickr.com/1234/2345/city?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ city(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.city', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, 'city', randomize); } @@ -220,8 +535,16 @@ export class ImageModule { * faker.image.food(1234, 2345, true) // 'https://loremflickr.com/1234/2345/food?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ food(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.food', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, 'food', randomize); } @@ -238,8 +561,16 @@ export class ImageModule { * faker.image.nightlife(1234, 2345, true) // 'https://loremflickr.com/1234/2345/nightlife?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ nightlife(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.nightlife', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, 'nightlife', randomize); } @@ -256,8 +587,16 @@ export class ImageModule { * faker.image.fashion(1234, 2345, true) // 'https://loremflickr.com/1234/2345/fashion?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ fashion(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.fashion', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, 'fashion', randomize); } @@ -274,8 +613,16 @@ export class ImageModule { * faker.image.people(1234, 2345, true) // 'https://loremflickr.com/1234/2345/people?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ people(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.people', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, 'people', randomize); } @@ -292,8 +639,16 @@ export class ImageModule { * faker.image.nature(1234, 2345, true) // 'https://loremflickr.com/1234/2345/nature?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ nature(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.nature', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, 'nature', randomize); } @@ -310,8 +665,16 @@ export class ImageModule { * faker.image.sports(1234, 2345, true) // 'https://loremflickr.com/1234/2345/sports?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ sports(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.sports', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, 'sports', randomize); } @@ -328,8 +691,16 @@ export class ImageModule { * faker.image.technics(1234, 2345, true) // 'https://loremflickr.com/1234/2345/technics?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ technics(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.technics', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, 'technics', randomize); } @@ -346,30 +717,16 @@ export class ImageModule { * faker.image.transport(1234, 2345, true) // 'https://loremflickr.com/1234/2345/transport?lock=56789' * * @since 2.0.1 + * + * @deprecated Use `faker.image.url` instead. */ transport(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.image.transport', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, 'transport', randomize); } - - /** - * Generates a random data uri containing an svg image. - * - * @param width The width of the image. Defaults to `640`. - * @param height The height of the image. Defaults to `480`. - * @param color The color to use. Defaults to `grey`. - * - * @example - * faker.image.dataUri() // 'data:image/svg+xml;charset=UTF-8,%3Csvg%20xmlns%3D%22http...' - * - * @since 4.0.0 - */ - dataUri(width?: number, height?: number, color: string = 'grey'): string { - 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="${ - width / 2 - }" y="${ - height / 2 - }" font-size="20" alignment-baseline="middle" text-anchor="middle" fill="white">${width}x${height}</text></svg>`; - const rawPrefix = 'data:image/svg+xml;charset=UTF-8,'; - return rawPrefix + encodeURIComponent(svgString); - } } diff --git a/src/modules/image/providers/lorempicsum.ts b/src/modules/image/providers/lorempicsum.ts index d44673bd..f2f915ea 100644 --- a/src/modules/image/providers/lorempicsum.ts +++ b/src/modules/image/providers/lorempicsum.ts @@ -1,19 +1,23 @@ import type { Faker } from '../../..'; +import { deprecated } from '../../../internal/deprecated'; /** - * Module to generate links to random images on `https://picsum.photos/`. + * Module to generate links to random images on https://picsum.photos. + * + * @deprecated Use `faker.image.urlPicsumPhotos` instead. */ -// TODO ST-DDT 2022-03-11: Rename to picsum? export class LoremPicsum { constructor(private readonly faker: Faker) {} /** - * Generates a new picsum image url. + * Generates a new picsum photos image url. * * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param grayscale Whether to return a grayscale image. Default to `false`. * @param blur The optional level of blur to apply. Supports `1` - `10`. + * + * @deprecated Use `faker.image.urlPicsumPhotos` instead. */ image( width?: number, @@ -21,43 +25,67 @@ export class LoremPicsum { grayscale?: boolean, blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 ): string { + deprecated({ + deprecated: 'faker.lorempicsum.image', + proposed: 'faker.image.urlPicsumPhotos', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, grayscale, blur); } /** - * Generates a new picsum image url. + * Generates a new picsum photos image url. * * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param grayscale Whether to return a grayscale image. Default to `false`. + * + * @deprecated Use `faker.image.urlPicsumPhotos` instead. */ imageGrayscale(width?: number, height?: number, grayscale?: boolean): string { + deprecated({ + deprecated: 'faker.lorempicsum.imageGrayscale', + proposed: 'faker.image.urlPicsumPhotos', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, grayscale); } /** - * Generates a new picsum image url. + * Generates a new picsum photos image url. * * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param blur The optional level of blur to apply. Supports `1` - `10`. + * + * @deprecated Use `faker.image.urlPicsumPhotos` instead. */ imageBlurred( width?: number, height?: number, blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 ): string { + deprecated({ + deprecated: 'faker.lorempicsum.imageBlurred', + proposed: 'faker.image.urlPicsumPhotos', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, undefined, blur); } /** - * Generates a new picsum image url. + * Generates a new picsum photos image url. * * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param grayscale Whether to return a grayscale image. Default to `false`. * @param blur The optional level of blur to apply. Supports `1` - `10`. * @param seed The optional seed to use. + * + * @deprecated Use `faker.image.urlPicsumPhotos` instead. */ imageRandomSeeded( width?: number, @@ -66,18 +94,26 @@ export class LoremPicsum { blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10, seed?: string ): string { + deprecated({ + deprecated: 'faker.lorempicsum.imageRandomSeeded', + proposed: 'faker.image.urlPicsumPhotos', + since: '8.0', + until: '9.0', + }); // TODO ST-DDT 2022-03-11: This method does the same as image url, maybe generate a seed, if it is missig? return this.imageUrl(width, height, grayscale, blur, seed); } /** - * Generates a new picsum image url. + * Generates a new picsum photos image url. * * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param grayscale Whether to return a grayscale image. Default to `false`. * @param blur The optional level of blur to apply. Supports `1` - `10`. * @param seed The optional seed to use. + * + * @deprecated Use `faker.image.urlPicsumPhotos` instead. */ imageUrl( width?: number, @@ -86,6 +122,12 @@ export class LoremPicsum { blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10, seed?: string ): string { + deprecated({ + deprecated: 'faker.lorempicsum.imageUrl', + proposed: 'faker.image.urlPicsumPhotos', + since: '8.0', + until: '9.0', + }); width = width || 640; height = height || 480; diff --git a/src/modules/image/providers/lorempixel.ts b/src/modules/image/providers/lorempixel.ts index f8bb81e5..ac80a8d7 100644 --- a/src/modules/image/providers/lorempixel.ts +++ b/src/modules/image/providers/lorempixel.ts @@ -1,8 +1,11 @@ import type { Faker } from '../../..'; +import { deprecated } from '../../../internal/deprecated'; import type { MethodsOf } from '../../../utils/types'; /** * Module to generate links to random images on `https://lorempixel.com/`. + * + * @deprecated Use `faker.image` instead. */ export class Lorempixel { constructor(private readonly faker: Faker) {} @@ -13,8 +16,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ image(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.image', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); const categories: MethodsOf<Lorempixel, Lorempixel['image']> = [ 'abstract', 'animals', @@ -44,6 +55,8 @@ export class Lorempixel { * @param height The height of the image. Defaults to `480`. * @param category The category of the image to generate. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ imageUrl( width?: number, @@ -51,6 +64,12 @@ export class Lorempixel { category?: string, randomize?: boolean ): string { + deprecated({ + deprecated: 'faker.lorempixel.imageUrl', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); width = width || 640; height = height || 480; @@ -72,8 +91,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ abstract(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.abstract', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.lorempixel.imageUrl( width, height, @@ -88,8 +115,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ animals(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.animals', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.lorempixel.imageUrl( width, height, @@ -104,8 +139,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ business(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.business', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.lorempixel.imageUrl( width, height, @@ -120,8 +163,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ cats(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.cats', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.lorempixel.imageUrl( width, height, @@ -136,8 +187,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ city(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.city', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.lorempixel.imageUrl( width, height, @@ -152,8 +211,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ food(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.food', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.lorempixel.imageUrl( width, height, @@ -168,8 +235,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ nightlife(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.nightlife', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.lorempixel.imageUrl( width, height, @@ -184,8 +259,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ fashion(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.fashion', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.lorempixel.imageUrl( width, height, @@ -200,8 +283,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ people(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.people', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.lorempixel.imageUrl( width, height, @@ -216,8 +307,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ nature(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.nature', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.lorempixel.imageUrl( width, height, @@ -232,8 +331,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ sports(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.sports', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.lorempixel.imageUrl( width, height, @@ -248,8 +355,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ technics(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.technics', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.lorempixel.imageUrl( width, height, @@ -264,8 +379,16 @@ export class Lorempixel { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param randomize Whether to append a seed to the url. Defaults to `false`. + * + * @deprecated Use `faker.image` instead. */ transport(width?: number, height?: number, randomize?: boolean): string { + deprecated({ + deprecated: 'faker.lorempixel.transport', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.lorempixel.imageUrl( width, height, diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index 224dccfe..d2cc9f51 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -1,7 +1,10 @@ import type { Faker } from '../../..'; +import { deprecated } from '../../../internal/deprecated'; /** * Module to generate links to images on `https://via.placeholder.com/`. + * + * @deprecated Use `faker.image.urlPlaceholder` instead. */ export class Placeholder { constructor(private readonly faker: Faker) { @@ -33,6 +36,7 @@ export class Placeholder { * faker.image.placeholder.imageUrl(200, 100, 'Fish', 'webp') // https://via.placeholder.com/200x100.webp?text=Fish * faker.image.placeholder.imageUrl(200, 100, 'Fish', 'webp', '000000', 'ffffff) // https://via.placeholder.com/200x100/000000/FFFFFF.webp?text=Fish * + * @deprecated Use `faker.image.urlPlaceholder` instead. */ imageUrl( width?: number, @@ -42,6 +46,12 @@ export class Placeholder { backgroundColor?: string, textColor?: string ): string { + deprecated({ + deprecated: 'faker.placeholder.imageUrl', + proposed: 'faker.image.urlPlaceholder', + since: '8.0', + until: '9.0', + }); width = width || 640; height = height || width; @@ -80,12 +90,20 @@ export class Placeholder { * faker.image.placeholder.randomUrl(150) // https://via.placeholder.com/150x150/000000/ffffff?text=lorum * faker.image.placeholder.randomUrl(150, 200) // https://via.placeholder.com/150x200/000000/ffffff?text=lorum * faker.image.placeholder.randomUrl(150, 200, 'png') // https://via.placeholder.com/150x200/000000/ffffff.png?text=lorum + * + * @deprecated Use `faker.image.urlPlaceholder` instead. */ randomUrl( width?: number, height?: number, format?: 'png' | 'jpeg' | 'jpg' | 'gif' | 'webp' ): string { + deprecated({ + deprecated: 'faker.placeholder.randomUrl', + proposed: 'faker.image.urlPlaceholder', + since: '8.0', + until: '9.0', + }); return this.imageUrl( width, height, diff --git a/src/modules/image/providers/unsplash.ts b/src/modules/image/providers/unsplash.ts index 8fc2e324..aaaae9c7 100644 --- a/src/modules/image/providers/unsplash.ts +++ b/src/modules/image/providers/unsplash.ts @@ -1,7 +1,10 @@ import type { Faker } from '../../..'; +import { deprecated } from '../../../internal/deprecated'; /** * Module to generate links to random images on `https://source.unsplash.com/`. + * + * @deprecated Use `faker.image` instead. */ export class Unsplash { constructor(private readonly faker: Faker) {} @@ -12,8 +15,16 @@ export class Unsplash { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param keyword The image keywords to use. + * + * @deprecated Use `faker.image` instead. */ image(width?: number, height?: number, keyword?: string): string { + deprecated({ + deprecated: 'faker.unsplash.image', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.imageUrl(width, height, undefined, keyword); } @@ -24,6 +35,8 @@ export class Unsplash { * @param height The height of the image. Defaults to `480`. * @param category The category of the image to generate. * @param keyword The image keywords to use. + * + * @deprecated Use `faker.image` instead. */ imageUrl( width?: number, @@ -31,6 +44,12 @@ export class Unsplash { category?: string, keyword?: string ): string { + deprecated({ + deprecated: 'faker.unsplash.imageUrl', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); width = width || 640; height = height || 480; @@ -58,8 +77,16 @@ export class Unsplash { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param keyword The image keywords to use. + * + * @deprecated Use `faker.image` instead. */ food(width?: number, height?: number, keyword?: string): string { + deprecated({ + deprecated: 'faker.unsplash.food', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.unsplash.imageUrl(width, height, 'food', keyword); } @@ -69,8 +96,16 @@ export class Unsplash { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param keyword The image keywords to use. + * + * @deprecated Use `faker.image` instead. */ people(width?: number, height?: number, keyword?: string): string { + deprecated({ + deprecated: 'faker.unsplash.people', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.unsplash.imageUrl(width, height, 'people', keyword); } @@ -80,8 +115,16 @@ export class Unsplash { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param keyword The image keywords to use. + * + * @deprecated Use `faker.image` instead. */ nature(width?: number, height?: number, keyword?: string): string { + deprecated({ + deprecated: 'faker.unsplash.nature', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.unsplash.imageUrl(width, height, 'nature', keyword); } @@ -91,8 +134,16 @@ export class Unsplash { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param keyword The image keywords to use. + * + * @deprecated Use `faker.image` instead. */ technology(width?: number, height?: number, keyword?: string): string { + deprecated({ + deprecated: 'faker.unsplash.technology', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.unsplash.imageUrl( width, height, @@ -107,8 +158,16 @@ export class Unsplash { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param keyword The image keywords to use. + * + * @deprecated Use `faker.image` instead. */ objects(width?: number, height?: number, keyword?: string): string { + deprecated({ + deprecated: 'faker.unsplash.objects', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.unsplash.imageUrl( width, height, @@ -123,8 +182,16 @@ export class Unsplash { * @param width The width of the image. Defaults to `640`. * @param height The height of the image. Defaults to `480`. * @param keyword The image keywords to use. + * + * @deprecated Use `faker.image` instead. */ buildings(width?: number, height?: number, keyword?: string): string { + deprecated({ + deprecated: 'faker.unsplash.buildings', + proposed: 'faker.image.url', + since: '8.0', + until: '9.0', + }); return this.faker.image.unsplash.imageUrl( width, height, |
