-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
isPostalCode.js
99 lines (95 loc) · 2.61 KB
/
isPostalCode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import assertString from './util/assertString';
// common patterns
const threeDigit = /^\d{3}$/;
const fourDigit = /^\d{4}$/;
const fiveDigit = /^\d{5}$/;
const sixDigit = /^\d{6}$/;
const patterns = {
AD: /^AD\d{3}$/,
AT: fourDigit,
AU: fourDigit,
AZ: /^AZ\d{4}$/,
BA: /^([7-8]\d{4}$)/,
BE: fourDigit,
BG: fourDigit,
BR: /^\d{5}-?\d{3}$/,
BY: /^2[1-4]\d{4}$/,
CA: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s\-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i,
CH: fourDigit,
CN: /^(0[1-7]|1[012356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[1-5]|8[1345]|9[09])\d{4}$/,
CO: /^(05|08|11|13|15|17|18|19|20|23|25|27|41|44|47|50|52|54|63|66|68|70|73|76|81|85|86|88|91|94|95|97|99)(\d{4})$/,
CZ: /^\d{3}\s?\d{2}$/,
DE: fiveDigit,
DK: fourDigit,
DO: fiveDigit,
DZ: fiveDigit,
EE: fiveDigit,
ES: /^(5[0-2]{1}|[0-4]{1}\d{1})\d{3}$/,
FI: fiveDigit,
FR: /^\d{2}\s?\d{3}$/,
GB: /^(gir\s?0aa|[a-z]{1,2}\d[\da-z]?\s?(\d[a-z]{2})?)$/i,
GR: /^\d{3}\s?\d{2}$/,
HR: /^([1-5]\d{4}$)/,
HT: /^HT\d{4}$/,
HU: fourDigit,
ID: fiveDigit,
IE: /^(?!.*(?:o))[A-Za-z]\d[\dw]\s\w{4}$/i,
IL: /^(\d{5}|\d{7})$/,
IN: /^((?!10|29|35|54|55|65|66|86|87|88|89)[1-9][0-9]{5})$/,
IR: /^(?!(\d)\1{3})[13-9]{4}[1346-9][013-9]{5}$/,
IS: threeDigit,
IT: fiveDigit,
JP: /^\d{3}\-\d{4}$/,
KE: fiveDigit,
KR: /^(\d{5}|\d{6})$/,
LI: /^(948[5-9]|949[0-7])$/,
LT: /^LT\-\d{5}$/,
LU: fourDigit,
LV: /^LV\-\d{4}$/,
LK: fiveDigit,
MG: threeDigit,
MX: fiveDigit,
MT: /^[A-Za-z]{3}\s{0,1}\d{4}$/,
MY: fiveDigit,
NL: /^[1-9]\d{3}\s?(?!sa|sd|ss)[a-z]{2}$/i,
NO: fourDigit,
NP: /^(10|21|22|32|33|34|44|45|56|57)\d{3}$|^(977)$/i,
NZ: fourDigit,
PL: /^\d{2}\-\d{3}$/,
PR: /^00[679]\d{2}([ -]\d{4})?$/,
PT: /^\d{4}\-\d{3}?$/,
RO: sixDigit,
RU: sixDigit,
SA: fiveDigit,
SE: /^[1-9]\d{2}\s?\d{2}$/,
SG: sixDigit,
SI: fourDigit,
SK: /^\d{3}\s?\d{2}$/,
TH: fiveDigit,
TN: fourDigit,
TW: /^\d{3}(\d{2})?$/,
UA: fiveDigit,
US: /^\d{5}(-\d{4})?$/,
ZA: fourDigit,
ZM: fiveDigit,
};
export const locales = Object.keys(patterns);
export default function isPostalCode(str, locale) {
assertString(str);
if (locale in patterns) {
return patterns[locale].test(str);
} else if (locale === 'any') {
for (const key in patterns) {
// https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
// istanbul ignore else
if (patterns.hasOwnProperty(key)) {
const pattern = patterns[key];
if (pattern.test(str)) {
return true;
}
}
}
return false;
}
throw new Error(`Invalid locale '${locale}'`);
}