summaryrefslogtreecommitdiff
path: root/src/node_modules/validator/es/lib/isIdentityCard.js
diff options
context:
space:
mode:
authorMinteck <contact@minteck.org>2021-12-21 16:52:28 +0100
committerMinteck <contact@minteck.org>2021-12-21 16:52:28 +0100
commit46e43f4bde4a35785b4997b81e86cd19f046b69b (patch)
treec53c2f826f777f9d6b2d249dab556feb72a6c3a6 /src/node_modules/validator/es/lib/isIdentityCard.js
downloadlangdetect-46e43f4bde4a35785b4997b81e86cd19f046b69b.tar.gz
langdetect-46e43f4bde4a35785b4997b81e86cd19f046b69b.tar.bz2
langdetect-46e43f4bde4a35785b4997b81e86cd19f046b69b.zip
Commit
Diffstat (limited to 'src/node_modules/validator/es/lib/isIdentityCard.js')
-rw-r--r--src/node_modules/validator/es/lib/isIdentityCard.js113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/node_modules/validator/es/lib/isIdentityCard.js b/src/node_modules/validator/es/lib/isIdentityCard.js
new file mode 100644
index 0000000..44a416b
--- /dev/null
+++ b/src/node_modules/validator/es/lib/isIdentityCard.js
@@ -0,0 +1,113 @@
+import assertString from './util/assertString';
+var validators = {
+ ES: function ES(str) {
+ assertString(str);
+ var DNI = /^[0-9X-Z][0-9]{7}[TRWAGMYFPDXBNJZSQVHLCKE]$/;
+ var charsValue = {
+ X: 0,
+ Y: 1,
+ Z: 2
+ };
+ 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
+
+ var sanitized = str.trim().toUpperCase(); // validate the data structure
+
+ if (!DNI.test(sanitized)) {
+ return false;
+ } // validate the control digit
+
+
+ var number = sanitized.slice(0, -1).replace(/[X,Y,Z]/g, function (_char) {
+ return charsValue[_char];
+ });
+ return sanitized.endsWith(controlDigits[number % 23]);
+ },
+ 'he-IL': function heIL(str) {
+ var DNI = /^\d{9}$/; // sanitize user input
+
+ var sanitized = str.trim(); // validate the data structure
+
+ if (!DNI.test(sanitized)) {
+ return false;
+ }
+
+ var id = sanitized;
+ var sum = 0,
+ incNum;
+
+ for (var i = 0; i < id.length; i++) {
+ incNum = Number(id[i]) * (i % 2 + 1); // Multiply number by 1 or 2
+
+ sum += incNum > 9 ? incNum - 9 : incNum; // Sum the digits up and add to total
+ }
+
+ return sum % 10 === 0;
+ },
+ 'zh-TW': function zhTW(str) {
+ var ALPHABET_CODES = {
+ A: 10,
+ B: 11,
+ C: 12,
+ D: 13,
+ E: 14,
+ F: 15,
+ G: 16,
+ H: 17,
+ I: 34,
+ J: 18,
+ K: 19,
+ L: 20,
+ M: 21,
+ N: 22,
+ O: 35,
+ P: 23,
+ Q: 24,
+ R: 25,
+ S: 26,
+ T: 27,
+ U: 28,
+ V: 29,
+ W: 32,
+ X: 30,
+ Y: 31,
+ Z: 33
+ };
+ var sanitized = str.trim().toUpperCase();
+ if (!/^[A-Z][0-9]{9}$/.test(sanitized)) return false;
+ return Array.from(sanitized).reduce(function (sum, number, index) {
+ if (index === 0) {
+ var code = ALPHABET_CODES[number];
+ return code % 10 * 9 + Math.floor(code / 10);
+ }
+
+ if (index === 9) {
+ return (10 - sum % 10 - Number(number)) % 10 === 0;
+ }
+
+ return sum + Number(number) * (9 - index);
+ }, 0);
+ }
+};
+export default function isIdentityCard(str, locale) {
+ assertString(str);
+
+ if (locale in validators) {
+ return validators[locale](str);
+ } else if (locale === 'any') {
+ for (var key in validators) {
+ // https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
+ // istanbul ignore else
+ if (validators.hasOwnProperty(key)) {
+ var validator = validators[key];
+
+ if (validator(str)) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+
+ throw new Error("Invalid locale '".concat(locale, "'"));
+} \ No newline at end of file