Skip to content

Commit 054c284

Browse files
authored
Merge branch 'next' into chore/chore/remove-company-en-dupes
2 parents 3abaee5 + 8574125 commit 054c284

File tree

5 files changed

+70
-34
lines changed

5 files changed

+70
-34
lines changed

.github/ISSUE_TEMPLATE/feature_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: "\U0001F680 New feature proposal"
22
description: Propose a new feature
3-
labels: ['s: pending triage'] # This will automatically assign the 's: pending triage' label
3+
labels: ['s: pending triage', 'c: feature', 's: waiting for user interest']
44
body:
55
- type: markdown
66
attributes:

docs/guide/upgrading.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ For more information refer to our [Localization Guide](localization).
101101

102102
`faker.mersenne` and `faker.helpers.repeatString` were only ever intended for internal use, and are no longer available.
103103

104+
### `faker.location.zipCodeByState`
105+
106+
The `faker.location.zipCodeByState` method has been deprecated, but will also now throw an error if the current locale does not have a `postcode_by_state` definition.
107+
104108
### Other deprecated methods removed/replaced
105109

106110
| Old method | New method |

src/modules/location/index.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Faker } from '../..';
2+
import { FakerError } from '../../errors/faker-error';
23
import { deprecated } from '../../internal/deprecated';
34

45
/**
@@ -31,8 +32,11 @@ export class LocationModule {
3132
* the locale's zip format is used.
3233
*
3334
* @param options The format used to generate the the zip code or an options object. Defaults to `{}`.
35+
* @param options.state The state to generate the zip code for.
36+
* If the current locale does not have a corresponding `postcode_by_state` definition, an error is thrown.
3437
* @param options.format The optional format used to generate the the zip code.
3538
* By default, a random format is used from the locale zip formats.
39+
* This wont be used if the state option is specified.
3640
*
3741
* @see faker.helpers.replaceSymbols()
3842
*
@@ -46,9 +50,17 @@ export class LocationModule {
4650
options:
4751
| string
4852
| {
53+
/**
54+
* The state to generate the zip code for.
55+
*
56+
* If the currrent locale does not have a corresponding `postcode_by_state` definition, an error is thrown.
57+
*/
58+
state?: string;
4959
/**
5060
* The optional format used to generate the the zip code.
5161
*
62+
* This wont be used if the state option is specified.
63+
*
5264
* @default faker.definitions.location.postcode
5365
*/
5466
format?: string;
@@ -58,6 +70,19 @@ export class LocationModule {
5870
options = { format: options };
5971
}
6072

