From ca5a910911cec91f9f65767f99c5dcb23c760708 Mon Sep 17 00:00:00 2001 From: Adegoke Temitope Date: Sun, 24 Jul 2022 09:06:57 +0100 Subject: [PATCH 01/17] feat(image): add placeholder.com as module --- src/modules/image/index.ts | 3 + src/modules/image/providers/placeholder.ts | 64 ++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/modules/image/providers/placeholder.ts diff --git a/src/modules/image/index.ts b/src/modules/image/index.ts index 5ca06c71cd7..08cef08ff59 100644 --- a/src/modules/image/index.ts +++ b/src/modules/image/index.ts @@ -2,6 +2,7 @@ import type { Faker } from '../..'; import type { MethodsOf } from '../../utils/types'; import { LoremPicsum } from './providers/lorempicsum'; import { Lorempixel } from './providers/lorempixel'; +import { Placeholder } from './providers/placeholder'; import { Unsplash } from './providers/unsplash'; /** @@ -13,6 +14,7 @@ export class Image { readonly lorempixel: Lorempixel; readonly unsplash: Unsplash; readonly lorempicsum: LoremPicsum; + readonly placeholder: Placeholder; constructor(private readonly faker: Faker) { // Bind `this` so namespaced is working correctly @@ -26,6 +28,7 @@ export class Image { this.lorempixel = new Lorempixel(this.faker); this.unsplash = new Unsplash(this.faker); this.lorempicsum = new LoremPicsum(this.faker); + this.placeholder = new Placeholder(this.faker); } /** diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts new file mode 100644 index 00000000000..2e18cb15f43 --- /dev/null +++ b/src/modules/image/providers/placeholder.ts @@ -0,0 +1,64 @@ +import type { Faker } from '../../..'; + +/** + * Module to generate links to images on `https://via.placeholder.com/`. + */ +export class Placeholder { + constructor(private readonly faker: Faker) {} + + /** + * Generates a new placeholder image url + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `480`. + * @param text The text of the image + */ + image(width?: number, height?: number, text?: string): string { + return this.imageUrl(width, height, text); + } + + /** + * Generates a new placeholder image url. + * + * @param width The width of the image. Defaults to `640`. + * @param height The height of the image. Defaults to `width`. + * @param text The text of the image + * @param format The file format of the image. Supports `png` `jpeg` `png` `gif` `webp` + * @param backgroundColor The background color of the placeholder. Supports HEX CODE format + * @param textColor The text color of the placeholder. Requires `backgroundColor` Supports HEX CODE format + */ + imageUrl( + width?: number, + height?: number, + text?: string, + format?: 'png' | 'jpeg' | 'jpg' | 'gif' | 'webp', + backgroundColor?: string, + textColor?: string + ): string { + width = width || 640; + height = height || width; + + let url = 'https://via.placeholder.com'; + + url += `/${width}/${height}`; + + if (backgroundColor != null) { + url += `/${backgroundColor.replace('#', '').toUpperCase()}`; + } + + if (textColor != null && backgroundColor !== null) { + url += `/${textColor.replace('#', '').toUpperCase()}`; + } + + if (format != null) { + url += `.${format}`; + } + + if (text != null) { + const urlParam = new URLSearchParams({ text }); + url += `?${urlParam.toString()}`; + } + + return url; + } +} From 53c6774510d9c03d7b525e9c03e6bce2229a9af0 Mon Sep 17 00:00:00 2001 From: Adegoke Temitope Date: Sun, 24 Jul 2022 09:07:47 +0100 Subject: [PATCH 02/17] test: tests for image placeholder.com module --- test/image.spec.ts | 94 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/test/image.spec.ts b/test/image.spec.ts index bbb99ffaaab..e99de3ad266 100644 --- a/test/image.spec.ts +++ b/test/image.spec.ts @@ -237,6 +237,100 @@ describe('image', () => { } }); + describe('placeholder', () => { + describe('imageUrl()', () => { + it('should return a random image url from placeholder', () => { + const imageUrl = faker.image.placeholder.imageUrl(); + + expect(imageUrl).toBe('https://via.placeholder.com/640/640'); + }); + + it('should return a square random image url from placeholder with width and height', () => { + const imageUrl = faker.image.placeholder.imageUrl(100); + + expect(imageUrl).toBe('https://via.placeholder.com/100/100'); + }); + + it('should return a random image url with a gif format', () => { + const imageUrl = faker.image.placeholder.imageUrl( + 100, + 100, + undefined, + 'gif' + ); + + expect(imageUrl).toBe('https://via.placeholder.com/100/100.gif'); + }); + + it('should return a random image url with correct text for a specified format', () => { + const imageUrl = faker.image.placeholder.imageUrl( + 100, + 100, + 'I love food', + 'png' + ); + + expect(imageUrl).toBe( + 'https://via.placeholder.com/100/100.png?text=I+love+food' + ); + }); + + it('should return a random image url with specified background color and text color', () => { + const imageUrl = faker.image.placeholder.imageUrl( + 100, + 100, + undefined, + undefined, + '000000', + 'ffffff' + ); + + expect(imageUrl).toBe( + 'https://via.placeholder.com/100/100/000000/FFFFFF' + ); + }); + + it('should return a random image url with specified background color and color without the #', () => { + const imageUrl = faker.image.placeholder.imageUrl( + 100, + 100, + undefined, + undefined, + '#000000', + '#ffffff' + ); + + expect(imageUrl).toBe( + 'https://via.placeholder.com/100/100/000000/FFFFFF' + ); + }); + + it('should return a random image url given all parameter', () => { + const imageUrl = faker.image.placeholder.imageUrl( + 100, + 200, + 'I love food', + 'jpg', + '000000', + 'ffffff' + ); + + expect(imageUrl).toBe( + 'https://via.placeholder.com/100/200/000000/FFFFFF.jpg?text=I+love+food' + ); + }); + }); + + describe('image()', () => { + it('should return a searching image url with text', () => { + const imageUrl = faker.image.placeholder.image(100, 200, 'I love food'); + expect(imageUrl).toBe( + 'https://via.placeholder.com/100/200?text=I+love+food' + ); + }); + }); + }); + describe('dataUri', () => { it('should return a blank data', () => { const dataUri = faker.image.dataUri(200, 300); From f2923815dd69f5b9358df54cba1b54e6d4bef983 Mon Sep 17 00:00:00 2001 From: Adegoke Temitope Date: Sun, 24 Jul 2022 17:28:30 +0100 Subject: [PATCH 03/17] feat(image) - include . after jsdoc comments - specifiy that width and height is in pixels --- src/modules/image/providers/placeholder.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index 2e18cb15f43..1061c4b6ea5 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -7,11 +7,11 @@ export class Placeholder { constructor(private readonly faker: Faker) {} /** - * Generates a new placeholder image url + * Generates a new placeholder image url. * - * @param width The width of the image. Defaults to `640`. - * @param height The height of the image. Defaults to `480`. - * @param text The text of the image + * @param width The width of the image (in pixels). Defaults to `640`. + * @param height The height of the image (in pixels). Defaults to `480`. + * @param text The text of the image. */ image(width?: number, height?: number, text?: string): string { return this.imageUrl(width, height, text); @@ -20,12 +20,12 @@ export class Placeholder { /** * Generates a new placeholder image url. * - * @param width The width of the image. Defaults to `640`. - * @param height The height of the image. Defaults to `width`. - * @param text The text of the image - * @param format The file format of the image. Supports `png` `jpeg` `png` `gif` `webp` - * @param backgroundColor The background color of the placeholder. Supports HEX CODE format - * @param textColor The text color of the placeholder. Requires `backgroundColor` Supports HEX CODE format + * @param width The width of the image (in pixels). Defaults to `640`. + * @param height The height of the image (in pixels). Defaults to `width`. + * @param text The text of the image. + * @param format The file format of the image. Supports `png` `jpeg` `png` `gif` `webp`. + * @param backgroundColor The background color of the placeholder. Supports HEX CODE format. + * @param textColor The text color of the placeholder. Requires `backgroundColor` Supports HEX CODE format. */ imageUrl( width?: number, @@ -39,7 +39,6 @@ export class Placeholder { height = height || width; let url = 'https://via.placeholder.com'; - url += `/${width}/${height}`; if (backgroundColor != null) { From e4d5892889ae0c1c145e2661ac1e7ea411723901 Mon Sep 17 00:00:00 2001 From: Adegoke Temitope Date: Mon, 25 Jul 2022 09:10:39 +0100 Subject: [PATCH 04/17] update height default value --- src/modules/image/providers/placeholder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index 1061c4b6ea5..c1aed9c6e7b 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -10,7 +10,7 @@ export class Placeholder { * Generates a new placeholder image url. * * @param width The width of the image (in pixels). Defaults to `640`. - * @param height The height of the image (in pixels). Defaults to `480`. + * @param height The height of the image (in pixels). Defaults to `width`. * @param text The text of the image. */ image(width?: number, height?: number, text?: string): string { From f174782e47c87a938351f401d5dc39c32dc71b1c Mon Sep 17 00:00:00 2001 From: Adegoke Temitope Date: Wed, 27 Jul 2022 16:05:10 +0100 Subject: [PATCH 05/17] remove image method, add randomUrl method --- src/modules/image/providers/placeholder.ts | 48 +++++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index c1aed9c6e7b..f8f13aa1fe6 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -6,17 +6,6 @@ import type { Faker } from '../../..'; export class Placeholder { constructor(private readonly faker: Faker) {} - /** - * Generates a new placeholder image url. - * - * @param width The width of the image (in pixels). Defaults to `640`. - * @param height The height of the image (in pixels). Defaults to `width`. - * @param text The text of the image. - */ - image(width?: number, height?: number, text?: string): string { - return this.imageUrl(width, height, text); - } - /** * Generates a new placeholder image url. * @@ -60,4 +49,41 @@ export class Placeholder { return url; } + + /** + * Generate a new placeholder image with random colors and text. + * + * @param width The width of the image (in pixels). Defaults to `640`. + * @param height The height of the image (in pixels). Defaults to `width` + * @param format The file format of the image. Supports `png` `jpeg` `png` `gif` `webp`. + */ + randomUrl( + width?: number, + height?: number, + format?: 'png' | 'jpeg' | 'jpg' | 'gif' | 'webp' + ): string { + width = width || 640; + height = height || width; + + let url = 'https://via.placeholder.com'; + url += `/${width}/${height}`; + + const backgroundColor = this.faker.color.rgb({ + casing: 'upper', + prefix: '', + }); + const textColor = this.faker.color.rgb({ casing: 'upper', prefix: '' }); + + url += `/${backgroundColor}/$${textColor}`; + + if (format != null) { + url += `.${format}`; + } + + const text = this.faker.lorem.text(); + const urlParam = new URLSearchParams({ text }); + url += `?${urlParam.toString()}`; + + return url; + } } From cae24bb387c32ac4e81f6049cfa8d20a42da1cd3 Mon Sep 17 00:00:00 2001 From: Adegoke Temitope Date: Wed, 27 Jul 2022 16:36:25 +0100 Subject: [PATCH 06/17] remove test for image method on placeholder image module --- test/image.spec.ts | 9 --------- 1 file changed, 9 deletions(-) diff --git a/test/image.spec.ts b/test/image.spec.ts index e99de3ad266..d7dc93a11bb 100644 --- a/test/image.spec.ts +++ b/test/image.spec.ts @@ -320,15 +320,6 @@ describe('image', () => { ); }); }); - - describe('image()', () => { - it('should return a searching image url with text', () => { - const imageUrl = faker.image.placeholder.image(100, 200, 'I love food'); - expect(imageUrl).toBe( - 'https://via.placeholder.com/100/200?text=I+love+food' - ); - }); - }); }); describe('dataUri', () => { From 28769a28e460ab49a7d70ca1ec9661c182c443ca Mon Sep 17 00:00:00 2001 From: Adegoke Temitope Date: Wed, 27 Jul 2022 16:36:44 +0100 Subject: [PATCH 07/17] refactor randomUrl method --- src/modules/image/providers/placeholder.ts | 34 +++++++--------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index f8f13aa1fe6..18ff3fd2267 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -62,28 +62,16 @@ export class Placeholder { height?: number, format?: 'png' | 'jpeg' | 'jpg' | 'gif' | 'webp' ): string { - width = width || 640; - height = height || width; - - let url = 'https://via.placeholder.com'; - url += `/${width}/${height}`; - - const backgroundColor = this.faker.color.rgb({ - casing: 'upper', - prefix: '', - }); - const textColor = this.faker.color.rgb({ casing: 'upper', prefix: '' }); - - url += `/${backgroundColor}/$${textColor}`; - - if (format != null) { - url += `.${format}`; - } - - const text = this.faker.lorem.text(); - const urlParam = new URLSearchParams({ text }); - url += `?${urlParam.toString()}`; - - return url; + return this.imageUrl( + width, + height, + this.faker.lorem.text(), + format, + this.faker.color.rgb({ + casing: 'upper', + prefix: '', + }), + this.faker.color.rgb({ casing: 'upper', prefix: '' }) + ); } } From c2352b978caed5238219935a4c07a386c1c16c79 Mon Sep 17 00:00:00 2001 From: Adegoke Temitope <49331266+Devdre1909@users.noreply.github.com> Date: Wed, 27 Jul 2022 18:13:39 +0100 Subject: [PATCH 08/17] Update src/modules/image/providers/placeholder.ts Co-authored-by: ST-DDT --- src/modules/image/providers/placeholder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index 18ff3fd2267..ad7109986d4 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -28,7 +28,7 @@ export class Placeholder { height = height || width; let url = 'https://via.placeholder.com'; - url += `/${width}/${height}`; + url += `/${width}x${height}`; if (backgroundColor != null) { url += `/${backgroundColor.replace('#', '').toUpperCase()}`; From bbcfb39e754d467ccbf2d13178ebdd4e1e524951 Mon Sep 17 00:00:00 2001 From: Adegoke Temitope <49331266+Devdre1909@users.noreply.github.com> Date: Wed, 27 Jul 2022 18:13:53 +0100 Subject: [PATCH 09/17] Update src/modules/image/providers/placeholder.ts Co-authored-by: ST-DDT --- src/modules/image/providers/placeholder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index ad7109986d4..0339605d129 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -54,7 +54,7 @@ export class Placeholder { * Generate a new placeholder image with random colors and text. * * @param width The width of the image (in pixels). Defaults to `640`. - * @param height The height of the image (in pixels). Defaults to `width` + * @param height The height of the image (in pixels). Defaults to `width`. * @param format The file format of the image. Supports `png` `jpeg` `png` `gif` `webp`. */ randomUrl( From c5943c943b6b3d5795b29830b7ea6f8d7431cafa Mon Sep 17 00:00:00 2001 From: Adegoke Temitope <49331266+Devdre1909@users.noreply.github.com> Date: Wed, 27 Jul 2022 18:14:05 +0100 Subject: [PATCH 10/17] Update src/modules/image/providers/placeholder.ts Co-authored-by: ST-DDT --- src/modules/image/providers/placeholder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index 0339605d129..164971fc8f8 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -14,7 +14,7 @@ export class Placeholder { * @param text The text of the image. * @param format The file format of the image. Supports `png` `jpeg` `png` `gif` `webp`. * @param backgroundColor The background color of the placeholder. Supports HEX CODE format. - * @param textColor The text color of the placeholder. Requires `backgroundColor` Supports HEX CODE format. + * @param textColor The text color of the placeholder. Requires `backgroundColor`. Supports HEX CODE format. */ imageUrl( width?: number, From dcb509abdc0cc2c6a3610cdf76163bc1557a6586 Mon Sep 17 00:00:00 2001 From: Adegoke Temitope <49331266+Devdre1909@users.noreply.github.com> Date: Wed, 27 Jul 2022 18:14:15 +0100 Subject: [PATCH 11/17] Update src/modules/image/providers/placeholder.ts Co-authored-by: ST-DDT --- src/modules/image/providers/placeholder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index 164971fc8f8..7e8ace55763 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -12,7 +12,7 @@ export class Placeholder { * @param width The width of the image (in pixels). Defaults to `640`. * @param height The height of the image (in pixels). Defaults to `width`. * @param text The text of the image. - * @param format The file format of the image. Supports `png` `jpeg` `png` `gif` `webp`. + * @param format The file format of the image. Supports `png`, `jpeg`, `png`, `gif`, `webp`. * @param backgroundColor The background color of the placeholder. Supports HEX CODE format. * @param textColor The text color of the placeholder. Requires `backgroundColor`. Supports HEX CODE format. */ From 8e2c6872eb0b5e4a4fd3636d95715a01afee812f Mon Sep 17 00:00:00 2001 From: Adegoke Temitope <49331266+Devdre1909@users.noreply.github.com> Date: Wed, 27 Jul 2022 18:14:32 +0100 Subject: [PATCH 12/17] Update src/modules/image/providers/placeholder.ts Co-authored-by: ST-DDT --- src/modules/image/providers/placeholder.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index 7e8ace55763..fcacedccb12 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -32,10 +32,10 @@ export class Placeholder { if (backgroundColor != null) { url += `/${backgroundColor.replace('#', '').toUpperCase()}`; - } - if (textColor != null && backgroundColor !== null) { - url += `/${textColor.replace('#', '').toUpperCase()}`; + if (textColor != null) { + url += `/${textColor.replace('#', '').toUpperCase()}`; + } } if (format != null) { From 4666e598df1c52f44d063abfe8866b760a1c3f01 Mon Sep 17 00:00:00 2001 From: Adegoke Temitope Date: Fri, 29 Jul 2022 14:38:59 +0100 Subject: [PATCH 13/17] test from randomUrl in placeholder image module --- test/image.spec.ts | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/test/image.spec.ts b/test/image.spec.ts index d7dc93a11bb..36503efa3eb 100644 --- a/test/image.spec.ts +++ b/test/image.spec.ts @@ -242,13 +242,13 @@ describe('image', () => { it('should return a random image url from placeholder', () => { const imageUrl = faker.image.placeholder.imageUrl(); - expect(imageUrl).toBe('https://via.placeholder.com/640/640'); + expect(imageUrl).toBe('https://via.placeholder.com/640x640'); }); it('should return a square random image url from placeholder with width and height', () => { const imageUrl = faker.image.placeholder.imageUrl(100); - expect(imageUrl).toBe('https://via.placeholder.com/100/100'); + expect(imageUrl).toBe('https://via.placeholder.com/100x100'); }); it('should return a random image url with a gif format', () => { @@ -259,7 +259,7 @@ describe('image', () => { 'gif' ); - expect(imageUrl).toBe('https://via.placeholder.com/100/100.gif'); + expect(imageUrl).toBe('https://via.placeholder.com/100x100.gif'); }); it('should return a random image url with correct text for a specified format', () => { @@ -271,7 +271,7 @@ describe('image', () => { ); expect(imageUrl).toBe( - 'https://via.placeholder.com/100/100.png?text=I+love+food' + 'https://via.placeholder.com/100x100.png?text=I+love+food' ); }); @@ -286,7 +286,7 @@ describe('image', () => { ); expect(imageUrl).toBe( - 'https://via.placeholder.com/100/100/000000/FFFFFF' + 'https://via.placeholder.com/100x100/000000/FFFFFF' ); }); @@ -301,7 +301,7 @@ describe('image', () => { ); expect(imageUrl).toBe( - 'https://via.placeholder.com/100/100/000000/FFFFFF' + 'https://via.placeholder.com/100x100/000000/FFFFFF' ); }); @@ -316,10 +316,41 @@ describe('image', () => { ); expect(imageUrl).toBe( - 'https://via.placeholder.com/100/200/000000/FFFFFF.jpg?text=I+love+food' + 'https://via.placeholder.com/100x200/000000/FFFFFF.jpg?text=I+love+food' ); }); }); + + describe('randomUrl()', () => { + it('should return a random url with specified width and height', () => { + const imageUrl = faker.image.placeholder.randomUrl(200, 150); + + // https://via.placeholder.com/150/000000/FFFFFF/ + const urlSpilt = imageUrl.split('/'); + + console.log(imageUrl); + + expect(urlSpilt[0]).toBe('https:'); + expect(urlSpilt[2]).toBe('via.placeholder.com'); + expect(urlSpilt[3]).toBe('200x150'); + expect(urlSpilt[4]).toHaveLength(6); + expect(urlSpilt[5].split('?')[0]).toHaveLength(6); + expect(urlSpilt[5].split('?')[1]).toContain('text='); + }); + it('should return a random url with specified width and height and format', () => { + const imageUrl = faker.image.placeholder.randomUrl(200, 150, 'png'); + + const urlSpilt = imageUrl.split('/'); + + expect(urlSpilt[0]).toBe('https:'); + expect(urlSpilt[2]).toBe('via.placeholder.com'); + expect(urlSpilt[3]).toBe('200x150'); + expect(urlSpilt[4]).toHaveLength(6); + expect(urlSpilt[5].split('?')[0]).toHaveLength(10); + expect(urlSpilt[5].split('?')[0]).toContain('.png'); + expect(urlSpilt[5].split('?')[1]).toContain('text='); + }); + }); }); describe('dataUri', () => { From 7c128aea944472b48211085f2e8b0749c41eca6b Mon Sep 17 00:00:00 2001 From: Adegoke Temitope <49331266+Devdre1909@users.noreply.github.com> Date: Fri, 29 Jul 2022 14:39:15 +0100 Subject: [PATCH 14/17] Update src/modules/image/providers/placeholder.ts Co-authored-by: ST-DDT --- src/modules/image/providers/placeholder.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index fcacedccb12..003125f61aa 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -4,7 +4,15 @@ import type { Faker } from '../../..'; * Module to generate links to images on `https://via.placeholder.com/`. */ export class Placeholder { - constructor(private readonly faker: Faker) {} + constructor(private readonly faker: Faker) { + // Bind `this` so namespaced is working correctly + for (const name of Object.getOwnPropertyNames(_Date.prototype)) { + if (name === 'constructor' || typeof this[name] !== 'function') { + continue; + } + this[name] = this[name].bind(this); + } + } /** * Generates a new placeholder image url. From 8ab1e33f1c699ff8607f93bb3cffd3aec1de883f Mon Sep 17 00:00:00 2001 From: ST-DDT Date: Fri, 29 Jul 2022 16:25:04 +0200 Subject: [PATCH 15/17] chore: apply suggestion --- src/modules/image/providers/placeholder.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index 003125f61aa..3edbc4770d2 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -6,7 +6,7 @@ import type { Faker } from '../../..'; export class Placeholder { constructor(private readonly faker: Faker) { // Bind `this` so namespaced is working correctly - for (const name of Object.getOwnPropertyNames(_Date.prototype)) { + for (const name of Object.getOwnPropertyNames(Placeholder.prototype)) { if (name === 'constructor' || typeof this[name] !== 'function') { continue; } From 420349c9bd194c96627e051015918b172890566a Mon Sep 17 00:00:00 2001 From: Adegoke Temitope Date: Fri, 29 Jul 2022 15:34:06 +0100 Subject: [PATCH 16/17] add examples --- src/modules/image/providers/placeholder.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index 3edbc4770d2..6273fc04592 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -23,6 +23,16 @@ export class Placeholder { * @param format The file format of the image. Supports `png`, `jpeg`, `png`, `gif`, `webp`. * @param backgroundColor The background color of the placeholder. Supports HEX CODE format. * @param textColor The text color of the placeholder. Requires `backgroundColor`. Supports HEX CODE format. + * + * @example + * faker.image.placeholder.imageUrl() // https://via.placeholder.com/640x640 + * faker.image.placeholder.imageUrl(200) // https://via.placeholder.com/200x200 + * faker.image.placeholder.imageUrl(200, 100) // https://via.placeholder.com/200x100 + * faker.image.placeholder.imageUrl(200, 100, 'Fish') // https://via.placeholder.com/200x100?text=Fish + * faker.image.placeholder.imageUrl(200, 100, 'Fish', 'webp') // https://via.placeholder.com/200x100.webp?text=Fish + * 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 + * */ imageUrl( width?: number, From 1cf507a60e7c6640f51c026a13c202cc2bda9314 Mon Sep 17 00:00:00 2001 From: Adegoke Temitope Date: Sat, 6 Aug 2022 04:00:44 +0100 Subject: [PATCH 17/17] add examples to randomImage methos --- src/modules/image/providers/placeholder.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/modules/image/providers/placeholder.ts b/src/modules/image/providers/placeholder.ts index 6273fc04592..224dccfe03a 100644 --- a/src/modules/image/providers/placeholder.ts +++ b/src/modules/image/providers/placeholder.ts @@ -74,6 +74,12 @@ export class Placeholder { * @param width The width of the image (in pixels). Defaults to `640`. * @param height The height of the image (in pixels). Defaults to `width`. * @param format The file format of the image. Supports `png` `jpeg` `png` `gif` `webp`. + * + * @example + * faker.image.placeholder.randomUrl() // https://via.placeholder.com/640x640/000000/ffffff?text=lorum + * 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 */ randomUrl( width?: number, @@ -83,7 +89,7 @@ export class Placeholder { return this.imageUrl( width, height, - this.faker.lorem.text(), + this.faker.lorem.word(), format, this.faker.color.rgb({ casing: 'upper',