diff options
| author | Shinigami <[email protected]> | 2022-01-14 17:11:50 +0100 |
|---|---|---|
| committer | Damien Retzinger <[email protected]> | 2022-01-14 18:37:49 -0500 |
| commit | 3c3e567f4d9d901770a76bf30068a6742a00d882 (patch) | |
| tree | 98d39bcb556ffaeefbd21a8caedcd7bfc2b86151 /src | |
| parent | 8fcfcc6b1a64f078ad14b4a434ffb2969487aca1 (diff) | |
| download | faker-3c3e567f4d9d901770a76bf30068a6742a00d882.tar.xz faker-3c3e567f4d9d901770a76bf30068a6742a00d882.zip | |
feat: migrate image (#92)
Diffstat (limited to 'src')
| -rw-r--r-- | src/image.ts | 290 | ||||
| -rw-r--r-- | src/index.ts | 3 |
2 files changed, 292 insertions, 1 deletions
diff --git a/src/image.ts b/src/image.ts new file mode 100644 index 00000000..e16fa348 --- /dev/null +++ b/src/image.ts @@ -0,0 +1,290 @@ +import type { Faker } from '.'; + +const Lorempixel = require('./image_providers/lorempixel'); +const Unsplash = require('./image_providers/unsplash'); +const LoremPicsum = require('./image_providers/lorempicsum'); + +/** + * Default provider is unsplash image provider. + */ +export class Image { + readonly lorempixel: typeof Lorempixel; + readonly unsplash: typeof Unsplash; + readonly lorempicsum: typeof LoremPicsum; + + constructor(private readonly faker: Faker) { + // Bind `this` so namespaced is working correctly + for (const name of Object.getOwnPropertyNames(Image.prototype)) { + if (name === 'constructor' || typeof this[name] !== 'function') { + continue; + } + this[name] = this[name].bind(this); + } + + this.lorempixel = new Lorempixel(this.faker); + this.unsplash = new Unsplash(this.faker); + this.lorempicsum = new LoremPicsum(this.faker); + } + + /** + * image + * + * @method faker.image.image + * @param width + * @param height + * @param randomize + */ + 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.avatar + */ + avatar(): string { + return this.faker.internet.avatar(); + } + + /** + * imageUrl + * + * @method faker.image.imageUrl + * @param width + * @param height + * @param category + * @param randomize + */ + imageUrl( + width?: number, + height?: number, + category?: string, + randomize?: boolean, + https?: boolean + ): string { + width ||= 640; + height ||= 480; + let protocol = 'http://'; + if (typeof https !== 'undefined' && https === true) { + protocol = 'https://'; + } + let url = protocol + 'placeimg.com/' + width + '/' + height; + if (typeof category !== 'undefined') { + url += '/' + category; + } + + if (randomize) { + url += '?' + this.faker.datatype.number(); + } + + return url; + } + + /** + * abstract + * + * @method faker.image.abstract + * @param width + * @param height + * @param randomize + */ + abstract(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.imageUrl(width, height, 'abstract', randomize); + } + + /** + * animals + * + * @method faker.image.animals + * @param width + * @param height + * @param randomize + */ + animals(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.imageUrl(width, height, 'animals', randomize); + } + + /** + * business + * + * @method faker.image.business + * @param width + * @param height + * @param randomize + */ + business(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.imageUrl(width, height, 'business', randomize); + } + + /** + * cats + * + * @method faker.image.cats + * @param width + * @param height + * @param randomize + */ + cats(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.imageUrl(width, height, 'cats', randomize); + } + + /** + * city + * + * @method faker.image.city + * @param width + * @param height + * @param randomize + */ + city(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.imageUrl(width, height, 'city', randomize); + } + + /** + * food + * + * @method faker.image.food + * @param width + * @param height + * @param randomize + */ + food(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.imageUrl(width, height, 'food', randomize); + } + + /** + * nightlife + * + * @method faker.image.nightlife + * @param width + * @param height + * @param randomize + */ + nightlife(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.imageUrl(width, height, 'nightlife', randomize); + } + + /** + * fashion + * + * @method faker.image.fashion + * @param width + * @param height + * @param randomize + */ + fashion(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.imageUrl(width, height, 'fashion', randomize); + } + + /** + * people + * + * @method faker.image.people + * @param width + * @param height + * @param randomize + */ + people(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.imageUrl(width, height, 'people', randomize); + } + + /** + * nature + * + * @method faker.image.nature + * @param width + * @param height + * @param randomize + */ + nature(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.imageUrl(width, height, 'nature', randomize); + } + + /** + * sports + * + * @method faker.image.sports + * @param width + * @param height + * @param randomize + */ + sports(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.imageUrl(width, height, 'sports', randomize); + } + + /** + * technics + * + * @method faker.image.technics + * @param width + * @param height + * @param randomize + */ + technics(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.imageUrl(width, height, 'technics', randomize); + } + + /** + * transport + * + * @method faker.image.transport + * @param width + * @param height + * @param randomize + */ + transport(width?: number, height?: number, randomize?: boolean): string { + return this.faker.image.imageUrl(width, height, 'transport', randomize); + } + + /** + * dataUri + * + * @method faker.image.dataUri + * @param width + * @param height + * @param color + */ + 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); + } + + // Object.assign(self, self.unsplash); + // How to set default as unsplash? should be image.default? +} diff --git a/src/index.ts b/src/index.ts index 5a14336b..e41569c5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import { Fake } from './fake'; import { Git } from './git'; import { Hacker } from './hacker'; import { Helpers } from './helpers'; +import { Image } from './image'; import { Internet } from './internet'; import { Mersenne } from './mersenne'; import { Name } from './name'; @@ -180,7 +181,7 @@ export class Faker { readonly hacker: Hacker = new Hacker(this); // TODO @Shinigami92 2022-01-12: iban was not used // readonly iban = new (require('./iban'))(this); - readonly image = new (require('./image'))(this); + readonly image: Image = new Image(this); readonly internet: Internet = new Internet(this); readonly lorem = new (require('./lorem'))(this); readonly music = new (require('./music'))(this); |
