Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(image): add image via.placeholder provider #1186

Merged
merged 22 commits into from Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ca5a910
feat(image): add placeholder.com as module
Devdre1909 Jul 24, 2022
53c6774
test: tests for image placeholder.com module
Devdre1909 Jul 24, 2022
f292381
feat(image)
Devdre1909 Jul 24, 2022
e4d5892
update height default value
Devdre1909 Jul 25, 2022
f174782
remove image method, add randomUrl method
Devdre1909 Jul 27, 2022
cae24bb
remove test for image method on placeholder image module
Devdre1909 Jul 27, 2022
28769a2
refactor randomUrl method
Devdre1909 Jul 27, 2022
c2352b9
Update src/modules/image/providers/placeholder.ts
Devdre1909 Jul 27, 2022
bbcfb39
Update src/modules/image/providers/placeholder.ts
Devdre1909 Jul 27, 2022
c5943c9
Update src/modules/image/providers/placeholder.ts
Devdre1909 Jul 27, 2022
dcb509a
Update src/modules/image/providers/placeholder.ts
Devdre1909 Jul 27, 2022
8e2c687
Update src/modules/image/providers/placeholder.ts
Devdre1909 Jul 27, 2022
4666e59
test from randomUrl in placeholder image module
Devdre1909 Jul 29, 2022
7c128ae
Update src/modules/image/providers/placeholder.ts
Devdre1909 Jul 29, 2022
fbcbf29
Merge branch 'feat/image-placeholder' of github.com:Devdre1909/faker …
Devdre1909 Jul 29, 2022
8ab1e33
chore: apply suggestion
ST-DDT Jul 29, 2022
740426a
Merge branch 'main' into feat/image-placeholder
ST-DDT Jul 29, 2022
d3b6f75
Merge branch 'feat/image-placeholder' of github.com:Devdre1909/faker …
Devdre1909 Jul 29, 2022
420349c
add examples
Devdre1909 Jul 29, 2022
1cf507a
add examples to randomImage methos
Devdre1909 Aug 6, 2022
5dbb164
Merge branch 'main' into feat/image-placeholder
Shinigami92 Aug 8, 2022
ec77e3b
Merge branch 'main' into feat/image-placeholder
Shinigami92 Aug 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/modules/image/index.ts
Expand Up @@ -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';

/**
Expand All @@ -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
Expand All @@ -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);
}

/**
Expand Down
77 changes: 77 additions & 0 deletions src/modules/image/providers/placeholder.ts
@@ -0,0 +1,77 @@
import type { Faker } from '../../..';

/**
* Module to generate links to images on `https://via.placeholder.com/`.
*/
export class Placeholder {
constructor(private readonly faker: Faker) {}
Devdre1909 marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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.
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
* @param format The file format of the image. Supports `png` `jpeg` `png` `gif` `webp`.
Devdre1909 marked this conversation as resolved.
Show resolved Hide resolved
* @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.
Devdre1909 marked this conversation as resolved.
Show resolved Hide resolved
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*/
imageUrl(
width?: number,
height?: number,
text?: string,
format?: 'png' | 'jpeg' | 'jpg' | 'gif' | 'webp',
backgroundColor?: string,
textColor?: string
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
): string {
width = width || 640;
height = height || width;

let url = 'https://via.placeholder.com';
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
url += `/${width}/${height}`;
Devdre1909 marked this conversation as resolved.
Show resolved Hide resolved

if (backgroundColor != null) {
url += `/${backgroundColor.replace('#', '').toUpperCase()}`;
}

if (textColor != null && backgroundColor !== null) {
url += `/${textColor.replace('#', '').toUpperCase()}`;
}
Devdre1909 marked this conversation as resolved.
Show resolved Hide resolved

if (format != null) {
url += `.${format}`;
}

if (text != null) {
const urlParam = new URLSearchParams({ text });
url += `?${urlParam.toString()}`;
}

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`
Devdre1909 marked this conversation as resolved.
Show resolved Hide resolved
* @param format The file format of the image. Supports `png` `jpeg` `png` `gif` `webp`.
*/
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
randomUrl(
width?: number,
height?: number,
format?: 'png' | 'jpeg' | 'jpg' | 'gif' | 'webp'
): string {
return this.imageUrl(
width,
height,
this.faker.lorem.text(),
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
format,
this.faker.color.rgb({
casing: 'upper',
prefix: '',
}),
this.faker.color.rgb({ casing: 'upper', prefix: '' })
);
}
}
85 changes: 85 additions & 0 deletions test/image.spec.ts
Expand Up @@ -237,6 +237,91 @@ 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('dataUri', () => {
it('should return a blank data', () => {
const dataUri = faker.image.dataUri(200, 300);
Expand Down