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): replace zipCode format parameter with style #3223

Draft
wants to merge 10 commits into
base: next
Choose a base branch
from
121 changes: 115 additions & 6 deletions src/modules/location/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FakerError } from '../../errors/faker-error';
import { deprecated } from '../../internal/deprecated';
import { ModuleBase } from '../../internal/module-base';

/**
Expand All @@ -13,6 +14,60 @@
* For a random country, you can use [`country()`](https://fakerjs.dev/api/location.html#country) or [`countryCode()`](https://fakerjs.dev/api/location.html#countrycode).
*/
export class LocationModule extends ModuleBase {
/**
* Generates random zip code for the entire locale or the given state.
*
* @param options The optional options object.
* @param options.state The state to generate the zip code for.
* If the current locale does not have a corresponding `postcode_by_state` definition, an error is thrown.
*
* @example
* faker.location.zipCode() // '17839'
* fakerEN_US.location.zipCode({ state: 'CA' }) // '90210'
*
* @since 8.0.0
*/
zipCode(options?: {
/**
* The state to generate the zip code for.
*
* If the current locale does not have a corresponding `postcode_by_state` definition, an error is thrown.
*/
state?: string;
}): string;
/**
* Generates random zip code from specified format. If format is not specified,
* the locale's zip format is used.
*
* @param options The format used to generate the zip code or an options object.
* @param options.format The optional format used to generate the zip code.
* By default, a random format is used from the locale zip formats.
* This won't be used if the state option is specified.
*
* @see faker.helpers.replaceSymbols(): For more information about how the pattern is used.
*
* @example
* faker.location.zipCode() // '17839'
* faker.location.zipCode('####') // '6925'
*
* @since 8.0.0
*
* @deprecated Use `faker.location.zipCode()` or `faker.location.zipCode({ state })` or `faker.helpers.replaceSymbols(format)` instead.
*/
zipCode(
options?:
| string
| {
/**
* The optional format used to generate the zip code.
*
* This won't be used if the state option is specified.
*
* @default faker.definitions.location.postcode
*/
format?: string;
}
): string;
/**
* Generates random zip code from specified format. If format is not specified,
* the locale's zip format is used.
Expand All @@ -28,7 +83,46 @@
*
* @example
* faker.location.zipCode() // '17839'
* faker.location.zipCode('####') // '6925'
* fakerEN_US.location.zipCode({ state: 'CA' }) // '90210'
*
* @since 8.0.0
*/
zipCode(
options?:
| string
| {
/**
* The state to generate the zip code for.
*
* If the current locale does not have a corresponding `postcode_by_state` definition, an error is thrown.
*/
state?: string;
/**
* The optional format used to generate the zip code.
*
* This won't be used if the state option is specified.
*
* @default faker.definitions.location.postcode
*/
format?: string;
}
): string;
/**
* Generates random zip code from specified format. If format is not specified,
* the locale's zip format is used.
*
* @param options The format used to generate the zip code or an options object.
* @param options.state The state to generate the zip code for.
* If the current locale does not have a corresponding `postcode_by_state` definition, an error is thrown.
* @param options.format The optional format used to generate the zip code.
* By default, a random format is used from the locale zip formats.
* This won't be used if the state option is specified.
*
* @see faker.helpers.replaceSymbols(): For more information about how the pattern is used.
*
* @example
* faker.location.zipCode() // '17839'
* fakerEN_US.location.zipCode({ state: 'CA' }) // '90210'
*
* @since 8.0.0
*/
Expand Down Expand Up @@ -71,14 +165,29 @@
return this.faker.helpers.fake(zipPattern);
}

let { format = this.faker.definitions.location.postcode } = options;
if (typeof format === 'string') {
format = [format];
const { format } = options;

if (format != null) {
deprecated({
deprecated:
'faker.location.zipCode(format) and faker.location.zipCode({ format })',
proposed:
'faker.location.zipCode(), faker.location.zipCode({ state }), or faker.helpers.replaceSymbols(format)',
since: '9.1.0',
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
until: '10.0.0',
});
return this.faker.helpers.replaceSymbols(format);
}

let zipPattern = this.faker.definitions.location.postcode;

if (typeof zipPattern === 'string') {
zipPattern = [zipPattern];

Check warning on line 185 in src/modules/location/index.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/location/index.ts#L185

Added line #L185 was not covered by tests
}

format = this.faker.helpers.arrayElement(format);
zipPattern = this.faker.helpers.arrayElement(zipPattern);

return this.faker.helpers.replaceSymbols(format);
return this.faker.helpers.replaceSymbols(zipPattern);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions test/modules/location.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,13 @@ describe('location', () => {

describe('zipCode()', () => {
it('returns random zipCode - user specified format', () => {
// eslint-disable-next-line @typescript-eslint/no-deprecated
let zipCode = faker.location.zipCode({ format: '?#? #?#' });

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

// try another format
// eslint-disable-next-line @typescript-eslint/no-deprecated
zipCode = faker.location.zipCode({ format: '###-###' });

expect(zipCode).toMatch(/^\d{3}-\d{3}$/);
Expand Down