73+
const { state } = options;
74+
75+
if (state) {
76+
const zipRange =
77+
this.faker.definitions.location.postcode_by_state?.[state];
78+
79+
if (zipRange) {
80+
return String(this.faker.number.int(zipRange));
81+
}
82+
83+
throw new FakerError(`No zip code definition found for state "${state}"`);
84+
}
85+
6186
let { format = this.faker.definitions.location.postcode } = options;
6287
if (typeof format === 'string') {
6388
format = [format];
@@ -71,19 +96,22 @@ export class LocationModule {
7196
/**
7297
* Generates random zip code from state abbreviation.
7398
*
74-
* Only works for locales with postcode_by_state definition. If a locale does not
75-
* have a postcode_by_state definition, a random zip code is generated according
76-
* to the locale's zip format.
99+
* If the current locale does not have a corresponding `postcode_by_state` definition, an error is thrown.
77100
*
78101
* @param options A state abbreviation or an options object. Defaults to `{}`.
79102
* @param options.state The abbreviation of the state to generate the zip code for.
80103
* If not specified, a random zip code is generated according to the locale's zip format.
81104
*
105+
* @see faker.location.zipCode()
106+
*
82107
* @example
83108
* fakerEN_US.location.zipCodeByState("AK") // '99595'
84-
* fakerEN_US.location.zipCodeByState("??") // '47683-9880'
109+
* fakerEN_US.location.zipCodeByState() // '47683-9880'
110+
* fakerEN_US.location.zipCodeByState({ state: "AK" }) // '99595'
85111
*
86112
* @since 8.0.0
113+
*
114+
* @deprecated Use `faker.location.zipCode({ state })` instead.
87115
*/
88116
zipCodeByState(
89117
options:
@@ -96,18 +124,20 @@ export class LocationModule {
96124
state?: string;
97125
} = {}
98126
): string {
127+
deprecated({
128+
deprecated: 'faker.location.zipCodeByState',
129+
proposed: 'faker.location.zipCode({ state })',
130+
since: '8.0',
131+
until: '9.0',
132+
});
133+
99134
if (typeof options === 'string') {
100135
options = { state: options };
101136
}
102137

103138
const { state } = options;
104139

105-
const zipRange = this.faker.definitions.location.postcode_by_state?.[state];
106-
if (zipRange) {
107-
return String(this.faker.number.int(zipRange));
108-
}
109-
110-
return this.zipCode();
140+
return this.zipCode({ state });
111141
}
112142

113143
/**

test/__snapshots__/location.spec.ts.snap

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,6 @@ exports[`location > 42 > zipCode > with string 1`] = `"379"`;
152152

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

155-
exports[`location > 42 > zipCodeByState > with state options 1`] = `"79177"`;
156-
157-
exports[`location > 42 > zipCodeByState > with string 1 1`] = `"79177"`;
158-
159-
exports[`location > 42 > zipCodeByState > with string 2 1`] = `"79177"`;
160-
161155
exports[`location > 1211 > buildingNumber 1`] = `"487"`;
162156

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

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

313-
exports[`location > 1211 > zipCodeByState > with state options 1`] = `"48721-9061"`;
314-
315-
exports[`location > 1211 > zipCodeByState > with string 1 1`] = `"48721-9061"`;
316-
317-
exports[`location > 1211 > zipCodeByState > with string 2 1`] = `"48721-9061"`;
318-
319307
exports[`location > 1337 > buildingNumber 1`] = `"51225"`;
320308

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

469457
exports[`location > 1337 > zipCodeByState > noArgs 1`] = `"51225"`;
470-
471-
exports[`location > 1337 > zipCodeByState > with state options 1`] = `"51225"`;
472-
473-
exports[`location > 1337 > zipCodeByState > with string 1 1`] = `"51225"`;
474-
475-
exports[`location > 1337 > zipCodeByState > with string 2 1`] = `"51225"`;

test/location.spec.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest';
2-
import { faker, fakerEN_CA, fakerEN_US } from '../src';
2+
import { faker, fakerEN_CA, fakerEN_US, FakerError } from '../src';
33
import { seededTests } from './support/seededRuns';
44
import { times } from './support/times';
55

@@ -114,13 +114,17 @@ describe('location', () => {
114114
t.it('noArgs')
115115
.it('with string', '###')
116116
.it('with format option', { format: '###-###' });
117+
// These are currently commented out because non-default locales are currently not supported
118+
// .it('with state option', { state: 'CA' })
119+
// .it('with options', { state: 'CA', format: '###-###' });
117120
});
118121

119122
t.describe('zipCodeByState', (t) => {
120-
t.it('noArgs')
121-
.it('with string 1', 'CA')
122-
.it('with string 2', 'WA')
123-
.it('with state options', { state: 'WA' });
123+
t.it('noArgs');
124+
// These are currently commented out because non-default locales are currently not supported
125+
// .it('with string 1', 'CA')
126+
// .it('with string 2', 'WA')
127+
// .it('with state options', { state: 'WA' });
124128
});
125129
});
126130

@@ -156,6 +160,22 @@ describe('location', () => {
156160

157161
expect(zipCode).toMatch(/^[A-Za-z]\d[A-Za-z]\s?\d[A-Za-z]\d$/);
158162
});
163+
164+
it.each([
165+
['IL', 60001, 62999],
166+
['GA', 30001, 31999],
167+
['WA', 98001, 99403],
168+
])('returns zipCode valid for state %s', (state, lower, upper) => {
169+
const zipCode1 = +fakerEN_US.location.zipCode({ state });
170+
expect(zipCode1).toBeGreaterThanOrEqual(lower);
171+
expect(zipCode1).toBeLessThanOrEqual(upper);
172+
});
173+
174+
it('should throw when definitions.location.postcode_by_state not set', () => {
175+
expect(() => faker.location.zipCode({ state: 'XX' })).toThrow(
176+
new FakerError('No zip code definition found for state "XX"')
177+
);
178+
});
159179
});
160180

161181
describe('zipCodeByState()', () => {

0 commit comments

Comments
 (0)