Skip to content

Commit

Permalink
feat(isIdentityCard): add he-IL locale (#1041)
Browse files Browse the repository at this point in the history
* added support for IL in isIdentityCard()

* fix ci

* fix CI #2

* fix CI #3

* fix conflicts

* chnage locale to 'he-IL'
  • Loading branch information
perimiter authored and profnandaa committed Jun 18, 2019
1 parent 9ee09a7 commit 7735e0f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -100,7 +100,7 @@ Validator | Description
**isHash(str, algorithm)** | check if the string is a hash of type algorithm.<br/><br/>Algorithm is one of `['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']`
**isHexColor(str)** | check if the string is a hexadecimal color.
**isHexadecimal(str)** | check if the string is a hexadecimal number.
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES', 'zh-TW']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES', 'zh-TW', 'he-IL']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
**isIP(str [, version])** | check if the string is an IP (version 4 or 6).
**isIPRange(str)** | check if the string is an IP Range(version 4 only).
**isISBN(str [, version])** | check if the string is an ISBN (version 10 or 13).
Expand Down
27 changes: 27 additions & 0 deletions lib/isIdentityCard.js
Expand Up @@ -32,6 +32,33 @@ var validators = {
});
return sanitized.endsWith(controlDigits[number % 23]);
},
'he-IL': function heIL(str) {
var DNI = /^\d+$/; // sanitize user input

var sanitized = str.trim(); // validate the data structure

if (!DNI.test(sanitized)) {
return false;
}

var id = sanitized;

if (id.length !== 9 || isNaN(id)) {
// Make sure ID is formatted properly
return false;
}

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,
Expand Down
25 changes: 25 additions & 0 deletions src/lib/isIdentityCard.js
Expand Up @@ -30,6 +30,31 @@ const validators = {

return sanitized.endsWith(controlDigits[number % 23]);
},
'he-IL': (str) => {
const DNI = /^\d+$/;

// sanitize user input
const sanitized = str.trim();

// validate the data structure
if (!DNI.test(sanitized)) {
return false;
}

const id = sanitized;

if (id.length !== 9 || isNaN(id)) {
// Make sure ID is formatted properly
return false;
}
let sum = 0,
incNum;
for (let 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': (str) => {
const ALPHABET_CODES = {
A: 10,
Expand Down
39 changes: 39 additions & 0 deletions test/validators.js
Expand Up @@ -3062,6 +3062,41 @@ describe('Validators', () => {
'Z1234567C',
],
},
{
locale: 'he-IL',
valid: [
'219472156',
'219486610',
'219488962',
'219566726',
'219640216',
'219645041',
'334795465',
'335211686',
'335240479',
'335472171',
'336999842',
'337090443',
],
invalid: [
'123456789',
'12345678A',
'12345 678Z',
'12345678-Z',
'1234*6789',
'1234*678Z',
'12345678!',
'1234567L',
'A1234567L',
'X1234567A',
'Y1234567B',
'Z1234567C',
'219772156',
'219487710',
'334705465',
'336000842',
],
},
{
locale: 'zh-TW',
valid: [
Expand All @@ -3088,6 +3123,10 @@ describe('Validators', () => {
'X1234567A',
'Y1234567B',
'Z1234567C',
'219772156',
'219487710',
'334705465',
'336000842',
],
},
];
Expand Down
27 changes: 27 additions & 0 deletions validator.js
Expand Up @@ -1136,6 +1136,33 @@ var validators = {
});
return sanitized.endsWith(controlDigits[number % 23]);
},
'he-IL': function heIL(str) {
var DNI = /^\d+$/; // sanitize user input

var sanitized = str.trim(); // validate the data structure

if (!DNI.test(sanitized)) {
return false;
}

var id = sanitized;

if (id.length !== 9 || isNaN(id)) {
// Make sure ID is formatted properly
return false;
}

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,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 7735e0f

Please sign in to comment.