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

refactor(location): merge state and stateAbbr #2060

Merged
merged 9 commits into from
Apr 17, 2023
38 changes: 31 additions & 7 deletions src/modules/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,17 +338,35 @@ export class LocationModule {
/**
* Returns a random localized state, or other equivalent first-level administrative entity for the locale's country such as a province or region.
*
* @param options An options object. Defaults to `{}`.
* @param options.abbreviated If true this will return abbreviated first-level administrative entity names.
* Otherwise this will return the long name. Defaults to `false`.
*
* @example
* faker.location.state() // 'Mississippi'
* fakerEN_CA.location.state() // 'Saskatchewan'
* fakerDE.location.state() // 'Nordrhein-Westfalen'
* faker.location.state({ abbreviated: true }) // 'LA'
*
* @since 8.0.0
*/
state(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.location.state
);
state(
options: {
/**
* If true this will return abbreviated first-level administrative entity names.
* Otherwise this will return the long name.
*
* @default false
*/
abbreviated?: boolean;
} = {}
): string {
const { abbreviated = false } = options;
const stateDataSet = abbreviated
? this.faker.definitions.location.state_abbr
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
: this.faker.definitions.location.state;

return this.faker.helpers.arrayElement(stateDataSet);
}

/**
Expand All @@ -358,11 +376,17 @@ export class LocationModule {
* faker.location.stateAbbr() // 'ND'
*
* @since 8.0.0
*
* @deprecated Use `faker.location.state({ abbreviated: true })` instead.
*/
stateAbbr(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.location.state_abbr
);
deprecated({
deprecated: 'faker.location.stateAbbr()',
proposed: 'faker.location.state({ abbreviated: true })',
since: '8.0',
until: '9.0',
});
return this.state({ abbreviated: true });
}

/**
Expand Down
12 changes: 9 additions & 3 deletions test/__snapshots__/location.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ exports[`location > 42 > ordinalDirection > with boolean 1`] = `"Northwest"`;

exports[`location > 42 > secondaryAddress 1`] = `"Apt. 791"`;

exports[`location > 42 > state 1`] = `"Maine"`;
exports[`location > 42 > state > noArgs 1`] = `"Maine"`;

exports[`location > 42 > state > with options 1`] = `"ME"`;

exports[`location > 42 > stateAbbr 1`] = `"ME"`;

Expand Down Expand Up @@ -280,7 +282,9 @@ exports[`location > 1211 > ordinalDirection > with boolean 1`] = `"Southwest"`;

exports[`location > 1211 > secondaryAddress 1`] = `"Suite 487"`;

exports[`location > 1211 > state 1`] = `"Washington"`;
exports[`location > 1211 > state > noArgs 1`] = `"Washington"`;

exports[`location > 1211 > state > with options 1`] = `"WA"`;

exports[`location > 1211 > stateAbbr 1`] = `"WA"`;

Expand Down Expand Up @@ -432,7 +436,9 @@ exports[`location > 1337 > ordinalDirection > with boolean 1`] = `"Northwest"`;

exports[`location > 1337 > secondaryAddress 1`] = `"Apt. 512"`;

exports[`location > 1337 > state 1`] = `"Indiana"`;
exports[`location > 1337 > state > noArgs 1`] = `"Indiana"`;

exports[`location > 1337 > state > with options 1`] = `"IN"`;

exports[`location > 1337 > stateAbbr 1`] = `"IN"`;

Expand Down
7 changes: 6 additions & 1 deletion test/location.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ describe('location', () => {
.it('only radius', { radius: 12 })
.it('only isMetric', { isMetric: true });
});
t.it('state').it('stateAbbr');

t.describe('state', (t) => {
t.it('noArgs').it('with options', { abbreviated: true });
});

t.it('stateAbbr');

t.it('timeZone');

Expand Down