Skip to content

Commit f2a1587

Browse files
wiktorwojcik112fedecitux-tn
authored
feat(isIdentityCard): Add PL locale (#1745)
* Add validation for PESEL * Move PESEL validation to isIdentityCard * Fix consistent-return * Fix trailing-space * Attempt to improve code coverage * Handle else in control sum check * Remove unnecessary return Co-authored-by: Federico Ciardi <[email protected]> * Fix linter error * Add missing tests * Add missing test * Replace Array.forEach with Array. reduce Co-authored-by: Sarhan Aissi <[email protected]> * Use reduce instead forEach * Simplify reduce function Co-authored-by: Federico Ciardi <[email protected]> Co-authored-by: Sarhan Aissi <[email protected]>
1 parent 04b73ad commit f2a1587

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ Validator | Description
115115
**isHexColor(str)** | check if the string is a hexadecimal color.
116116
**isHSL(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification](https://developer.mozilla.org/en-US/docs/Web/CSS/color_value).<br/><br/>Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`).
117117
**isIBAN(str)** | check if a string is a IBAN (International Bank Account Number).
118-
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES', 'IN', 'IT', 'IR', 'MZ', 'NO', 'TH', 'zh-TW', 'he-IL', 'ar-LY', 'ar-TN', 'zh-CN']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
118+
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['PL', 'ES', 'IN', 'IT', 'IR', 'MZ', 'NO', 'TH', 'zh-TW', 'he-IL', 'ar-LY', 'ar-TN', 'zh-CN']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
119119
**isIMEI(str [, options]))** | check if the string is a valid IMEI number. Imei should be of format `###############` or `##-######-######-#`.<br/><br/>`options` is an object which can contain the keys `allow_hyphens`. Defaults to first format . If allow_hyphens is set to true, the validator will validate the second format.
120120
**isIn(str, values)** | check if the string is in a array of allowed values.
121121
**isInt(str [, options])** | check if the string is an integer.<br/><br/>`options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4).

src/lib/isIdentityCard.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
11
import assertString from './util/assertString';
2+
import isInt from './isInt';
23

34
const validators = {
5+
PL: (str) => {
6+
assertString(str);
7+
8+
const weightOfDigits = {
9+
1: 1,
10+
2: 3,
11+
3: 7,
12+
4: 9,
13+
5: 1,
14+
6: 3,
15+
7: 7,
16+
8: 9,
17+
9: 1,
18+
10: 3,
19+
11: 0,
20+
};
21+
22+
if (str != null && str.length === 11 && isInt(str, { allow_leading_zeroes: true })) {
23+
const digits = str.split('').slice(0, -1);
24+
const sum = digits.reduce((acc, digit, index) =>
25+
acc + (Number(digit) * weightOfDigits[index + 1]), 0);
26+
27+
const modulo = sum % 10;
28+
const lastDigit = Number(str.charAt(str.length - 1));
29+
30+
if ((modulo === 0 && lastDigit === 0) || lastDigit === 10 - modulo) {
31+
return true;
32+
}
33+
}
34+
35+
return false;
36+
},
437
ES: (str) => {
538
assertString(str);
639

test/validators.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4686,6 +4686,31 @@ describe('Validators', () => {
46864686

46874687
it('should validate identity cards', () => {
46884688
const fixtures = [
4689+
{
4690+
locale: 'PL',
4691+
valid: [
4692+
'99012229019',
4693+
'09210215408',
4694+
'20313034701',
4695+
'86051575214',
4696+
'77334586883',
4697+
'54007481320',
4698+
'06566860643',
4699+
'77552478861',
4700+
],
4701+
invalid: [
4702+
'aa',
4703+
'5',
4704+
'195',
4705+
'',
4706+
' ',
4707+
'12345678901',
4708+
'99212229019',
4709+
'09210215402',
4710+
'20313534701',
4711+
'86241579214',
4712+
],
4713+
},
46894714
{
46904715
locale: 'ES',
46914716
valid: [

0 commit comments

Comments
 (0)