isVAT.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. "use strict";
  2. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = isVAT;
  7. exports.vatMatchers = void 0;
  8. var _assertString = _interopRequireDefault(require("./util/assertString"));
  9. var algorithms = _interopRequireWildcard(require("./util/algorithms"));
  10. function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
  11. function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. var AU = function AU(str) {
  14. var match = str.match(/^(AU)?(\d{11})$/);
  15. if (!match) {
  16. return false;
  17. }
  18. // @see {@link https://abr.business.gov.au/Help/AbnFormat}
  19. var weights = [10, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
  20. str = str.replace(/^AU/, '');
  21. var ABN = (parseInt(str.slice(0, 1), 10) - 1).toString() + str.slice(1);
  22. var total = 0;
  23. for (var i = 0; i < 11; i++) {
  24. total += weights[i] * ABN.charAt(i);
  25. }
  26. return total !== 0 && total % 89 === 0;
  27. };
  28. var CH = function CH(str) {
  29. // @see {@link https://www.ech.ch/de/ech/ech-0097/5.2.0}
  30. var hasValidCheckNumber = function hasValidCheckNumber(digits) {
  31. var lastDigit = digits.pop(); // used as check number
  32. var weights = [5, 4, 3, 2, 7, 6, 5, 4];
  33. var calculatedCheckNumber = (11 - digits.reduce(function (acc, el, idx) {
  34. return acc + el * weights[idx];
  35. }, 0) % 11) % 11;
  36. return lastDigit === calculatedCheckNumber;
  37. };
  38. // @see {@link https://www.estv.admin.ch/estv/de/home/mehrwertsteuer/uid/mwst-uid-nummer.html}
  39. return /^(CHE[- ]?)?(\d{9}|(\d{3}\.\d{3}\.\d{3})|(\d{3} \d{3} \d{3})) ?(TVA|MWST|IVA)?$/.test(str) && hasValidCheckNumber(str.match(/\d/g).map(function (el) {
  40. return +el;
  41. }));
  42. };
  43. var PT = function PT(str) {
  44. var match = str.match(/^(PT)?(\d{9})$/);
  45. if (!match) {
  46. return false;
  47. }
  48. var tin = match[2];
  49. var checksum = 11 - algorithms.reverseMultiplyAndSum(tin.split('').slice(0, 8).map(function (a) {
  50. return parseInt(a, 10);
  51. }), 9) % 11;
  52. if (checksum > 9) {
  53. return parseInt(tin[8], 10) === 0;
  54. }
  55. return checksum === parseInt(tin[8], 10);
  56. };
  57. var vatMatchers = exports.vatMatchers = {
  58. /**
  59. * European Union VAT identification numbers
  60. */
  61. AT: function AT(str) {
  62. return /^(AT)?U\d{8}$/.test(str);
  63. },
  64. BE: function BE(str) {
  65. return /^(BE)?\d{10}$/.test(str);
  66. },
  67. BG: function BG(str) {
  68. return /^(BG)?\d{9,10}$/.test(str);
  69. },
  70. HR: function HR(str) {
  71. return /^(HR)?\d{11}$/.test(str);
  72. },
  73. CY: function CY(str) {
  74. return /^(CY)?\w{9}$/.test(str);
  75. },
  76. CZ: function CZ(str) {
  77. return /^(CZ)?\d{8,10}$/.test(str);
  78. },
  79. DK: function DK(str) {
  80. return /^(DK)?\d{8}$/.test(str);
  81. },
  82. EE: function EE(str) {
  83. return /^(EE)?\d{9}$/.test(str);
  84. },
  85. FI: function FI(str) {
  86. return /^(FI)?\d{8}$/.test(str);
  87. },
  88. FR: function FR(str) {
  89. return /^(FR)?\w{2}\d{9}$/.test(str);
  90. },
  91. DE: function DE(str) {
  92. return /^(DE)?\d{9}$/.test(str);
  93. },
  94. EL: function EL(str) {
  95. return /^(EL)?\d{9}$/.test(str);
  96. },
  97. HU: function HU(str) {
  98. return /^(HU)?\d{8}$/.test(str);
  99. },
  100. IE: function IE(str) {
  101. return /^(IE)?\d{7}\w{1}(W)?$/.test(str);
  102. },
  103. IT: function IT(str) {
  104. return /^(IT)?\d{11}$/.test(str);
  105. },
  106. LV: function LV(str) {
  107. return /^(LV)?\d{11}$/.test(str);
  108. },
  109. LT: function LT(str) {
  110. return /^(LT)?\d{9,12}$/.test(str);
  111. },
  112. LU: function LU(str) {
  113. return /^(LU)?\d{8}$/.test(str);
  114. },
  115. MT: function MT(str) {
  116. return /^(MT)?\d{8}$/.test(str);
  117. },
  118. NL: function NL(str) {
  119. return /^(NL)?\d{9}B\d{2}$/.test(str);
  120. },
  121. PL: function PL(str) {
  122. return /^(PL)?(\d{10}|(\d{3}-\d{3}-\d{2}-\d{2})|(\d{3}-\d{2}-\d{2}-\d{3}))$/.test(str);
  123. },
  124. PT: PT,
  125. RO: function RO(str) {
  126. return /^(RO)?\d{2,10}$/.test(str);
  127. },
  128. SK: function SK(str) {
  129. return /^(SK)?\d{10}$/.test(str);
  130. },
  131. SI: function SI(str) {
  132. return /^(SI)?\d{8}$/.test(str);
  133. },
  134. ES: function ES(str) {
  135. return /^(ES)?\w\d{7}[A-Z]$/.test(str);
  136. },
  137. SE: function SE(str) {
  138. return /^(SE)?\d{12}$/.test(str);
  139. },
  140. /**
  141. * VAT numbers of non-EU countries
  142. */
  143. AL: function AL(str) {
  144. return /^(AL)?\w{9}[A-Z]$/.test(str);
  145. },
  146. MK: function MK(str) {
  147. return /^(MK)?\d{13}$/.test(str);
  148. },
  149. AU: AU,
  150. BY: function BY(str) {
  151. return /^(УНП )?\d{9}$/.test(str);
  152. },
  153. CA: function CA(str) {
  154. return /^(CA)?\d{9}$/.test(str);
  155. },
  156. IS: function IS(str) {
  157. return /^(IS)?\d{5,6}$/.test(str);
  158. },
  159. IN: function IN(str) {
  160. return /^(IN)?\d{15}$/.test(str);
  161. },
  162. ID: function ID(str) {
  163. return /^(ID)?(\d{15}|(\d{2}.\d{3}.\d{3}.\d{1}-\d{3}.\d{3}))$/.test(str);
  164. },
  165. IL: function IL(str) {
  166. return /^(IL)?\d{9}$/.test(str);
  167. },
  168. KZ: function KZ(str) {
  169. return /^(KZ)?\d{12}$/.test(str);
  170. },
  171. NZ: function NZ(str) {
  172. return /^(NZ)?\d{9}$/.test(str);
  173. },
  174. NG: function NG(str) {
  175. return /^(NG)?(\d{12}|(\d{8}-\d{4}))$/.test(str);
  176. },
  177. NO: function NO(str) {
  178. return /^(NO)?\d{9}MVA$/.test(str);
  179. },
  180. PH: function PH(str) {
  181. return /^(PH)?(\d{12}|\d{3} \d{3} \d{3} \d{3})$/.test(str);
  182. },
  183. RU: function RU(str) {
  184. return /^(RU)?(\d{10}|\d{12})$/.test(str);
  185. },
  186. SM: function SM(str) {
  187. return /^(SM)?\d{5}$/.test(str);
  188. },
  189. SA: function SA(str) {
  190. return /^(SA)?\d{15}$/.test(str);
  191. },
  192. RS: function RS(str) {
  193. return /^(RS)?\d{9}$/.test(str);
  194. },
  195. CH: CH,
  196. TR: function TR(str) {
  197. return /^(TR)?\d{10}$/.test(str);
  198. },
  199. UA: function UA(str) {
  200. return /^(UA)?\d{12}$/.test(str);
  201. },
  202. GB: function GB(str) {
  203. return /^GB((\d{3} \d{4} ([0-8][0-9]|9[0-6]))|(\d{9} \d{3})|(((GD[0-4])|(HA[5-9]))[0-9]{2}))$/.test(str);
  204. },
  205. UZ: function UZ(str) {
  206. return /^(UZ)?\d{9}$/.test(str);
  207. },
  208. /**
  209. * VAT numbers of Latin American countries
  210. */
  211. AR: function AR(str) {
  212. return /^(AR)?\d{11}$/.test(str);
  213. },
  214. BO: function BO(str) {
  215. return /^(BO)?\d{7}$/.test(str);
  216. },
  217. BR: function BR(str) {
  218. return /^(BR)?((\d{2}.\d{3}.\d{3}\/\d{4}-\d{2})|(\d{3}.\d{3}.\d{3}-\d{2}))$/.test(str);
  219. },
  220. CL: function CL(str) {
  221. return /^(CL)?\d{8}-\d{1}$/.test(str);
  222. },
  223. CO: function CO(str) {
  224. return /^(CO)?\d{10}$/.test(str);
  225. },
  226. CR: function CR(str) {
  227. return /^(CR)?\d{9,12}$/.test(str);
  228. },
  229. EC: function EC(str) {
  230. return /^(EC)?\d{13}$/.test(str);
  231. },
  232. SV: function SV(str) {
  233. return /^(SV)?\d{4}-\d{6}-\d{3}-\d{1}$/.test(str);
  234. },
  235. GT: function GT(str) {
  236. return /^(GT)?\d{7}-\d{1}$/.test(str);
  237. },
  238. HN: function HN(str) {
  239. return /^(HN)?$/.test(str);
  240. },
  241. MX: function MX(str) {
  242. return /^(MX)?\w{3,4}\d{6}\w{3}$/.test(str);
  243. },
  244. NI: function NI(str) {
  245. return /^(NI)?\d{3}-\d{6}-\d{4}\w{1}$/.test(str);
  246. },
  247. PA: function PA(str) {
  248. return /^(PA)?$/.test(str);
  249. },
  250. PY: function PY(str) {
  251. return /^(PY)?\d{6,8}-\d{1}$/.test(str);
  252. },
  253. PE: function PE(str) {
  254. return /^(PE)?\d{11}$/.test(str);
  255. },
  256. DO: function DO(str) {
  257. return /^(DO)?(\d{11}|(\d{3}-\d{7}-\d{1})|[1,4,5]{1}\d{8}|([1,4,5]{1})-\d{2}-\d{5}-\d{1})$/.test(str);
  258. },
  259. UY: function UY(str) {
  260. return /^(UY)?\d{12}$/.test(str);
  261. },
  262. VE: function VE(str) {
  263. return /^(VE)?[J,G,V,E]{1}-(\d{9}|(\d{8}-\d{1}))$/.test(str);
  264. }
  265. };
  266. function isVAT(str, countryCode) {
  267. (0, _assertString.default)(str);
  268. (0, _assertString.default)(countryCode);
  269. if (countryCode in vatMatchers) {
  270. return vatMatchers[countryCode](str);
  271. }
  272. throw new Error("Invalid country code: '".concat(countryCode, "'"));
  273. }