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)!: zip code state #1874

Merged
merged 22 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
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
46 changes: 38 additions & 8 deletions src/modules/location/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../..';
import { FakerError } from '../..';
import { deprecated } from '../../internal/deprecated';

/**
Expand All @@ -23,8 +24,12 @@ export class LocationModule {
* the locale's zip format is used.
*
* @param options The format used to generate the the zip code or an options object. Defaults to `{}`.
* @param options.state The state to generate the zip code for.
* Only works for locales with postcode_by_state definition. If a locale does not
* have a postcode_by_state definition an error is thrown.
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
* @param options.format The optional format used to generate the the zip code.
* By default, a random format is used from the locale zip formats.
* This wont be used if the state option is specified.
*
* @see faker.helpers.replaceSymbols()
*
Expand All @@ -38,9 +43,18 @@ export class LocationModule {
options:
| string
| {
/**
* The state to generate the zip code for.
*
* Only works for locales with postcode_by_state definition. If a locale does not
* have a postcode_by_state definition an error is thrown.
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
*/
state?: string;
/**
* The optional format used to generate the the zip code.
*
* This wont be used if the state option is specified.
*
* @default faker.definitions.location.postcode
*/
format?: string;
Expand All @@ -50,6 +64,19 @@ export class LocationModule {
options = { format: options };
}

const { state } = options;

if (state) {
const zipRange =
this.faker.definitions.location.postcode_by_state?.[state];

if (zipRange) {
return String(this.faker.number.int(zipRange));
}
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved

throw new FakerError(`No zip code range found for state "${state}"`);
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
}

let { format = this.faker.definitions.location.postcode } = options;
if (typeof format === 'string') {
format = [format];
Expand All @@ -64,8 +91,7 @@ export class LocationModule {
* Generates random zip code from state abbreviation.
*
* Only works for locales with postcode_by_state definition. If a locale does not
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
* have a postcode_by_state definition, a random zip code is generated according
* to the locale's zip format.
* have a postcode_by_state definition an error is thrown.
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
*
* @param options A state abbreviation or an options object. Defaults to `{}`.
* @param options.state The abbreviation of the state to generate the zip code for.
Expand All @@ -76,6 +102,8 @@ export class LocationModule {
* fakerEN_US.location.zipCodeByState("??") // '47683-9880'
*
* @since 8.0.0
*
* @deprecated Use `faker.location.zipCode({ state })` instead.
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
*/
zipCodeByState(
options:
Expand All @@ -88,18 +116,20 @@ export class LocationModule {
state?: string;
} = {}
): string {
deprecated({
deprecated: 'faker.location.zipCodeByState',
proposed: 'faker.location.zipCode({ state })',
since: '8.0',
until: '9.0',
});

if (typeof options === 'string') {
options = { state: options };
}

const { state } = options;

const zipRange = this.faker.definitions.location.postcode_by_state?.[state];
if (zipRange) {
return String(this.faker.number.int(zipRange));
}

return this.zipCode();
return this.zipCode({ state });
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
18 changes: 0 additions & 18 deletions test/__snapshots__/location.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,6 @@ exports[`location > 42 > zipCode > with string 1`] = `"379"`;

exports[`location > 42 > zipCodeByState > noArgs 1`] = `"79177"`;

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

exports[`location > 42 > zipCodeByState > with string 1 1`] = `"79177"`;

exports[`location > 42 > zipCodeByState > with string 2 1`] = `"79177"`;

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

exports[`location > 1211 > cardinalDirection > noArgs 1`] = `"West"`;
Expand Down Expand Up @@ -310,12 +304,6 @@ exports[`location > 1211 > zipCode > with string 1`] = `"948"`;

exports[`location > 1211 > zipCodeByState > noArgs 1`] = `"48721-9061"`;

exports[`location > 1211 > zipCodeByState > with state options 1`] = `"48721-9061"`;

exports[`location > 1211 > zipCodeByState > with string 1 1`] = `"48721-9061"`;

exports[`location > 1211 > zipCodeByState > with string 2 1`] = `"48721-9061"`;

exports[`location > 1337 > buildingNumber 1`] = `"51225"`;

exports[`location > 1337 > cardinalDirection > noArgs 1`] = `"East"`;
Expand Down Expand Up @@ -467,9 +455,3 @@ exports[`location > 1337 > zipCode > with format option 1`] = `"251-225"`;
exports[`location > 1337 > zipCode > with string 1`] = `"251"`;

exports[`location > 1337 > zipCodeByState > noArgs 1`] = `"51225"`;

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

exports[`location > 1337 > zipCodeByState > with string 1 1`] = `"51225"`;

exports[`location > 1337 > zipCodeByState > with string 2 1`] = `"51225"`;
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 20 additions & 2 deletions test/location.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { faker, fakerEN_CA, fakerEN_US } from '../src';
import { faker, fakerEN_CA, fakerEN_US, FakerError } from '../src';
import { seededTests } from './support/seededRuns';
import { times } from './support/times';

Expand Down Expand Up @@ -113,7 +113,9 @@ describe('location', () => {
t.describe('zipCode', (t) => {
t.it('noArgs')
.it('with string', '###')
.it('with format option', { format: '###-###' });
.it('with format option', { format: '###-###' })
.it('with state option', { state: 'CA' })
.it('with options', { state: 'CA', format: '###-###' });
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
});

t.describe('zipCodeByState', (t) => {
Expand Down Expand Up @@ -156,6 +158,22 @@ describe('location', () => {

expect(zipCode).toMatch(/^[A-Za-z]\d[A-Za-z]\s?\d[A-Za-z]\d$/);
});

it.each([
['IL', 60001, 62999],
['GA', 30001, 31999],
['WA', 98001, 99403],
])('returns zipCode valid for state %s', (state, lower, upper) => {
const zipCode1 = +fakerEN_US.location.zipCode({ state });
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
expect(zipCode1).toBeGreaterThanOrEqual(lower);
expect(zipCode1).toBeLessThanOrEqual(upper);
});

it('should throw when definitions.location.postcode_by_state not set', () => {
expect(() => faker.location.zipCode({ state: 'XX' })).toThrow(
new FakerError('No zip code range found for state "XX"')
);
});
});

describe('zipCodeByState()', () => {
Expand Down