Skip to content

Commit

Permalink
feat: add internet.emoji
Browse files Browse the repository at this point in the history
  • Loading branch information
hugoattal committed Feb 17, 2022
1 parent b192d25 commit 7acdb75
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ Faker provides many useful utility functions.
| color | Generates a random hexadecimal color |
| mac | Generates a random mac address |
| password | Generates a random password |
| emoji | Generates a random emoji |

- faker.lorem

Expand Down
19 changes: 18 additions & 1 deletion src/definitions/internet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ export interface InternetDefinitions {
* Some free-mail domains used in that country (e.g. `gmail.de`).
*/
free_email: string[];
/**
* List of all fully-qualified emoji ordered by groups.
*/
emoji: {
[key in
| 'smileys'
| 'body'
| 'person'
| 'nature'
| 'food'
| 'travel'
| 'activities'
| 'objects'
| 'symbols'
| 'flags']: string[];
};
}

/**
Expand All @@ -24,5 +40,6 @@ export interface InternetDefinitions {
export const INTERNET = allOf<keyof InternetDefinitions>()(
'domain_suffix',
'example_email',
'free_email'
'free_email',
'emoji'
);
23 changes: 22 additions & 1 deletion src/internet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Faker } from '.';
import type { Faker, InternetDefinitions } from '.';

import * as random_ua from './vendor/user-agent';

Expand Down Expand Up @@ -405,4 +405,25 @@ export class Internet {
};
return _password(len, memorable, pattern, prefix);
}

/**
* Generates a random emoji.
*
* @param filters A list of the emoji groups that should be used.
*
* @example
* faker.internet.emoji() // '🥰'
* faker.internet.emoji(['food', 'nature']) // '🥐'
*/
emoji(filters?: Array<keyof InternetDefinitions['emoji']>): string {
filters =
filters ||
(Object.keys(this.faker.definitions.internet.emoji) as Array<
keyof InternetDefinitions['emoji']
>);
const group = this.faker.random.arrayElement(filters);
return this.faker.random.arrayElement(
this.faker.definitions.internet.emoji[group]
);
}
}
121 changes: 121 additions & 0 deletions src/locales/en/internet/emoji.ts

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/locales/en/internet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
import type { InternetDefinitions } from '../../..';
import avatar_uri from './avatar_uri';
import domain_suffix from './domain_suffix';
import emoji from './emoji';
import example_email from './example_email';
import free_email from './free_email';

const internet = {
avatar_uri,
domain_suffix,
emoji,
example_email,
free_email,
} as InternetDefinitions;
Expand Down
2 changes: 1 addition & 1 deletion src/locales/fa/internet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import domain_suffix from './domain_suffix';
import example_email from './example_email';
import free_email from './free_email';

const internet: InternetDefinitions = {
const internet: Partial<InternetDefinitions> = {
domain_suffix,
example_email,
free_email,
Expand Down
14 changes: 14 additions & 0 deletions test/internet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const seededRuns = [
color: '#30667a',
mac: '5c:f2:bc:99:27:21',
password: 'Dl2fkYYKLsZdepz',
emoji: '🦟',
},
},
{
Expand All @@ -50,6 +51,7 @@ const seededRuns = [
color: '#214814',
mac: '48:23:48:70:53:89',
password: '9V05TL7RY9fmECg',
emoji: '💇‍♂️',
},
},
{
Expand All @@ -74,6 +76,7 @@ const seededRuns = [
color: '#773a72',
mac: 'e7:ec:32:f0:a2:a3',
password: 'yLuj60b5iHB0bhn',
emoji: '🇮🇸',
},
},
];
Expand All @@ -98,6 +101,7 @@ const functionNames = [
'color',
'mac',
'password',
'emoji',
];

describe('internet', () => {
Expand Down Expand Up @@ -546,6 +550,16 @@ describe('internet', () => {
expect(password).satisfy(validator.isStrongPassword);
});
});

describe('emoji', () => {
it('should return a random emoji', () => {
const emoji = faker.internet.emoji();

expect(emoji).toBeTruthy();
expect(typeof emoji).toBe('string');
expect(emoji.length).toBeGreaterThanOrEqual(1);
});
});
}
});
});

0 comments on commit 7acdb75

Please sign in to comment.