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
43 changes: 34 additions & 9 deletions src/modules/location/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,17 +338,36 @@ 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 Whether to return an abbreviation. Defaults to `false`.
*
* @example
* faker.location.state() // 'Mississippi'
* fakerEN_CA.location.state() // 'Saskatchewan'
* fakerDE.location.state() // 'Nordrhein-Westfalen'
* faker.location.state(false) // 'Iowa'
* faker.location.state({ abbreviated: true }) // 'LA'
*
* @since 8.0.0
*/
state(): string {
return this.faker.helpers.arrayElement(
this.faker.definitions.location.state
);
state(
options: {
/**
* Whether to return an abbreviation.
*
* @default false
*/
abbreviated?: boolean;
} = {}
): string {
if (typeof options === 'boolean') {
options = { abbreviated: options };
}

const { abbreviated = false } = options;
const stateDataSet = abbreviated
? this.faker.definitions.location.state_abbr
: this.faker.definitions.location.state;

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

/**
Expand All @@ -358,11 +377,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 useAbbr option 1`] = `"NW"`;

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 useAbbr option 1`] = `"SW"`;

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 useAbbr option 1`] = `"NW"`;

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