Skip to content
Open
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
55 changes: 36 additions & 19 deletions src/locales/en_CA/location/postcode.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,37 @@
export default [
'A#? #?#',
'B#? #?#',
'C#? #?#',
'E#? #?#',
'G#? #?#',
'H#? #?#',
'J#? #?#',
'K#? #?#',
'L#? #?#',
'M#? #?#',
'N#? #?#',
'P#? #?#',
'R#? #?#',
'S#? #?#',
'T#? #?#',
'V#? #?#',
'X#? #?#',
'Y#? #?#',
// Canadian postal codes alternate letters and digits, e.g. 'K1A 0B1'. The
// letters D, F, I, O, Q and U are never used, because they are easily confused
// with other characters when scanned. W and Z are additionally excluded from
// the first position, which identifies the postal district.
// See #1416 and https://en.wikipedia.org/wiki/Postal_codes_in_Canada
const letters = [
'A',
'B',
'C',
'E',
'G',
'H',
'J',
'K',
'L',
'M',
'N',
'P',
'R',
'S',
'T',
'V',
'W',
'X',
'Y',
'Z',
];

const firstLetters = letters.filter(
(letter) => letter !== 'W' && letter !== 'Z'
);

export default firstLetters.flatMap((first) =>
letters.flatMap((second) =>
letters.map((third) => `${first}#${second} #${third}#`)
)
);
2 changes: 1 addition & 1 deletion test/__snapshots__/locale-data.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ exports[`locale-data > should only have known characters 1`] = `
"en_AU": " #'()+-ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstuvwxyz",
"en_AU_ocker": " #()+-ABCDEFGHIJKLMNOPQRSTVWXYZabcdefghijklmnopqrstuvwxyz",
"en_BORK": "-BINTUabcdefghijklmnopqrstuvxyz",
"en_CA": " !#()+-.?ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyÎâèéô–’",
"en_CA": " !#()+-.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyÎâèéô–’",
"en_GB": " #'+,-.?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghiklmnopqrstuvwxyz",
"en_GH": " #+-.?ABCDEFGHIJKLMNOPQRSTUVWYZabcdefghijklmnoprstuvwxyzɔε",
"en_HK": " #'+,-.ABCDEFGHIJKLMNOPRSTUVWXYZabcdeghiklmnoprstuvwxyz",
Expand Down
13 changes: 13 additions & 0 deletions test/modules/location.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,19 @@ describe('location', () => {
}
});

it('should only return valid letters for en_CA locale', () => {
// Canadian postal codes never use the letters D, F, I, O, Q and U,
// and never use W or Z as the first letter.
// See https://github.com/faker-js/faker/issues/1416
for (let i = 0; i < 1000; i++) {
const zipCode = fakerEN_CA.location.zipCode();

expect(zipCode).toMatch(
/^[ABCEGHJ-NPRSTVXY]\d[ABCEGHJ-NPRSTV-Z] \d[ABCEGHJ-NPRSTV-Z]\d$/
);
}
});

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
Expand Down