Skip to content

Commit

Permalink
feat(isIdentityCard): add zh-TW locale (#1040)
Browse files Browse the repository at this point in the history
* Add zh-TW National ID Rule

* Add zh-TW ID rule on README.md

* fit eslint rules

* add built code

* add zh-TW id card number test

* fixed test on zh-TW id card numbers
  • Loading branch information
fantasywind authored and profnandaa committed Jun 18, 2019
1 parent 81302a7 commit d7dbdd5
Show file tree
Hide file tree
Showing 6 changed files with 166 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']` 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']` 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
44 changes: 44 additions & 0 deletions lib/isIdentityCard.js
Expand Up @@ -31,6 +31,50 @@ var validators = {
return charsValue[char];
});
return sanitized.endsWith(controlDigits[number % 23]);
},
'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);
}
};

Expand Down
48 changes: 48 additions & 0 deletions src/lib/isIdentityCard.js
Expand Up @@ -30,6 +30,54 @@ const validators = {

return sanitized.endsWith(controlDigits[number % 23]);
},
'zh-TW': (str) => {
const 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,
};

const sanitized = str.trim().toUpperCase();

if (!/^[A-Z][0-9]{9}$/.test(sanitized)) return false;

return Array.from(sanitized).reduce((sum, number, index) => {
if (index === 0) {
const 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 = 'any') {
Expand Down
28 changes: 28 additions & 0 deletions test/validators.js
Expand Up @@ -2981,6 +2981,34 @@ describe('Validators', () => {
'Z1234567C',
],
},
{
locale: 'zh-TW',
valid: [
'B176944193',
'K101189797',
'F112866121',
'A219758834',
'A244144802',
'A146047171',
'Q170219004',
'Z277018381',
'X231071923',
],
invalid: [
'123456789',
'A185034995',
'X431071923',
'GE9800as98',
'X231071922',
'1234*678Z',
'12345678!',
'1234567L',
'A1234567L',
'X1234567A',
'Y1234567B',
'Z1234567C',
],
},
];

let allValid = [];
Expand Down
44 changes: 44 additions & 0 deletions validator.js
Expand Up @@ -1133,6 +1133,50 @@ var validators = {
return charsValue[_char];
});
return sanitized.endsWith(controlDigits[number % 23]);
},
'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);
}
};
function isIdentityCard(str) {
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit d7dbdd5

Please sign in to comment.