-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #846 from hitbits/master
Added isIdentityCard for identity card codes validation
- Loading branch information
Showing
8 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = isIdentityCard; | ||
|
||
var _assertString = require('./util/assertString'); | ||
|
||
var _assertString2 = _interopRequireDefault(_assertString); | ||
|
||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
|
||
var validators = { | ||
ES: function ES(str) { | ||
(0, _assertString2.default)(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]); | ||
} | ||
}; | ||
|
||
function isIdentityCard(str) { | ||
var locale = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'any'; | ||
|
||
(0, _assertString2.default)(str); | ||
if (locale in validators) { | ||
return validators[locale](str); | ||
} else if (locale === 'any') { | ||
for (var key in validators) { | ||
if (validators.hasOwnProperty(key)) { | ||
var validator = validators[key]; | ||
if (validator(str)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
throw new Error('Invalid locale \'' + locale + '\''); | ||
} | ||
module.exports = exports['default']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import assertString from './util/assertString'; | ||
|
||
const validators = { | ||
ES: (str) => { | ||
assertString(str); | ||
|
||
const DNI = /^[0-9X-Z][0-9]{7}[TRWAGMYFPDXBNJZSQVHLCKE]$/; | ||
|
||
const charsValue = { | ||
X: 0, | ||
Y: 1, | ||
Z: 2, | ||
}; | ||
|
||
const 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 | ||
const sanitized = str.trim().toUpperCase(); | ||
|
||
// validate the data structure | ||
if (!DNI.test(sanitized)) { | ||
return false; | ||
} | ||
|
||
// validate the control digit | ||
const number = sanitized.slice(0, -1).replace(/[X,Y,Z]/g, char => charsValue[char]); | ||
|
||
return sanitized.endsWith(controlDigits[number % 23]); | ||
}, | ||
}; | ||
|
||
export default function isIdentityCard(str, locale = 'any') { | ||
assertString(str); | ||
if (locale in validators) { | ||
return validators[locale](str); | ||
} else if (locale === 'any') { | ||
for (const key in validators) { | ||
if (validators.hasOwnProperty(key)) { | ||
const validator = validators[key]; | ||
if (validator(str)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
throw new Error(`Invalid locale '${locale}'`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.