diff options
| author | Shinigami <[email protected]> | 2022-01-18 12:00:58 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-01-18 12:00:58 +0100 |
| commit | 8f9fd7be31c7daf00cc4c04c6aca3ca63e6f29a1 (patch) | |
| tree | 29ce8c1b8249bf98e0286f20c5d68ffbaf5d8937 /src | |
| parent | a973ee1ce64d2b593f64b633e927382280da8f27 (diff) | |
| download | faker-8f9fd7be31c7daf00cc4c04c6aca3ca63e6f29a1.tar.xz faker-8f9fd7be31c7daf00cc4c04c6aca3ca63e6f29a1.zip | |
chore: migrate image providers to TS (#178)
Diffstat (limited to 'src')
| -rw-r--r-- | src/image.ts | 13 | ||||
| -rw-r--r-- | src/image_providers/lorempicsum.ts | 127 | ||||
| -rw-r--r-- | src/image_providers/lorempixel.ts | 296 | ||||
| -rw-r--r-- | src/image_providers/unsplash.ts | 161 |
4 files changed, 590 insertions, 7 deletions
diff --git a/src/image.ts b/src/image.ts index e16fa348..00e94114 100644 --- a/src/image.ts +++ b/src/image.ts @@ -1,16 +1,15 @@ import type { Faker } from '.'; - -const Lorempixel = require('./image_providers/lorempixel'); -const Unsplash = require('./image_providers/unsplash'); -const LoremPicsum = require('./image_providers/lorempicsum'); +import { Lorempixel } from './image_providers/lorempixel'; +import { Unsplash } from './image_providers/unsplash'; +import { LoremPicsum } from './image_providers/lorempicsum'; /** * Default provider is unsplash image provider. */ export class Image { - readonly lorempixel: typeof Lorempixel; - readonly unsplash: typeof Unsplash; - readonly lorempicsum: typeof LoremPicsum; + readonly lorempixel: Lorempixel; + readonly unsplash: Unsplash; + readonly lorempicsum: LoremPicsum; constructor(private readonly faker: Faker) { // Bind `this` so namespaced is working correctly diff --git a/src/image_providers/lorempicsum.ts b/src/image_providers/lorempicsum.ts new file mode 100644 index 00000000..32c52867 --- /dev/null +++ b/src/image_providers/lorempicsum.ts @@ -0,0 +1,127 @@ +import type { Faker } from '..'; + +export class LoremPicsum { + constructor(private readonly faker: Faker) {} + + /** + * image + * + * @param width + * @param height + * @param grayscale + * @param blur 1-10 + * @method faker.image.lorempicsum.image + * @description search image from unsplash + */ + image( + width?: number, + height?: number, + grayscale?: boolean, + blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 + ): string { + return this.imageUrl(width, height, grayscale, blur); + } + + /** + * imageGrayscaled + * + * @param width + * @param height + * @param grayscale + * @method faker.image.lorempicsum.imageGrayscaled + * @description search grayscale image from unsplash + */ + imageGrayscale(width?: number, height?: number, grayscale?: boolean): string { + return this.imageUrl(width, height, grayscale); + } + + /** + * imageBlurred + * + * @param width + * @param height + * @param blur 1-10 + * @method faker.image.lorempicsum.imageBlurred + * @description search blurred image from unsplash + */ + imageBlurred( + width?: number, + height?: number, + blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 + ): string { + return this.imageUrl(width, height, undefined, blur); + } + + /** + * imageRandomSeeded + * + * @param width + * @param height + * @param grayscale + * @param blur 1-10 + * @param seed + * @method faker.image.lorempicsum.imageRandomSeeded + * @description search same random image from unsplash, based on a seed + */ + imageRandomSeeded( + width?: number, + height?: number, + grayscale?: boolean, + blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10, + seed?: string + ): string { + return this.imageUrl(width, height, grayscale, blur, seed); + } + + /** + * avatar + * + * @method faker.image.lorempicsum.avatar + */ + avatar(): string { + return this.faker.internet.avatar(); + } + + /** + * imageUrl + * + * @param width + * @param height + * @param grayscale + * @param blur 1-10 + * @param seed + * @method faker.image.lorempicsum.imageUrl + */ + imageUrl( + width?: number, + height?: number, + grayscale?: boolean, + blur?: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10, + seed?: string + ): string { + width ||= 640; + height ||= 480; + + let url = 'https://picsum.photos'; + + if (seed) { + url += '/seed/' + seed; + } + + url += '/' + width + '/' + height; + + if (grayscale && blur) { + return url + '?grayscale' + '&blur=' + blur; + } + + if (grayscale) { + return url + '?grayscale'; + } + + if (blur) { + return url + '?blur=' + blur; + } + + return url; + } +} diff --git a/src/image_providers/lorempixel.ts b/src/image_providers/lorempixel.ts new file mode 100644 index 00000000..d152cd6e --- /dev/null +++ b/src/image_providers/lorempixel.ts @@ -0,0 +1,296 @@ +import type { Faker } from '..'; + +export class Lorempixel { + constructor(private readonly faker: Faker) {} + + /** + * image + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.image + */ + image(width?: number, height?: number, randomize?: boolean): string { + const categories = [ + 'abstract', + 'animals', + 'business', + 'cats', + 'city', + 'food', + 'nightlife', + 'fashion', + 'people', + 'nature', + 'sports', + 'technics', + 'transport', + ]; + return this[this.faker.random.arrayElement(categories)]( + width, + height, + randomize + ); + } + + /** + * avatar + * + * @method faker.image.lorempixel.avatar + */ + avatar(): string { + return this.faker.internet.avatar(); + } + + /** + * imageUrl + * + * @param width + * @param height + * @param category + * @param randomize + * @method faker.image.lorempixel.imageUrl + */ + imageUrl( + width?: number, + height?: number, + category?: string, + randomize?: boolean + ): string { + width ||= 640; + height ||= 480; + + let url = 'https://lorempixel.com/' + width + '/' + height; + if (typeof category !== 'undefined') { + url += '/' + category; + } + + if (randomize) { + url += '?' + this.faker.datatype.number(); + } + + return url; + } + + /** + * abstract + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.abstract + */ + abstract(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'abstract', + randomize + ); + } + + /** + * animals + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.animals + */ + animals(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'animals', + randomize + ); + } + + /** + * business + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.business + */ + business(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'business', + randomize + ); + } + + /** + * cats + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.cats + */ + cats(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'cats', + randomize + ); + } + + /** + * city + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.city + */ + city(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'city', + randomize + ); + } + + /** + * food + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.food + */ + food(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'food', + randomize + ); + } + + /** + * nightlife + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.nightlife + */ + nightlife(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'nightlife', + randomize + ); + } + + /** + * fashion + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.fashion + */ + fashion(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'fashion', + randomize + ); + } + + /** + * people + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.people + */ + people(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'people', + randomize + ); + } + + /** + * nature + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.nature + */ + nature(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'nature', + randomize + ); + } + + /** + * sports + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.sports + */ + sports(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'sports', + randomize + ); + } + + /** + * technics + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.technics + */ + technics(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'technics', + randomize + ); + } + + /** + * transport + * + * @param width + * @param height + * @param randomize + * @method faker.image.lorempixel.transport + */ + transport(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.lorempixel.imageUrl( + width, + height, + 'transport', + randomize + ); + } +} diff --git a/src/image_providers/unsplash.ts b/src/image_providers/unsplash.ts new file mode 100644 index 00000000..269920f2 --- /dev/null +++ b/src/image_providers/unsplash.ts @@ -0,0 +1,161 @@ +import type { Faker } from '..'; + +export class Unsplash { + categories = [ + 'food', + 'nature', + 'people', + 'technology', + 'objects', + 'buildings', + ]; + + constructor(private readonly faker: Faker) {} + + /** + * image + * + * @param width + * @param height + * @param keyword + * @method faker.image.unsplash.image + * @description search image from unsplash + */ + image(width?: number, height?: number, keyword?: string): string { + return this.imageUrl(width, height, undefined, keyword); + } + + /** + * avatar + * + * @method faker.image.unsplash.avatar + */ + avatar() { + return this.faker.internet.avatar(); + } + + /** + * imageUrl + * + * @param width + * @param height + * @param category + * @param keyword + * @method faker.image.unsplash.imageUrl + */ + imageUrl( + width?: number, + height?: number, + category?: string, + keyword?: string + ): string { + width ||= 640; + height ||= 480; + + let url = 'https://source.unsplash.com'; + + if (typeof category !== 'undefined') { + url += '/category/' + category; + } + + url += '/' + width + 'x' + height; + + if (typeof keyword !== 'undefined') { + const keywordFormat = new RegExp( + '^([A-Za-z0-9].+,[A-Za-z0-9]+)$|^([A-Za-z0-9]+)$' + ); + if (keywordFormat.test(keyword)) { + url += '?' + keyword; + } + } + + return url; + } + + /** + * food + * + * @param width + * @param height + * @param keyword + * @method faker.image.unsplash.food + */ + food(width?: number, height?: number, keyword?: string): string { + return this.faker.image.unsplash.imageUrl(width, height, 'food', keyword); + } + + /** + * people + * + * @param width + * @param height + * @param keyword + * @method faker.image.unsplash.people + */ + people(width?: number, height?: number, keyword?: string): string { + return this.faker.image.unsplash.imageUrl(width, height, 'people', keyword); + } + + /** + * nature + * + * @param width + * @param height + * @param keyword + * @method faker.image.unsplash.nature + */ + nature(width?: number, height?: number, keyword?: string): string { + return this.faker.image.unsplash.imageUrl(width, height, 'nature', keyword); + } + + /** + * technology + * + * @param width + * @param height + * @param keyword + * @method faker.image.unsplash.technology + */ + technology(width?: number, height?: number, keyword?: string): string { + return this.faker.image.unsplash.imageUrl( + width, + height, + 'technology', + keyword + ); + } + + /** + * objects + * + * @param width + * @param height + * @param keyword + * @method faker.image.unsplash.objects + */ + objects(width?: number, height?: number, keyword?: string): string { + return this.faker.image.unsplash.imageUrl( + width, + height, + 'objects', + keyword + ); + } + + /** + * buildings + * + * @param width + * @param height + * @param keyword + * @method faker.image.unsplash.buildings + */ + buildings(width?: number, height?: number, keyword?: string): string { + return this.faker.image.unsplash.imageUrl( + width, + height, + 'buildings', + keyword + ); + } +} |
