1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = isIdentityCard;
|
7 |
|
8 | var _assertString = _interopRequireDefault(require("./util/assertString"));
|
9 |
|
10 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
11 |
|
12 | var 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'];
|
22 |
|
23 | var sanitized = str.trim().toUpperCase();
|
24 |
|
25 | if (!DNI.test(sanitized)) {
|
26 | return false;
|
27 | }
|
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 |
|
37 | function 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 |
|
60 | module.exports = exports.default; |
\ | No newline at end of file |