Skip to content

chore: address zipCodeByState fix types #760

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

Merged
merged 2 commits into from
Apr 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export class Address {
}

/**
* Generates random zipcode from specified format. If format is not specified, the
* locale's zip format is used.
* Generates random zip code from specified format. If format is not specified,
* the locale's zip format is used.
*
* @param format The optional format used to generate the the zip code.
* By default, a random format is used from the locale zip formats.
Expand All @@ -111,7 +111,7 @@ export class Address {
}

/**
* Generates random zipcode from state abbreviation. If state abbreviation is
* Generates random zip code from state abbreviation. If state abbreviation is
* not specified, a random zip code is generated according to the locale's zip format.
* Only works for locales with postcode_by_state definition. If a locale does not
* have a postcode_by_state definition, a random zip code is generated according
Expand All @@ -123,11 +123,10 @@ export class Address {
* fakerUS.address.zipCodeByState("AK") // '99595'
* fakerUS.address.zipCodeByState("??") // '47683-9880'
*/
zipCodeByState(state: string): string | number {
const zipRange = this.faker.definitions.address.postcode_by_state[state];
zipCodeByState(state: string): string {
const zipRange = this.faker.definitions.address.postcode_by_state?.[state];
if (zipRange) {
// TODO ST-DDT 2022-02-10: Fix types
return this.faker.datatype.number(zipRange);
return String(this.faker.datatype.number(zipRange));
}
return this.faker.address.zipCode();
}
Expand Down
5 changes: 1 addition & 4 deletions src/definitions/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ export interface AddressDefinitions {
/**
* Postcodes patterns by state
*/
// TODO ST-DDT 2022-01-31: address.zipCodeByState() expects only { [state: string]: { min: number; max: number } }
postcode_by_state:
| string[]
| { [state: string]: { min: number; max: number } };
postcode_by_state: { [state: string]: { min: number; max: number } };
/**
* Postcodes patterns (Fake-Pattern | Fake-Pattern[]).
*/
Expand Down
2 changes: 0 additions & 2 deletions src/locales/ar/address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import city_name from './city_name';
import country from './country';
import default_country from './default_country';
import postcode from './postcode';
import postcode_by_state from './postcode_by_state';
import secondary_address from './secondary_address';
import state from './state';
import street_address from './street_address';
Expand All @@ -23,7 +22,6 @@ const address = {
country,
default_country,
postcode,
postcode_by_state,
secondary_address,
state,
street_address,
Expand Down
1 change: 0 additions & 1 deletion src/locales/ar/address/postcode_by_state.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/locales/en/address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import default_country from './default_country';
import direction from './direction';
import direction_abbr from './direction_abbr';
import postcode from './postcode';
import postcode_by_state from './postcode_by_state';
import secondary_address from './secondary_address';
import state from './state';
import state_abbr from './state_abbr';
Expand All @@ -39,7 +38,6 @@ const address = {
direction,
direction_abbr,
postcode,
postcode_by_state,
secondary_address,
state,
state_abbr,
Expand Down
1 change: 0 additions & 1 deletion src/locales/en/address/postcode_by_state.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/locales/he/address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import default_country from './default_country';
import direction from './direction';
import direction_abbr from './direction_abbr';
import postcode from './postcode';
import postcode_by_state from './postcode_by_state';
import secondary_address from './secondary_address';
import state from './state';
import state_abbr from './state_abbr';
Expand All @@ -33,7 +32,6 @@ const address = {
direction,
direction_abbr,
postcode,
postcode_by_state,
secondary_address,
state,
state_abbr,
Expand Down
1 change: 0 additions & 1 deletion src/locales/he/address/postcode_by_state.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/locales/ur/address/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import country from './country';
import default_country from './default_country';
import direction from './direction';
import postcode from './postcode';
import postcode_by_state from './postcode_by_state';
import secondary_address from './secondary_address';
import state from './state';
import state_abbr from './state_abbr';
Expand All @@ -30,7 +29,6 @@ const address = {
default_country,
direction,
postcode,
postcode_by_state,
secondary_address,
state,
state_abbr,
Expand Down
1 change: 0 additions & 1 deletion src/locales/ur/address/postcode_by_state.ts

This file was deleted.

6 changes: 3 additions & 3 deletions test/address.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,15 @@ describe('address', () => {
faker.locale = 'en_US';
const states = ['IL', 'GA', 'WA'];

const zipCode1 = faker.address.zipCodeByState(states[0]);
const zipCode1 = +faker.address.zipCodeByState(states[0]);
expect(zipCode1).greaterThanOrEqual(60001);
expect(zipCode1).lessThanOrEqual(62999);

const zipCode2 = faker.address.zipCodeByState(states[1]);
const zipCode2 = +faker.address.zipCodeByState(states[1]);
expect(zipCode2).greaterThanOrEqual(30001);
expect(zipCode2).lessThanOrEqual(31999);

const zipCode3 = faker.address.zipCodeByState(states[2]);
const zipCode3 = +faker.address.zipCodeByState(states[2]);
expect(zipCode3).greaterThanOrEqual(98001);
expect(zipCode3).lessThanOrEqual(99403);
});
Expand Down