isTaxID.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528
  1. "use strict";
  2. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = isTaxID;
  7. var _assertString = _interopRequireDefault(require("./util/assertString"));
  8. var algorithms = _interopRequireWildcard(require("./util/algorithms"));
  9. var _isDate = _interopRequireDefault(require("./isDate"));
  10. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
  11. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  14. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  15. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  16. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
  17. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  18. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  19. /**
  20. * TIN Validation
  21. * Validates Tax Identification Numbers (TINs) from the US, EU member states and the United Kingdom.
  22. *
  23. * EU-UK:
  24. * National TIN validity is calculated using public algorithms as made available by DG TAXUD.
  25. *
  26. * See `https://ec.europa.eu/taxation_customs/tin/specs/FS-TIN%20Algorithms-Public.docx` for more information.
  27. *
  28. * US:
  29. * An Employer Identification Number (EIN), also known as a Federal Tax Identification Number,
  30. * is used to identify a business entity.
  31. *
  32. * NOTES:
  33. * - Prefix 47 is being reserved for future use
  34. * - Prefixes 26, 27, 45, 46 and 47 were previously assigned by the Philadelphia campus.
  35. *
  36. * See `http://www.irs.gov/Businesses/Small-Businesses-&-Self-Employed/How-EINs-are-Assigned-and-Valid-EIN-Prefixes`
  37. * for more information.
  38. */
  39. // Locale functions
  40. /*
  41. * bg-BG validation function
  42. * (Edinen graždanski nomer (EGN/ЕГН), persons only)
  43. * Checks if birth date (first six digits) is valid and calculates check (last) digit
  44. */
  45. function bgBgCheck(tin) {
  46. // Extract full year, normalize month and check birth date validity
  47. var century_year = tin.slice(0, 2);
  48. var month = parseInt(tin.slice(2, 4), 10);
  49. if (month > 40) {
  50. month -= 40;
  51. century_year = "20".concat(century_year);
  52. } else if (month > 20) {
  53. month -= 20;
  54. century_year = "18".concat(century_year);
  55. } else {
  56. century_year = "19".concat(century_year);
  57. }
  58. if (month < 10) {
  59. month = "0".concat(month);
  60. }
  61. var date = "".concat(century_year, "/").concat(month, "/").concat(tin.slice(4, 6));
  62. if (!(0, _isDate.default)(date, 'YYYY/MM/DD')) {
  63. return false;
  64. } // split digits into an array for further processing
  65. var digits = tin.split('').map(function (a) {
  66. return parseInt(a, 10);
  67. }); // Calculate checksum by multiplying digits with fixed values
  68. var multip_lookup = [2, 4, 8, 5, 10, 9, 7, 3, 6];
  69. var checksum = 0;
  70. for (var i = 0; i < multip_lookup.length; i++) {
  71. checksum += digits[i] * multip_lookup[i];
  72. }
  73. checksum = checksum % 11 === 10 ? 0 : checksum % 11;
  74. return checksum === digits[9];
  75. }
  76. /*
  77. * cs-CZ validation function
  78. * (Rodné číslo (RČ), persons only)
  79. * Checks if birth date (first six digits) is valid and divisibility by 11
  80. * Material not in DG TAXUD document sourced from:
  81. * -`https://lorenc.info/3MA381/overeni-spravnosti-rodneho-cisla.htm`
  82. * -`https://www.mvcr.cz/clanek/rady-a-sluzby-dokumenty-rodne-cislo.aspx`
  83. */
  84. function csCzCheck(tin) {
  85. tin = tin.replace(/\W/, ''); // Extract full year from TIN length
  86. var full_year = parseInt(tin.slice(0, 2), 10);
  87. if (tin.length === 10) {
  88. if (full_year < 54) {
  89. full_year = "20".concat(full_year);
  90. } else {
  91. full_year = "19".concat(full_year);
  92. }
  93. } else {
  94. if (tin.slice(6) === '000') {
  95. return false;
  96. } // Three-zero serial not assigned before 1954
  97. if (full_year < 54) {
  98. full_year = "19".concat(full_year);
  99. } else {
  100. return false; // No 18XX years seen in any of the resources
  101. }
  102. } // Add missing zero if needed
  103. if (full_year.length === 3) {
  104. full_year = [full_year.slice(0, 2), '0', full_year.slice(2)].join('');
  105. } // Extract month from TIN and normalize
  106. var month = parseInt(tin.slice(2, 4), 10);
  107. if (month > 50) {
  108. month -= 50;
  109. }
  110. if (month > 20) {
  111. // Month-plus-twenty was only introduced in 2004
  112. if (parseInt(full_year, 10) < 2004) {
  113. return false;
  114. }
  115. month -= 20;
  116. }
  117. if (month < 10) {
  118. month = "0".concat(month);
  119. } // Check date validity
  120. var date = "".concat(full_year, "/").concat(month, "/").concat(tin.slice(4, 6));
  121. if (!(0, _isDate.default)(date, 'YYYY/MM/DD')) {
  122. return false;
  123. } // Verify divisibility by 11
  124. if (tin.length === 10) {
  125. if (parseInt(tin, 10) % 11 !== 0) {
  126. // Some numbers up to and including 1985 are still valid if
  127. // check (last) digit equals 0 and modulo of first 9 digits equals 10
  128. var checkdigit = parseInt(tin.slice(0, 9), 10) % 11;
  129. if (parseInt(full_year, 10) < 1986 && checkdigit === 10) {
  130. if (parseInt(tin.slice(9), 10) !== 0) {
  131. return false;
  132. }
  133. } else {
  134. return false;
  135. }
  136. }
  137. }
  138. return true;
  139. }
  140. /*
  141. * de-AT validation function
  142. * (Abgabenkontonummer, persons/entities)
  143. * Verify TIN validity by calling luhnCheck()
  144. */
  145. function deAtCheck(tin) {
  146. return algorithms.luhnCheck(tin);
  147. }
  148. /*
  149. * de-DE validation function
  150. * (Steueridentifikationsnummer (Steuer-IdNr.), persons only)
  151. * Tests for single duplicate/triplicate value, then calculates ISO 7064 check (last) digit
  152. * Partial implementation of spec (same result with both algorithms always)
  153. */
  154. function deDeCheck(tin) {
  155. // Split digits into an array for further processing
  156. var digits = tin.split('').map(function (a) {
  157. return parseInt(a, 10);
  158. }); // Fill array with strings of number positions
  159. var occurences = [];
  160. for (var i = 0; i < digits.length - 1; i++) {
  161. occurences.push('');
  162. for (var j = 0; j < digits.length - 1; j++) {
  163. if (digits[i] === digits[j]) {
  164. occurences[i] += j;
  165. }
  166. }
  167. } // Remove digits with one occurence and test for only one duplicate/triplicate
  168. occurences = occurences.filter(function (a) {
  169. return a.length > 1;
  170. });
  171. if (occurences.length !== 2 && occurences.length !== 3) {
  172. return false;
  173. } // In case of triplicate value only two digits are allowed next to each other
  174. if (occurences[0].length === 3) {
  175. var trip_locations = occurences[0].split('').map(function (a) {
  176. return parseInt(a, 10);
  177. });
  178. var recurrent = 0; // Amount of neighbour occurences
  179. for (var _i = 0; _i < trip_locations.length - 1; _i++) {
  180. if (trip_locations[_i] + 1 === trip_locations[_i + 1]) {
  181. recurrent += 1;
  182. }
  183. }
  184. if (recurrent === 2) {
  185. return false;
  186. }
  187. }
  188. return algorithms.iso7064Check(tin);
  189. }
  190. /*
  191. * dk-DK validation function
  192. * (CPR-nummer (personnummer), persons only)
  193. * Checks if birth date (first six digits) is valid and assigned to century (seventh) digit,
  194. * and calculates check (last) digit
  195. */
  196. function dkDkCheck(tin) {
  197. tin = tin.replace(/\W/, ''); // Extract year, check if valid for given century digit and add century
  198. var year = parseInt(tin.slice(4, 6), 10);
  199. var century_digit = tin.slice(6, 7);
  200. switch (century_digit) {
  201. case '0':
  202. case '1':
  203. case '2':
  204. case '3':
  205. year = "19".concat(year);
  206. break;
  207. case '4':
  208. case '9':
  209. if (year < 37) {
  210. year = "20".concat(year);
  211. } else {
  212. year = "19".concat(year);
  213. }
  214. break;
  215. default:
  216. if (year < 37) {
  217. year = "20".concat(year);
  218. } else if (year > 58) {
  219. year = "18".concat(year);
  220. } else {
  221. return false;
  222. }
  223. break;
  224. } // Add missing zero if needed
  225. if (year.length === 3) {
  226. year = [year.slice(0, 2), '0', year.slice(2)].join('');
  227. } // Check date validity
  228. var date = "".concat(year, "/").concat(tin.slice(2, 4), "/").concat(tin.slice(0, 2));
  229. if (!(0, _isDate.default)(date, 'YYYY/MM/DD')) {
  230. return false;
  231. } // Split digits into an array for further processing
  232. var digits = tin.split('').map(function (a) {
  233. return parseInt(a, 10);
  234. });
  235. var checksum = 0;
  236. var weight = 4; // Multiply by weight and add to checksum
  237. for (var i = 0; i < 9; i++) {
  238. checksum += digits[i] * weight;
  239. weight -= 1;
  240. if (weight === 1) {
  241. weight = 7;
  242. }
  243. }
  244. checksum %= 11;
  245. if (checksum === 1) {
  246. return false;
  247. }
  248. return checksum === 0 ? digits[9] === 0 : digits[9] === 11 - checksum;
  249. }
  250. /*
  251. * el-CY validation function
  252. * (Arithmos Forologikou Mitroou (AFM/ΑΦΜ), persons only)
  253. * Verify TIN validity by calculating ASCII value of check (last) character
  254. */
  255. function elCyCheck(tin) {
  256. // split digits into an array for further processing
  257. var digits = tin.slice(0, 8).split('').map(function (a) {
  258. return parseInt(a, 10);
  259. });
  260. var checksum = 0; // add digits in even places
  261. for (var i = 1; i < digits.length; i += 2) {
  262. checksum += digits[i];
  263. } // add digits in odd places
  264. for (var _i2 = 0; _i2 < digits.length; _i2 += 2) {
  265. if (digits[_i2] < 2) {
  266. checksum += 1 - digits[_i2];
  267. } else {
  268. checksum += 2 * (digits[_i2] - 2) + 5;
  269. if (digits[_i2] > 4) {
  270. checksum += 2;
  271. }
  272. }
  273. }
  274. return String.fromCharCode(checksum % 26 + 65) === tin.charAt(8);
  275. }
  276. /*
  277. * el-GR validation function
  278. * (Arithmos Forologikou Mitroou (AFM/ΑΦΜ), persons/entities)
  279. * Verify TIN validity by calculating check (last) digit
  280. * Algorithm not in DG TAXUD document- sourced from:
  281. * - `http://epixeirisi.gr/%CE%9A%CE%A1%CE%99%CE%A3%CE%99%CE%9C%CE%91-%CE%98%CE%95%CE%9C%CE%91%CE%A4%CE%91-%CE%A6%CE%9F%CE%A1%CE%9F%CE%9B%CE%9F%CE%93%CE%99%CE%91%CE%A3-%CE%9A%CE%91%CE%99-%CE%9B%CE%9F%CE%93%CE%99%CE%A3%CE%A4%CE%99%CE%9A%CE%97%CE%A3/23791/%CE%91%CF%81%CE%B9%CE%B8%CE%BC%CF%8C%CF%82-%CE%A6%CE%BF%CF%81%CE%BF%CE%BB%CE%BF%CE%B3%CE%B9%CE%BA%CE%BF%CF%8D-%CE%9C%CE%B7%CF%84%CF%81%CF%8E%CE%BF%CF%85`
  282. */
  283. function elGrCheck(tin) {
  284. // split digits into an array for further processing
  285. var digits = tin.split('').map(function (a) {
  286. return parseInt(a, 10);
  287. });
  288. var checksum = 0;
  289. for (var i = 0; i < 8; i++) {
  290. checksum += digits[i] * Math.pow(2, 8 - i);
  291. }
  292. return checksum % 11 % 10 === digits[8];
  293. }
  294. /*
  295. * en-GB validation function (should go here if needed)
  296. * (National Insurance Number (NINO) or Unique Taxpayer Reference (UTR),
  297. * persons/entities respectively)
  298. */
  299. /*
  300. * en-IE validation function
  301. * (Personal Public Service Number (PPS No), persons only)
  302. * Verify TIN validity by calculating check (second to last) character
  303. */
  304. function enIeCheck(tin) {
  305. var checksum = algorithms.reverseMultiplyAndSum(tin.split('').slice(0, 7).map(function (a) {
  306. return parseInt(a, 10);
  307. }), 8);
  308. if (tin.length === 9 && tin[8] !== 'W') {
  309. checksum += (tin[8].charCodeAt(0) - 64) * 9;
  310. }
  311. checksum %= 23;
  312. if (checksum === 0) {
  313. return tin[7].toUpperCase() === 'W';
  314. }
  315. return tin[7].toUpperCase() === String.fromCharCode(64 + checksum);
  316. } // Valid US IRS campus prefixes
  317. var enUsCampusPrefix = {
  318. andover: ['10', '12'],
  319. atlanta: ['60', '67'],
  320. austin: ['50', '53'],
  321. brookhaven: ['01', '02', '03', '04', '05', '06', '11', '13', '14', '16', '21', '22', '23', '25', '34', '51', '52', '54', '55', '56', '57', '58', '59', '65'],
  322. cincinnati: ['30', '32', '35', '36', '37', '38', '61'],
  323. fresno: ['15', '24'],
  324. internet: ['20', '26', '27', '45', '46', '47'],
  325. kansas: ['40', '44'],
  326. memphis: ['94', '95'],
  327. ogden: ['80', '90'],
  328. philadelphia: ['33', '39', '41', '42', '43', '46', '48', '62', '63', '64', '66', '68', '71', '72', '73', '74', '75', '76', '77', '81', '82', '83', '84', '85', '86', '87', '88', '91', '92', '93', '98', '99'],
  329. sba: ['31']
  330. }; // Return an array of all US IRS campus prefixes
  331. function enUsGetPrefixes() {
  332. var prefixes = [];
  333. for (var location in enUsCampusPrefix) {
  334. // https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
  335. // istanbul ignore else
  336. if (enUsCampusPrefix.hasOwnProperty(location)) {
  337. prefixes.push.apply(prefixes, _toConsumableArray(enUsCampusPrefix[location]));
  338. }
  339. }
  340. return prefixes;
  341. }
  342. /*
  343. * en-US validation function
  344. * Verify that the TIN starts with a valid IRS campus prefix
  345. */
  346. function enUsCheck(tin) {
  347. return enUsGetPrefixes().indexOf(tin.substr(0, 2)) !== -1;
  348. }
  349. /*
  350. * es-ES validation function
  351. * (Documento Nacional de Identidad (DNI)
  352. * or Número de Identificación de Extranjero (NIE), persons only)
  353. * Verify TIN validity by calculating check (last) character
  354. */
  355. function esEsCheck(tin) {
  356. // Split characters into an array for further processing
  357. var chars = tin.toUpperCase().split(''); // Replace initial letter if needed
  358. if (isNaN(parseInt(chars[0], 10)) && chars.length > 1) {
  359. var lead_replace = 0;
  360. switch (chars[0]) {
  361. case 'Y':
  362. lead_replace = 1;
  363. break;
  364. case 'Z':
  365. lead_replace = 2;
  366. break;
  367. default:
  368. }
  369. chars.splice(0, 1, lead_replace); // Fill with zeros if smaller than proper
  370. } else {
  371. while (chars.length < 9) {
  372. chars.unshift(0);
  373. }
  374. } // Calculate checksum and check according to lookup
  375. var lookup = ['T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X', 'B', 'N', 'J', 'Z', 'S', 'Q', 'V', 'H', 'L', 'C', 'K', 'E'];
  376. chars = chars.join('');
  377. var checksum = parseInt(chars.slice(0, 8), 10) % 23;
  378. return chars[8] === lookup[checksum];
  379. }
  380. /*
  381. * et-EE validation function
  382. * (Isikukood (IK), persons only)
  383. * Checks if birth date (century digit and six following) is valid and calculates check (last) digit
  384. * Material not in DG TAXUD document sourced from:
  385. * - `https://www.oecd.org/tax/automatic-exchange/crs-implementation-and-assistance/tax-identification-numbers/Estonia-TIN.pdf`
  386. */
  387. function etEeCheck(tin) {
  388. // Extract year and add century
  389. var full_year = tin.slice(1, 3);
  390. var century_digit = tin.slice(0, 1);
  391. switch (century_digit) {
  392. case '1':
  393. case '2':
  394. full_year = "18".concat(full_year);
  395. break;
  396. case '3':
  397. case '4':
  398. full_year = "19".concat(full_year);
  399. break;
  400. default:
  401. full_year = "20".concat(full_year);
  402. break;
  403. } // Check date validity
  404. var date = "".concat(full_year, "/").concat(tin.slice(3, 5), "/").concat(tin.slice(5, 7));
  405. if (!(0, _isDate.default)(date, 'YYYY/MM/DD')) {
  406. return false;
  407. } // Split digits into an array for further processing
  408. var digits = tin.split('').map(function (a) {
  409. return parseInt(a, 10);
  410. });
  411. var checksum = 0;
  412. var weight = 1; // Multiply by weight and add to checksum
  413. for (var i = 0; i < 10; i++) {
  414. checksum += digits[i] * weight;
  415. weight += 1;
  416. if (weight === 10) {
  417. weight = 1;
  418. }
  419. } // Do again if modulo 11 of checksum is 10
  420. if (checksum % 11 === 10) {
  421. checksum = 0;
  422. weight = 3;
  423. for (var _i3 = 0; _i3 < 10; _i3++) {
  424. checksum += digits[_i3] * weight;
  425. weight += 1;
  426. if (weight === 10) {
  427. weight = 1;
  428. }
  429. }
  430. if (checksum % 11 === 10) {
  431. return digits[10] === 0;
  432. }
  433. }
  434. return checksum % 11 === digits[10];
  435. }
  436. /*
  437. * fi-FI validation function
  438. * (Henkilötunnus (HETU), persons only)
  439. * Checks if birth date (first six digits plus century symbol) is valid
  440. * and calculates check (last) digit
  441. */
  442. function fiFiCheck(tin) {
  443. // Extract year and add century
  444. var full_year = tin.slice(4, 6);
  445. var century_symbol = tin.slice(6, 7);
  446. switch (century_symbol) {
  447. case '+':
  448. full_year = "18".concat(full_year);
  449. break;
  450. case '-':
  451. full_year = "19".concat(full_year);
  452. break;
  453. default:
  454. full_year = "20".concat(full_year);
  455. break;
  456. } // Check date validity
  457. var date = "".concat(full_year, "/").concat(tin.slice(2, 4), "/").concat(tin.slice(0, 2));
  458. if (!(0, _isDate.default)(date, 'YYYY/MM/DD')) {
  459. return false;
  460. } // Calculate check character
  461. var checksum = parseInt(tin.slice(0, 6) + tin.slice(7, 10), 10) % 31;
  462. if (checksum < 10) {
  463. return checksum === parseInt(tin.slice(10), 10);
  464. }
  465. checksum -= 10;
  466. var letters_lookup = ['A', 'B', 'C', 'D', 'E', 'F', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y'];
  467. return letters_lookup[checksum] === tin.slice(10);
  468. }
  469. /*
  470. * fr/nl-BE validation function
  471. * (Numéro national (N.N.), persons only)
  472. * Checks if birth date (first six digits) is valid and calculates check (last two) digits
  473. */
  474. function frBeCheck(tin) {
  475. // Zero month/day value is acceptable
  476. if (tin.slice(2, 4) !== '00' || tin.slice(4, 6) !== '00') {
  477. // Extract date from first six digits of TIN
  478. var date = "".concat(tin.slice(0, 2), "/").concat(tin.slice(2, 4), "/").concat(tin.slice(4, 6));
  479. if (!(0, _isDate.default)(date, 'YY/MM/DD')) {
  480. return false;
  481. }
  482. }
  483. var checksum = 97 - parseInt(tin.slice(0, 9), 10) % 97;
  484. var checkdigits = parseInt(tin.slice(9, 11), 10);
  485. if (checksum !== checkdigits) {
  486. checksum = 97 - parseInt("2".concat(tin.slice(0, 9)), 10) % 97;
  487. if (checksum !== checkdigits) {
  488. return false;
  489. }
  490. }
  491. return true;
  492. }
  493. /*
  494. * fr-FR validation function
  495. * (Numéro fiscal de référence (numéro SPI), persons only)
  496. * Verify TIN validity by calculating check (last three) digits
  497. */
  498. function frFrCheck(tin) {
  499. tin = tin.replace(/\s/g, '');
  500. var checksum = parseInt(tin.slice(0, 10), 10) % 511;
  501. var checkdigits = parseInt(tin.slice(10, 13), 10);
  502. return checksum === checkdigits;
  503. }
  504. /*
  505. * fr/lb-LU validation function
  506. * (numéro d’identification personnelle, persons only)
  507. * Verify birth date validity and run Luhn and Verhoeff checks
  508. */
  509. function frLuCheck(tin) {
  510. // Extract date and check validity
  511. var date = "".concat(tin.slice(0, 4), "/").concat(tin.slice(4, 6), "/").concat(tin.slice(6, 8));
  512. if (!(0, _isDate.default)(date, 'YYYY/MM/DD')) {
  513. return false;
  514. } // Run Luhn check
  515. if (!algorithms.luhnCheck(tin.slice(0, 12))) {
  516. return false;
  517. } // Remove Luhn check digit and run Verhoeff check
  518. return algorithms.verhoeffCheck("".concat(tin.slice(0, 11)).concat(tin[12]));
  519. }
  520. /*
  521. * hr-HR validation function
  522. * (Osobni identifikacijski broj (OIB), persons/entities)
  523. * Verify TIN validity by calling iso7064Check(digits)
  524. */
  525. function hrHrCheck(tin) {
  526. return algorithms.iso7064Check(tin);
  527. }
  528. /*
  529. * hu-HU validation function
  530. * (Adóazonosító jel, persons only)
  531. * Verify TIN validity by calculating check (last) digit
  532. */
  533. function huHuCheck(tin) {
  534. // split digits into an array for further processing
  535. var digits = tin.split('').map(function (a) {
  536. return parseInt(a, 10);
  537. });
  538. var checksum = 8;
  539. for (var i = 1; i < 9; i++) {
  540. checksum += digits[i] * (i + 1);
  541. }
  542. return checksum % 11 === digits[9];
  543. }
  544. /*
  545. * lt-LT validation function (should go here if needed)
  546. * (Asmens kodas, persons/entities respectively)
  547. * Current validation check is alias of etEeCheck- same format applies
  548. */
  549. /*
  550. * it-IT first/last name validity check
  551. * Accepts it-IT TIN-encoded names as a three-element character array and checks their validity
  552. * Due to lack of clarity between resources ("Are only Italian consonants used?
  553. * What happens if a person has X in their name?" etc.) only two test conditions
  554. * have been implemented:
  555. * Vowels may only be followed by other vowels or an X character
  556. * and X characters after vowels may only be followed by other X characters.
  557. */
  558. function itItNameCheck(name) {
  559. // true at the first occurence of a vowel
  560. var vowelflag = false; // true at the first occurence of an X AFTER vowel
  561. // (to properly handle last names with X as consonant)
  562. var xflag = false;
  563. for (var i = 0; i < 3; i++) {
  564. if (!vowelflag && /[AEIOU]/.test(name[i])) {
  565. vowelflag = true;
  566. } else if (!xflag && vowelflag && name[i] === 'X') {
  567. xflag = true;
  568. } else if (i > 0) {
  569. if (vowelflag && !xflag) {
  570. if (!/[AEIOU]/.test(name[i])) {
  571. return false;
  572. }
  573. }
  574. if (xflag) {
  575. if (!/X/.test(name[i])) {
  576. return false;
  577. }
  578. }
  579. }
  580. }
  581. return true;
  582. }
  583. /*
  584. * it-IT validation function
  585. * (Codice fiscale (TIN-IT), persons only)
  586. * Verify name, birth date and codice catastale validity
  587. * and calculate check character.
  588. * Material not in DG-TAXUD document sourced from:
  589. * `https://en.wikipedia.org/wiki/Italian_fiscal_code`
  590. */
  591. function itItCheck(tin) {
  592. // Capitalize and split characters into an array for further processing
  593. var chars = tin.toUpperCase().split(''); // Check first and last name validity calling itItNameCheck()
  594. if (!itItNameCheck(chars.slice(0, 3))) {
  595. return false;
  596. }
  597. if (!itItNameCheck(chars.slice(3, 6))) {
  598. return false;
  599. } // Convert letters in number spaces back to numbers if any
  600. var number_locations = [6, 7, 9, 10, 12, 13, 14];
  601. var number_replace = {
  602. L: '0',
  603. M: '1',
  604. N: '2',
  605. P: '3',
  606. Q: '4',
  607. R: '5',
  608. S: '6',
  609. T: '7',
  610. U: '8',
  611. V: '9'
  612. };
  613. for (var _i4 = 0, _number_locations = number_locations; _i4 < _number_locations.length; _i4++) {
  614. var i = _number_locations[_i4];
  615. if (chars[i] in number_replace) {
  616. chars.splice(i, 1, number_replace[chars[i]]);
  617. }
  618. } // Extract month and day, and check date validity
  619. var month_replace = {
  620. A: '01',
  621. B: '02',
  622. C: '03',
  623. D: '04',
  624. E: '05',
  625. H: '06',
  626. L: '07',
  627. M: '08',
  628. P: '09',
  629. R: '10',
  630. S: '11',
  631. T: '12'
  632. };
  633. var month = month_replace[chars[8]];
  634. var day = parseInt(chars[9] + chars[10], 10);
  635. if (day > 40) {
  636. day -= 40;
  637. }
  638. if (day < 10) {
  639. day = "0".concat(day);
  640. }
  641. var date = "".concat(chars[6]).concat(chars[7], "/").concat(month, "/").concat(day);
  642. if (!(0, _isDate.default)(date, 'YY/MM/DD')) {
  643. return false;
  644. } // Calculate check character by adding up even and odd characters as numbers
  645. var checksum = 0;
  646. for (var _i5 = 1; _i5 < chars.length - 1; _i5 += 2) {
  647. var char_to_int = parseInt(chars[_i5], 10);
  648. if (isNaN(char_to_int)) {
  649. char_to_int = chars[_i5].charCodeAt(0) - 65;
  650. }
  651. checksum += char_to_int;
  652. }
  653. var odd_convert = {
  654. // Maps of characters at odd places
  655. A: 1,
  656. B: 0,
  657. C: 5,
  658. D: 7,
  659. E: 9,
  660. F: 13,
  661. G: 15,
  662. H: 17,
  663. I: 19,
  664. J: 21,
  665. K: 2,
  666. L: 4,
  667. M: 18,
  668. N: 20,
  669. O: 11,
  670. P: 3,
  671. Q: 6,
  672. R: 8,
  673. S: 12,
  674. T: 14,
  675. U: 16,
  676. V: 10,
  677. W: 22,
  678. X: 25,
  679. Y: 24,
  680. Z: 23,
  681. 0: 1,
  682. 1: 0
  683. };
  684. for (var _i6 = 0; _i6 < chars.length - 1; _i6 += 2) {
  685. var _char_to_int = 0;
  686. if (chars[_i6] in odd_convert) {
  687. _char_to_int = odd_convert[chars[_i6]];
  688. } else {
  689. var multiplier = parseInt(chars[_i6], 10);
  690. _char_to_int = 2 * multiplier + 1;
  691. if (multiplier > 4) {
  692. _char_to_int += 2;
  693. }
  694. }
  695. checksum += _char_to_int;
  696. }
  697. if (String.fromCharCode(65 + checksum % 26) !== chars[15]) {
  698. return false;
  699. }
  700. return true;
  701. }
  702. /*
  703. * lv-LV validation function
  704. * (Personas kods (PK), persons only)
  705. * Check validity of birth date and calculate check (last) digit
  706. * Support only for old format numbers (not starting with '32', issued before 2017/07/01)
  707. * Material not in DG TAXUD document sourced from:
  708. * `https://boot.ritakafija.lv/forums/index.php?/topic/88314-personas-koda-algoritms-%C4%8Deksumma/`
  709. */
  710. function lvLvCheck(tin) {
  711. tin = tin.replace(/\W/, ''); // Extract date from TIN
  712. var day = tin.slice(0, 2);
  713. if (day !== '32') {
  714. // No date/checksum check if new format
  715. var month = tin.slice(2, 4);
  716. if (month !== '00') {
  717. // No date check if unknown month
  718. var full_year = tin.slice(4, 6);
  719. switch (tin[6]) {
  720. case '0':
  721. full_year = "18".concat(full_year);
  722. break;
  723. case '1':
  724. full_year = "19".concat(full_year);
  725. break;
  726. default:
  727. full_year = "20".concat(full_year);
  728. break;
  729. } // Check date validity
  730. var date = "".concat(full_year, "/").concat(tin.slice(2, 4), "/").concat(day);
  731. if (!(0, _isDate.default)(date, 'YYYY/MM/DD')) {
  732. return false;
  733. }
  734. } // Calculate check digit
  735. var checksum = 1101;
  736. var multip_lookup = [1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  737. for (var i = 0; i < tin.length - 1; i++) {
  738. checksum -= parseInt(tin[i], 10) * multip_lookup[i];
  739. }
  740. return parseInt(tin[10], 10) === checksum % 11;
  741. }
  742. return true;
  743. }
  744. /*
  745. * mt-MT validation function
  746. * (Identity Card Number or Unique Taxpayer Reference, persons/entities)
  747. * Verify Identity Card Number structure (no other tests found)
  748. */
  749. function mtMtCheck(tin) {
  750. if (tin.length !== 9) {
  751. // No tests for UTR
  752. var chars = tin.toUpperCase().split(''); // Fill with zeros if smaller than proper
  753. while (chars.length < 8) {
  754. chars.unshift(0);
  755. } // Validate format according to last character
  756. switch (tin[7]) {
  757. case 'A':
  758. case 'P':
  759. if (parseInt(chars[6], 10) === 0) {
  760. return false;
  761. }
  762. break;
  763. default:
  764. {
  765. var first_part = parseInt(chars.join('').slice(0, 5), 10);
  766. if (first_part > 32000) {
  767. return false;
  768. }
  769. var second_part = parseInt(chars.join('').slice(5, 7), 10);
  770. if (first_part === second_part) {
  771. return false;
  772. }
  773. }
  774. }
  775. }
  776. return true;
  777. }
  778. /*
  779. * nl-NL validation function
  780. * (Burgerservicenummer (BSN) or Rechtspersonen Samenwerkingsverbanden Informatie Nummer (RSIN),
  781. * persons/entities respectively)
  782. * Verify TIN validity by calculating check (last) digit (variant of MOD 11)
  783. */
  784. function nlNlCheck(tin) {
  785. return algorithms.reverseMultiplyAndSum(tin.split('').slice(0, 8).map(function (a) {
  786. return parseInt(a, 10);
  787. }), 9) % 11 === parseInt(tin[8], 10);
  788. }
  789. /*
  790. * pl-PL validation function
  791. * (Powszechny Elektroniczny System Ewidencji Ludności (PESEL)
  792. * or Numer identyfikacji podatkowej (NIP), persons/entities)
  793. * Verify TIN validity by validating birth date (PESEL) and calculating check (last) digit
  794. */
  795. function plPlCheck(tin) {
  796. // NIP
  797. if (tin.length === 10) {
  798. // Calculate last digit by multiplying with lookup
  799. var lookup = [6, 5, 7, 2, 3, 4, 5, 6, 7];
  800. var _checksum = 0;
  801. for (var i = 0; i < lookup.length; i++) {
  802. _checksum += parseInt(tin[i], 10) * lookup[i];
  803. }
  804. _checksum %= 11;
  805. if (_checksum === 10) {
  806. return false;
  807. }
  808. return _checksum === parseInt(tin[9], 10);
  809. } // PESEL
  810. // Extract full year using month
  811. var full_year = tin.slice(0, 2);
  812. var month = parseInt(tin.slice(2, 4), 10);
  813. if (month > 80) {
  814. full_year = "18".concat(full_year);
  815. month -= 80;
  816. } else if (month > 60) {
  817. full_year = "22".concat(full_year);
  818. month -= 60;
  819. } else if (month > 40) {
  820. full_year = "21".concat(full_year);
  821. month -= 40;
  822. } else if (month > 20) {
  823. full_year = "20".concat(full_year);
  824. month -= 20;
  825. } else {
  826. full_year = "19".concat(full_year);
  827. } // Add leading zero to month if needed
  828. if (month < 10) {
  829. month = "0".concat(month);
  830. } // Check date validity
  831. var date = "".concat(full_year, "/").concat(month, "/").concat(tin.slice(4, 6));
  832. if (!(0, _isDate.default)(date, 'YYYY/MM/DD')) {
  833. return false;
  834. } // Calculate last digit by mulitplying with odd one-digit numbers except 5
  835. var checksum = 0;
  836. var multiplier = 1;
  837. for (var _i7 = 0; _i7 < tin.length - 1; _i7++) {
  838. checksum += parseInt(tin[_i7], 10) * multiplier % 10;
  839. multiplier += 2;
  840. if (multiplier > 10) {
  841. multiplier = 1;
  842. } else if (multiplier === 5) {
  843. multiplier += 2;
  844. }
  845. }
  846. checksum = 10 - checksum % 10;
  847. return checksum === parseInt(tin[10], 10);
  848. }
  849. /*
  850. * pt-BR validation function
  851. * (Cadastro de Pessoas Físicas (CPF, persons)
  852. * Cadastro Nacional de Pessoas Jurídicas (CNPJ, entities)
  853. * Both inputs will be validated
  854. */
  855. function ptBrCheck(tin) {
  856. if (tin.length === 11) {
  857. var _sum;
  858. var remainder;
  859. _sum = 0;
  860. if ( // Reject known invalid CPFs
  861. tin === '11111111111' || tin === '22222222222' || tin === '33333333333' || tin === '44444444444' || tin === '55555555555' || tin === '66666666666' || tin === '77777777777' || tin === '88888888888' || tin === '99999999999' || tin === '00000000000') return false;
  862. for (var i = 1; i <= 9; i++) {
  863. _sum += parseInt(tin.substring(i - 1, i), 10) * (11 - i);
  864. }
  865. remainder = _sum * 10 % 11;
  866. if (remainder === 10) remainder = 0;
  867. if (remainder !== parseInt(tin.substring(9, 10), 10)) return false;
  868. _sum = 0;
  869. for (var _i8 = 1; _i8 <= 10; _i8++) {
  870. _sum += parseInt(tin.substring(_i8 - 1, _i8), 10) * (12 - _i8);
  871. }
  872. remainder = _sum * 10 % 11;
  873. if (remainder === 10) remainder = 0;
  874. if (remainder !== parseInt(tin.substring(10, 11), 10)) return false;
  875. return true;
  876. }
  877. if ( // Reject know invalid CNPJs
  878. tin === '00000000000000' || tin === '11111111111111' || tin === '22222222222222' || tin === '33333333333333' || tin === '44444444444444' || tin === '55555555555555' || tin === '66666666666666' || tin === '77777777777777' || tin === '88888888888888' || tin === '99999999999999') {
  879. return false;
  880. }
  881. var length = tin.length - 2;
  882. var identifiers = tin.substring(0, length);
  883. var verificators = tin.substring(length);
  884. var sum = 0;
  885. var pos = length - 7;
  886. for (var _i9 = length; _i9 >= 1; _i9--) {
  887. sum += identifiers.charAt(length - _i9) * pos;
  888. pos -= 1;
  889. if (pos < 2) {
  890. pos = 9;
  891. }
  892. }
  893. var result = sum % 11 < 2 ? 0 : 11 - sum % 11;
  894. if (result !== parseInt(verificators.charAt(0), 10)) {
  895. return false;
  896. }
  897. length += 1;
  898. identifiers = tin.substring(0, length);
  899. sum = 0;
  900. pos = length - 7;
  901. for (var _i10 = length; _i10 >= 1; _i10--) {
  902. sum += identifiers.charAt(length - _i10) * pos;
  903. pos -= 1;
  904. if (pos < 2) {
  905. pos = 9;
  906. }
  907. }
  908. result = sum % 11 < 2 ? 0 : 11 - sum % 11;
  909. if (result !== parseInt(verificators.charAt(1), 10)) {
  910. return false;
  911. }
  912. return true;
  913. }
  914. /*
  915. * pt-PT validation function
  916. * (Número de identificação fiscal (NIF), persons/entities)
  917. * Verify TIN validity by calculating check (last) digit (variant of MOD 11)
  918. */
  919. function ptPtCheck(tin) {
  920. var checksum = 11 - algorithms.reverseMultiplyAndSum(tin.split('').slice(0, 8).map(function (a) {
  921. return parseInt(a, 10);
  922. }), 9) % 11;
  923. if (checksum > 9) {
  924. return parseInt(tin[8], 10) === 0;
  925. }
  926. return checksum === parseInt(tin[8], 10);
  927. }
  928. /*
  929. * ro-RO validation function
  930. * (Cod Numeric Personal (CNP) or Cod de înregistrare fiscală (CIF),
  931. * persons only)
  932. * Verify CNP validity by calculating check (last) digit (test not found for CIF)
  933. * Material not in DG TAXUD document sourced from:
  934. * `https://en.wikipedia.org/wiki/National_identification_number#Romania`
  935. */
  936. function roRoCheck(tin) {
  937. if (tin.slice(0, 4) !== '9000') {
  938. // No test found for this format
  939. // Extract full year using century digit if possible
  940. var full_year = tin.slice(1, 3);
  941. switch (tin[0]) {
  942. case '1':
  943. case '2':
  944. full_year = "19".concat(full_year);
  945. break;
  946. case '3':
  947. case '4':
  948. full_year = "18".concat(full_year);
  949. break;
  950. case '5':
  951. case '6':
  952. full_year = "20".concat(full_year);
  953. break;
  954. default:
  955. } // Check date validity
  956. var date = "".concat(full_year, "/").concat(tin.slice(3, 5), "/").concat(tin.slice(5, 7));
  957. if (date.length === 8) {
  958. if (!(0, _isDate.default)(date, 'YY/MM/DD')) {
  959. return false;
  960. }
  961. } else if (!(0, _isDate.default)(date, 'YYYY/MM/DD')) {
  962. return false;
  963. } // Calculate check digit
  964. var digits = tin.split('').map(function (a) {
  965. return parseInt(a, 10);
  966. });
  967. var multipliers = [2, 7, 9, 1, 4, 6, 3, 5, 8, 2, 7, 9];
  968. var checksum = 0;
  969. for (var i = 0; i < multipliers.length; i++) {
  970. checksum += digits[i] * multipliers[i];
  971. }
  972. if (checksum % 11 === 10) {
  973. return digits[12] === 1;
  974. }
  975. return digits[12] === checksum % 11;
  976. }
  977. return true;
  978. }
  979. /*
  980. * sk-SK validation function
  981. * (Rodné číslo (RČ) or bezvýznamové identifikačné číslo (BIČ), persons only)
  982. * Checks validity of pre-1954 birth numbers (rodné číslo) only
  983. * Due to the introduction of the pseudo-random BIČ it is not possible to test
  984. * post-1954 birth numbers without knowing whether they are BIČ or RČ beforehand
  985. */
  986. function skSkCheck(tin) {
  987. if (tin.length === 9) {
  988. tin = tin.replace(/\W/, '');
  989. if (tin.slice(6) === '000') {
  990. return false;
  991. } // Three-zero serial not assigned before 1954
  992. // Extract full year from TIN length
  993. var full_year = parseInt(tin.slice(0, 2), 10);
  994. if (full_year > 53) {
  995. return false;
  996. }
  997. if (full_year < 10) {
  998. full_year = "190".concat(full_year);
  999. } else {
  1000. full_year = "19".concat(full_year);
  1001. } // Extract month from TIN and normalize
  1002. var month = parseInt(tin.slice(2, 4), 10);
  1003. if (month > 50) {
  1004. month -= 50;
  1005. }
  1006. if (month < 10) {
  1007. month = "0".concat(month);
  1008. } // Check date validity
  1009. var date = "".concat(full_year, "/").concat(month, "/").concat(tin.slice(4, 6));
  1010. if (!(0, _isDate.default)(date, 'YYYY/MM/DD')) {
  1011. return false;
  1012. }
  1013. }
  1014. return true;
  1015. }
  1016. /*
  1017. * sl-SI validation function
  1018. * (Davčna številka, persons/entities)
  1019. * Verify TIN validity by calculating check (last) digit (variant of MOD 11)
  1020. */
  1021. function slSiCheck(tin) {
  1022. var checksum = 11 - algorithms.reverseMultiplyAndSum(tin.split('').slice(0, 7).map(function (a) {
  1023. return parseInt(a, 10);
  1024. }), 8) % 11;
  1025. if (checksum === 10) {
  1026. return parseInt(tin[7], 10) === 0;
  1027. }
  1028. return checksum === parseInt(tin[7], 10);
  1029. }
  1030. /*
  1031. * sv-SE validation function
  1032. * (Personnummer or samordningsnummer, persons only)
  1033. * Checks validity of birth date and calls luhnCheck() to validate check (last) digit
  1034. */
  1035. function svSeCheck(tin) {
  1036. // Make copy of TIN and normalize to two-digit year form
  1037. var tin_copy = tin.slice(0);
  1038. if (tin.length > 11) {
  1039. tin_copy = tin_copy.slice(2);
  1040. } // Extract date of birth
  1041. var full_year = '';
  1042. var month = tin_copy.slice(2, 4);
  1043. var day = parseInt(tin_copy.slice(4, 6), 10);
  1044. if (tin.length > 11) {
  1045. full_year = tin.slice(0, 4);
  1046. } else {
  1047. full_year = tin.slice(0, 2);
  1048. if (tin.length === 11 && day < 60) {
  1049. // Extract full year from centenarian symbol
  1050. // Should work just fine until year 10000 or so
  1051. var current_year = new Date().getFullYear().toString();
  1052. var current_century = parseInt(current_year.slice(0, 2), 10);
  1053. current_year = parseInt(current_year, 10);
  1054. if (tin[6] === '-') {
  1055. if (parseInt("".concat(current_century).concat(full_year), 10) > current_year) {
  1056. full_year = "".concat(current_century - 1).concat(full_year);
  1057. } else {
  1058. full_year = "".concat(current_century).concat(full_year);
  1059. }
  1060. } else {
  1061. full_year = "".concat(current_century - 1).concat(full_year);
  1062. if (current_year - parseInt(full_year, 10) < 100) {
  1063. return false;
  1064. }
  1065. }
  1066. }
  1067. } // Normalize day and check date validity
  1068. if (day > 60) {
  1069. day -= 60;
  1070. }
  1071. if (day < 10) {
  1072. day = "0".concat(day);
  1073. }
  1074. var date = "".concat(full_year, "/").concat(month, "/").concat(day);
  1075. if (date.length === 8) {
  1076. if (!(0, _isDate.default)(date, 'YY/MM/DD')) {
  1077. return false;
  1078. }
  1079. } else if (!(0, _isDate.default)(date, 'YYYY/MM/DD')) {
  1080. return false;
  1081. }
  1082. return algorithms.luhnCheck(tin.replace(/\W/, ''));
  1083. } // Locale lookup objects
  1084. /*
  1085. * Tax id regex formats for various locales
  1086. *
  1087. * Where not explicitly specified in DG-TAXUD document both
  1088. * uppercase and lowercase letters are acceptable.
  1089. */
  1090. var taxIdFormat = {
  1091. 'bg-BG': /^\d{10}$/,
  1092. 'cs-CZ': /^\d{6}\/{0,1}\d{3,4}$/,
  1093. 'de-AT': /^\d{9}$/,
  1094. 'de-DE': /^[1-9]\d{10}$/,
  1095. 'dk-DK': /^\d{6}-{0,1}\d{4}$/,
  1096. 'el-CY': /^[09]\d{7}[A-Z]$/,
  1097. 'el-GR': /^([0-4]|[7-9])\d{8}$/,
  1098. 'en-GB': /^\d{10}$|^(?!GB|NK|TN|ZZ)(?![DFIQUV])[A-Z](?![DFIQUVO])[A-Z]\d{6}[ABCD ]$/i,
  1099. 'en-IE': /^\d{7}[A-W][A-IW]{0,1}$/i,
  1100. 'en-US': /^\d{2}[- ]{0,1}\d{7}$/,
  1101. 'es-ES': /^(\d{0,8}|[XYZKLM]\d{7})[A-HJ-NP-TV-Z]$/i,
  1102. 'et-EE': /^[1-6]\d{6}(00[1-9]|0[1-9][0-9]|[1-6][0-9]{2}|70[0-9]|710)\d$/,
  1103. 'fi-FI': /^\d{6}[-+A]\d{3}[0-9A-FHJ-NPR-Y]$/i,
  1104. 'fr-BE': /^\d{11}$/,
  1105. 'fr-FR': /^[0-3]\d{12}$|^[0-3]\d\s\d{2}(\s\d{3}){3}$/,
  1106. // Conforms both to official spec and provided example
  1107. 'fr-LU': /^\d{13}$/,
  1108. 'hr-HR': /^\d{11}$/,
  1109. 'hu-HU': /^8\d{9}$/,
  1110. 'it-IT': /^[A-Z]{6}[L-NP-V0-9]{2}[A-EHLMPRST][L-NP-V0-9]{2}[A-ILMZ][L-NP-V0-9]{3}[A-Z]$/i,
  1111. 'lv-LV': /^\d{6}-{0,1}\d{5}$/,
  1112. // Conforms both to DG TAXUD spec and original research
  1113. 'mt-MT': /^\d{3,7}[APMGLHBZ]$|^([1-8])\1\d{7}$/i,
  1114. 'nl-NL': /^\d{9}$/,
  1115. 'pl-PL': /^\d{10,11}$/,
  1116. 'pt-BR': /(?:^\d{11}$)|(?:^\d{14}$)/,
  1117. 'pt-PT': /^\d{9}$/,
  1118. 'ro-RO': /^\d{13}$/,
  1119. 'sk-SK': /^\d{6}\/{0,1}\d{3,4}$/,
  1120. 'sl-SI': /^[1-9]\d{7}$/,
  1121. 'sv-SE': /^(\d{6}[-+]{0,1}\d{4}|(18|19|20)\d{6}[-+]{0,1}\d{4})$/
  1122. }; // taxIdFormat locale aliases
  1123. taxIdFormat['lb-LU'] = taxIdFormat['fr-LU'];
  1124. taxIdFormat['lt-LT'] = taxIdFormat['et-EE'];
  1125. taxIdFormat['nl-BE'] = taxIdFormat['fr-BE']; // Algorithmic tax id check functions for various locales
  1126. var taxIdCheck = {
  1127. 'bg-BG': bgBgCheck,
  1128. 'cs-CZ': csCzCheck,
  1129. 'de-AT': deAtCheck,
  1130. 'de-DE': deDeCheck,
  1131. 'dk-DK': dkDkCheck,
  1132. 'el-CY': elCyCheck,
  1133. 'el-GR': elGrCheck,
  1134. 'en-IE': enIeCheck,
  1135. 'en-US': enUsCheck,
  1136. 'es-ES': esEsCheck,
  1137. 'et-EE': etEeCheck,
  1138. 'fi-FI': fiFiCheck,
  1139. 'fr-BE': frBeCheck,
  1140. 'fr-FR': frFrCheck,
  1141. 'fr-LU': frLuCheck,
  1142. 'hr-HR': hrHrCheck,
  1143. 'hu-HU': huHuCheck,
  1144. 'it-IT': itItCheck,
  1145. 'lv-LV': lvLvCheck,
  1146. 'mt-MT': mtMtCheck,
  1147. 'nl-NL': nlNlCheck,
  1148. 'pl-PL': plPlCheck,
  1149. 'pt-BR': ptBrCheck,
  1150. 'pt-PT': ptPtCheck,
  1151. 'ro-RO': roRoCheck,
  1152. 'sk-SK': skSkCheck,
  1153. 'sl-SI': slSiCheck,
  1154. 'sv-SE': svSeCheck
  1155. }; // taxIdCheck locale aliases
  1156. taxIdCheck['lb-LU'] = taxIdCheck['fr-LU'];
  1157. taxIdCheck['lt-LT'] = taxIdCheck['et-EE'];
  1158. taxIdCheck['nl-BE'] = taxIdCheck['fr-BE']; // Regexes for locales where characters should be omitted before checking format
  1159. var allsymbols = /[-\\\/!@#$%\^&\*\(\)\+\=\[\]]+/g;
  1160. var sanitizeRegexes = {
  1161. 'de-AT': allsymbols,
  1162. 'de-DE': /[\/\\]/g,
  1163. 'fr-BE': allsymbols
  1164. }; // sanitizeRegexes locale aliases
  1165. sanitizeRegexes['nl-BE'] = sanitizeRegexes['fr-BE'];
  1166. /*
  1167. * Validator function
  1168. * Return true if the passed string is a valid tax identification number
  1169. * for the specified locale.
  1170. * Throw an error exception if the locale is not supported.
  1171. */
  1172. function isTaxID(str) {
  1173. var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'en-US';
  1174. (0, _assertString.default)(str); // Copy TIN to avoid replacement if sanitized
  1175. var strcopy = str.slice(0);
  1176. if (locale in taxIdFormat) {
  1177. if (locale in sanitizeRegexes) {
  1178. strcopy = strcopy.replace(sanitizeRegexes[locale], '');
  1179. }
  1180. if (!taxIdFormat[locale].test(strcopy)) {
  1181. return false;
  1182. }
  1183. if (locale in taxIdCheck) {
  1184. return taxIdCheck[locale](strcopy);
  1185. } // Fallthrough; not all locales have algorithmic checks
  1186. return true;
  1187. }
  1188. throw new Error("Invalid locale '".concat(locale, "'"));
  1189. }
  1190. module.exports = exports.default;
  1191. module.exports.default = exports.default;