UNPKG

1.96 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = _default;
7exports.locales = void 0;
8
9var _assertString = _interopRequireDefault(require("./util/assertString"));
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13// common patterns
14var threeDigit = /^\d{3}$/;
15var fourDigit = /^\d{4}$/;
16var fiveDigit = /^\d{5}$/;
17var sixDigit = /^\d{6}$/;
18var patterns = {
19 AD: /^AD\d{3}$/,
20 AT: fourDigit,
21 AU: fourDigit,
22 BE: fourDigit,
23 BG: fourDigit,
24 CA: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s\-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i,
25 CH: fourDigit,
26 CZ: /^\d{3}\s?\d{2}$/,
27 DE: fiveDigit,
28 DK: fourDigit,
29 DZ: fiveDigit,
30 EE: fiveDigit,
31 ES: fiveDigit,
32 FI: fiveDigit,
33 FR: /^\d{2}\s?\d{3}$/,
34 GB: /^(gir\s?0aa|[a-z]{1,2}\d[\da-z]?\s?(\d[a-z]{2})?)$/i,
35 GR: /^\d{3}\s?\d{2}$/,
36 HR: /^([1-5]\d{4}$)/,
37 HU: fourDigit,
38 IL: fiveDigit,
39 IN: sixDigit,
40 IS: threeDigit,
41 IT: fiveDigit,
42 JP: /^\d{3}\-\d{4}$/,
43 KE: fiveDigit,
44 LI: /^(948[5-9]|949[0-7])$/,
45 LT: /^LT\-\d{5}$/,
46 LU: fourDigit,
47 LV: /^LV\-\d{4}$/,
48 MX: fiveDigit,
49 NL: /^\d{4}\s?[a-z]{2}$/i,
50 NO: fourDigit,
51 PL: /^\d{2}\-\d{3}$/,
52 PT: /^\d{4}\-\d{3}?$/,
53 RO: sixDigit,
54 RU: sixDigit,
55 SA: fiveDigit,
56 SE: /^\d{3}\s?\d{2}$/,
57 SI: fourDigit,
58 SK: /^\d{3}\s?\d{2}$/,
59 TN: fourDigit,
60 TW: /^\d{3}(\d{2})?$/,
61 US: /^\d{5}(-\d{4})?$/,
62 ZA: fourDigit,
63 ZM: fiveDigit
64};
65var locales = Object.keys(patterns);
66exports.locales = locales;
67
68function _default(str, locale) {
69 (0, _assertString.default)(str);
70
71 if (locale in patterns) {
72 return patterns[locale].test(str);
73 } else if (locale === 'any') {
74 for (var key in patterns) {
75 if (patterns.hasOwnProperty(key)) {
76 var pattern = patterns[key];
77
78 if (pattern.test(str)) {
79 return true;
80 }
81 }
82 }
83
84 return false;
85 }
86
87 throw new Error("Invalid locale '".concat(locale, "'"));
88}
\No newline at end of file