Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 18 additions & 1 deletion src/locales/fr/location/postcode.ts
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
export default ['#####'];
// French postal codes are five digits whose leading digits identify the
// department: metropolitan departments use the prefixes 01-95 (followed by
// three digits), while the overseas departments and collectivities use 971-978,
// 984 and 986-989 (followed by two digits). The 00xxx, 96xxx, 980-983xx, 985xx
// and 99xxx ranges are not assigned, so they are excluded here. See #3550 and
// https://en.wikipedia.org/wiki/Postal_codes_in_France
const range = (start: number, end: number): number[] =>
Array.from({ length: end - start + 1 }, (_, index) => start + index);

const metropolitan = range(1, 95).map(
(department) => `${department.toString().padStart(2, '0')}###`
);

const overseas = [...range(971, 978), 984, ...range(986, 989)].map(
(territory) => `${territory}##`
);

export default [...metropolitan, ...overseas];
20 changes: 20 additions & 0 deletions test/modules/location.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
faker,
fakerEN_CA,
fakerEN_US,
fakerFR,
fakerNL,
simpleFaker,
} from '../../src';
Expand Down Expand Up @@ -222,6 +223,25 @@ describe('location', () => {
}
});

it('should only return valid department prefixes for fr locale', () => {
// French postal codes use department prefixes 01-95 (metropolitan)
// and 971-978/984/986-989 (overseas); 00xxx, 96xxx and 99xxx are not
// assigned. See https://github.com/faker-js/faker/issues/3550
const overseas = new Set([
971, 972, 973, 974, 975, 976, 977, 978, 984, 986, 987, 988, 989,
]);

for (let i = 0; i < 1000; i++) {
const zipCode = fakerFR.location.zipCode();

expect(zipCode).toMatch(/^\d{5}$/);
const department = Number(zipCode.slice(0, 2));
const isMetropolitan = department >= 1 && department <= 95;
const isOverseas = overseas.has(Number(zipCode.slice(0, 3)));
expect(isMetropolitan || isOverseas).toBe(true);
}
});

it.each([
['IL', 60001, 62999],
['GA', 30001, 31999],
Expand Down