Skip to content

Commit

Permalink
feat(location): add continent method (#3162)
Browse files Browse the repository at this point in the history
  • Loading branch information
joscha authored Oct 12, 2024
1 parent 033c23b commit 4056ab0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/definitions/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ export type LocationDefinition = LocaleEntry<{
*/
city_suffix: string[];

/**
* The names of all continents.
*/
continent: string[];

/**
* The names of all countries.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/locales/en/location/continent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default [
'Africa',
'Antarctica',
'Asia',
'Australia',
'Europe',
'North America',
'South America',
];
2 changes: 2 additions & 0 deletions src/locales/en/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import city_name from './city_name';
import city_pattern from './city_pattern';
import city_prefix from './city_prefix';
import city_suffix from './city_suffix';
import continent from './continent';
import country from './country';
import county from './county';
import direction from './direction';
Expand All @@ -26,6 +27,7 @@ const location: LocationDefinition = {
city_pattern,
city_prefix,
city_suffix,
continent,
country,
county,
direction,
Expand Down
14 changes: 14 additions & 0 deletions src/modules/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ export class LocationModule extends ModuleBase {
);
}

/**
* Returns a random continent name.
*
* @example
* faker.location.continent() // 'Asia'
*
* @since 9.1.0
*/
continent(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.location.continent
);
}

/**
* Returns a random [ISO_3166-1](https://en.wikipedia.org/wiki/ISO_3166-1) country code.
*
Expand Down
6 changes: 6 additions & 0 deletions test/modules/__snapshots__/location.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ exports[`location > 42 > cardinalDirection > with abbreviated option 1`] = `"E"`

exports[`location > 42 > city 1`] = `"Fort Moses"`;

exports[`location > 42 > continent 1`] = `"Asia"`;

exports[`location > 42 > country 1`] = `"Guinea"`;

exports[`location > 42 > countryCode > noArgs 1`] = `"GY"`;
Expand Down Expand Up @@ -144,6 +146,8 @@ exports[`location > 1211 > cardinalDirection > with abbreviated option 1`] = `"W

exports[`location > 1211 > city 1`] = `"The Villages"`;

exports[`location > 1211 > continent 1`] = `"South America"`;

exports[`location > 1211 > country 1`] = `"Uganda"`;

exports[`location > 1211 > countryCode > noArgs 1`] = `"UM"`;
Expand Down Expand Up @@ -280,6 +284,8 @@ exports[`location > 1337 > cardinalDirection > with abbreviated option 1`] = `"E

exports[`location > 1337 > city 1`] = `"East Duane"`;

exports[`location > 1337 > continent 1`] = `"Antarctica"`;

exports[`location > 1337 > country 1`] = `"Egypt"`;

exports[`location > 1337 > countryCode > noArgs 1`] = `"EH"`;
Expand Down
12 changes: 12 additions & 0 deletions test/modules/location.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ describe('location', () => {

t.it('country');

t.it('continent');

t.describe('countryCode', (t) => {
t.it('noArgs')
.it('with string alpha-2', 'alpha-2')
Expand Down Expand Up @@ -145,6 +147,16 @@ describe('location', () => {
describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
'random seeded tests for seed %i',
() => {
describe('continent()', () => {
it('returns random continent', () => {
const actual = faker.location.continent();

expect(actual).toBeTruthy();
expect(actual).toBeTypeOf('string');
expect(faker.definitions.location.continent).toContain(actual);
});
});

describe('countryCode()', () => {
it('returns random alpha-2 countryCode', () => {
const countryCode = faker.location.countryCode('alpha-2');
Expand Down

0 comments on commit 4056ab0

Please sign in to comment.