isPostalCode.js 2.7 KB

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