UNPKG

1.58 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = isIdentityCard;
7
8var _assertString = _interopRequireDefault(require("./util/assertString"));
9
10function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
12var validators = {
13 ES: function ES(str) {
14 (0, _assertString.default)(str);
15 var DNI = /^[0-9X-Z][0-9]{7}[TRWAGMYFPDXBNJZSQVHLCKE]$/;
16 var charsValue = {
17 X: 0,
18 Y: 1,
19 Z: 2
20 };
21 var controlDigits = ['T', 'R', 'W', 'A', 'G', 'M', 'Y', 'F', 'P', 'D', 'X', 'B', 'N', 'J', 'Z', 'S', 'Q', 'V', 'H', 'L', 'C', 'K', 'E']; // sanitize user input
22
23 var sanitized = str.trim().toUpperCase(); // validate the data structure
24
25 if (!DNI.test(sanitized)) {
26 return false;
27 } // validate the control digit
28
29
30 var number = sanitized.slice(0, -1).replace(/[X,Y,Z]/g, function (char) {
31 return charsValue[char];
32 });
33 return sanitized.endsWith(controlDigits[number % 23]);
34 }
35};
36
37function isIdentityCard(str) {
38 var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'any';
39 (0, _assertString.default)(str);
40
41 if (locale in validators) {
42 return validators[locale](str);
43 } else if (locale === 'any') {
44 for (var key in validators) {
45 if (validators.hasOwnProperty(key)) {
46 var validator = validators[key];
47
48 if (validator(str)) {
49 return true;
50 }
51 }
52 }
53
54 return false;
55 }
56
57 throw new Error("Invalid locale '".concat(locale, "'"));
58}
59
60module.exports = exports.default;
\No newline at end of file