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 | |
| parent | a973ee1ce64d2b593f64b633e927382280da8f27 (diff) | |
| download | faker-8f9fd7be31c7daf00cc4c04c6aca3ca63e6f29a1.tar.xz faker-8f9fd7be31c7daf00cc4c04c6aca3ca63e6f29a1.zip | |
chore: migrate image providers to TS (#178)
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | lib/image_providers/lorempicsum.js | 106 | ||||
| -rw-r--r-- | lib/image_providers/lorempixel.js | 241 | ||||
| -rw-r--r-- | lib/image_providers/unsplash.js | 137 | ||||
| -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 |
8 files changed, 590 insertions, 492 deletions
@@ -87,5 +87,4 @@ REVISION /lib/* !/lib/locales.js -!/lib/image_providers !/lib/locales diff --git a/lib/image_providers/lorempicsum.js b/lib/image_providers/lorempicsum.js deleted file mode 100644 index 65639da7..00000000 --- a/lib/image_providers/lorempicsum.js +++ /dev/null @@ -1,106 +0,0 @@ -/** - * - * @namespace lorempicsum - * @memberof faker.image - */ -var LoremPicsum = function (faker) { - var self = this; - - /** - * image - * - * @param {number} width - * @param {number} height - * @param {boolean} grayscale - * @param {number} blur 1-10 - * @method faker.image.lorempicsum.image - * @description search image from unsplash - */ - self.image = function (width, height, grayscale, blur) { - return self.imageUrl(width, height, grayscale, blur); - }; - /** - * imageGrayscaled - * - * @param {number} width - * @param {number} height - * @param {boolean} grayscale - * @method faker.image.lorempicsum.imageGrayscaled - * @description search grayscale image from unsplash - */ - self.imageGrayscale = function (width, height, grayscale) { - return self.imageUrl(width, height, grayscale); - }; - /** - * imageBlurred - * - * @param {number} width - * @param {number} height - * @param {number} blur 1-10 - * @method faker.image.lorempicsum.imageBlurred - * @description search blurred image from unsplash - */ - self.imageBlurred = function (width, height, blur) { - return self.imageUrl(width, height, undefined, blur); - }; - /** - * imageRandomSeeded - * - * @param {number} width - * @param {number} height - * @param {boolean} grayscale - * @param {number} blur 1-10 - * @param {string} seed - * @method faker.image.lorempicsum.imageRandomSeeded - * @description search same random image from unsplash, based on a seed - */ - self.imageRandomSeeded = function (width, height, grayscale, blur, seed) { - return self.imageUrl(width, height, grayscale, blur, seed); - }; - /** - * avatar - * - * @method faker.image.lorempicsum.avatar - */ - self.avatar = function () { - return faker.internet.avatar(); - }; - /** - * imageUrl - * - * @param {number} width - * @param {number} height - * @param {boolean} grayscale - * @param {number} blur 1-10 - * @param {string} seed - * @method faker.image.lorempicsum.imageUrl - */ - self.imageUrl = function (width, height, grayscale, blur, seed) { - var width = width || 640; - var height = height || 480; - - var 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; - }; -}; - -module['exports'] = LoremPicsum; diff --git a/lib/image_providers/lorempixel.js b/lib/image_providers/lorempixel.js deleted file mode 100644 index 24ad4806..00000000 --- a/lib/image_providers/lorempixel.js +++ /dev/null @@ -1,241 +0,0 @@ -/** - * - * @namespace lorempixel - * @memberof faker.image - */ -var Lorempixel = function (faker) { - var self = this; - - /** - * image - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.image - */ - self.image = function (width, height, randomize) { - var categories = [ - 'abstract', - 'animals', - 'business', - 'cats', - 'city', - 'food', - 'nightlife', - 'fashion', - 'people', - 'nature', - 'sports', - 'technics', - 'transport', - ]; - return self[faker.random.arrayElement(categories)]( - width, - height, - randomize - ); - }; - /** - * avatar - * - * @method faker.image.lorempixel.avatar - */ - self.avatar = function () { - return faker.internet.avatar(); - }; - /** - * imageUrl - * - * @param {number} width - * @param {number} height - * @param {string} category - * @param {boolean} randomize - * @method faker.image.lorempixel.imageUrl - */ - self.imageUrl = function (width, height, category, randomize) { - var width = width || 640; - var height = height || 480; - - var url = 'https://lorempixel.com/' + width + '/' + height; - if (typeof category !== 'undefined') { - url += '/' + category; - } - - if (randomize) { - url += '?' + faker.datatype.number(); - } - - return url; - }; - /** - * abstract - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.abstract - */ - self.abstract = function (width, height, randomize) { - return faker.image.lorempixel.imageUrl( - width, - height, - 'abstract', - randomize - ); - }; - /** - * animals - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.animals - */ - self.animals = function (width, height, randomize) { - return faker.image.lorempixel.imageUrl(width, height, 'animals', randomize); - }; - /** - * business - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.business - */ - self.business = function (width, height, randomize) { - return faker.image.lorempixel.imageUrl( - width, - height, - 'business', - randomize - ); - }; - /** - * cats - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.cats - */ - self.cats = function (width, height, randomize) { - return faker.image.lorempixel.imageUrl(width, height, 'cats', randomize); - }; - /** - * city - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.city - */ - self.city = function (width, height, randomize) { - return faker.image.lorempixel.imageUrl(width, height, 'city', randomize); - }; - /** - * food - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.food - */ - self.food = function (width, height, randomize) { - return faker.image.lorempixel.imageUrl(width, height, 'food', randomize); - }; - /** - * nightlife - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.nightlife - */ - self.nightlife = function (width, height, randomize) { - return faker.image.lorempixel.imageUrl( - width, - height, - 'nightlife', - randomize - ); - }; - /** - * fashion - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.fashion - */ - self.fashion = function (width, height, randomize) { - return faker.image.lorempixel.imageUrl(width, height, 'fashion', randomize); - }; - /** - * people - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.people - */ - self.people = function (width, height, randomize) { - return faker.image.lorempixel.imageUrl(width, height, 'people', randomize); - }; - /** - * nature - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.nature - */ - self.nature = function (width, height, randomize) { - return faker.image.lorempixel.imageUrl(width, height, 'nature', randomize); - }; - /** - * sports - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.sports - */ - self.sports = function (width, height, randomize) { - return faker.image.lorempixel.imageUrl(width, height, 'sports', randomize); - }; - /** - * technics - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.technics - */ - self.technics = function (width, height, randomize) { - return faker.image.lorempixel.imageUrl( - width, - height, - 'technics', - randomize - ); - }; - /** - * transport - * - * @param {number} width - * @param {number} height - * @param {boolean} randomize - * @method faker.image.lorempixel.transport - */ - self.transport = function (width, height, randomize) { - return faker.image.lorempixel.imageUrl( - width, - height, - 'transport', - randomize - ); - }; -}; - -module['exports'] = Lorempixel; diff --git a/lib/image_providers/unsplash.js b/lib/image_providers/unsplash.js deleted file mode 100644 index e53f270a..00000000 --- a/lib/image_providers/unsplash.js +++ /dev/null @@ -1,137 +0,0 @@ -/** - * - * @namespace unsplash - * @memberof faker.image - */ -var Unsplash = function (faker) { - var self = this; - var categories = [ - 'food', - 'nature', - 'people', - 'technology', - 'objects', - 'buildings', - ]; - - /** - * image - * - * @param {number} width - * @param {number} height - * @param {string} keyword - * @method faker.image.unsplash.image - * @description search image from unsplash - */ - self.image = function (width, height, keyword) { - return self.imageUrl(width, height, undefined, keyword); - }; - /** - * avatar - * - * @method faker.image.unsplash.avatar - */ - self.avatar = function () { - return faker.internet.avatar(); - }; - /** - * imageUrl - * - * @param {number} width - * @param {number} height - * @param {string} category - * @param {string} keyword - * @method faker.image.unsplash.imageUrl - */ - self.imageUrl = function (width, height, category, keyword) { - var width = width || 640; - var height = height || 480; - - var url = 'https://source.unsplash.com'; - - if (typeof category !== 'undefined') { - url += '/category/' + category; - } - - url += '/' + width + 'x' + height; - - if (typeof keyword !== 'undefined') { - var 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 {number} width - * @param {number} height - * @param {string} keyword - * @method faker.image.unsplash.food - */ - self.food = function (width, height, keyword) { - return faker.image.unsplash.imageUrl(width, height, 'food', keyword); - }; - /** - * people - * - * @param {number} width - * @param {number} height - * @param {string} keyword - * @method faker.image.unsplash.people - */ - self.people = function (width, height, keyword) { - return faker.image.unsplash.imageUrl(width, height, 'people', keyword); - }; - /** - * nature - * - * @param {number} width - * @param {number} height - * @param {string} keyword - * @method faker.image.unsplash.nature - */ - self.nature = function (width, height, keyword) { - return faker.image.unsplash.imageUrl(width, height, 'nature', keyword); - }; - /** - * technology - * - * @param {number} width - * @param {number} height - * @param {string} keyword - * @method faker.image.unsplash.technology - */ - self.technology = function (width, height, keyword) { - return faker.image.unsplash.imageUrl(width, height, 'technology', keyword); - }; - /** - * objects - * - * @param {number} width - * @param {number} height - * @param {string} keyword - * @method faker.image.unsplash.objects - */ - self.objects = function (width, height, keyword) { - return faker.image.unsplash.imageUrl(width, height, 'objects', keyword); - }; - /** - * buildings - * - * @param {number} width - * @param {number} height - * @param {string} keyword - * @method faker.image.unsplash.buildings - */ - self.buildings = function (width, height, keyword) { - return faker.image.unsplash.imageUrl(width, height, 'buildings', keyword); - }; -}; - -module['exports'] = Unsplash; 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 + ); + } +} |
