isPostalCode.js 3.0 KB

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