From c1e656bc568bc2b7ca157585dc78a1eeab192436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Wo=CC=81jcik?= Date: Sat, 2 Oct 2021 11:17:54 +0200 Subject: [PATCH 01/13] Add validation for PESEL --- README.md | 1 + src/index.js | 2 ++ src/lib/isPESEL.js | 53 ++++++++++++++++++++++++++++++++++++++++++++++ test/validators.js | 22 +++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 src/lib/isPESEL.js diff --git a/README.md b/README.md index cb278ff16..01660b2bd 100644 --- a/README.md +++ b/README.md @@ -99,6 +99,7 @@ Validator | Description **isCreditCard(str)** | check if the string is a credit card. **isCurrency(str [, options])** | check if the string is a valid currency amount.

`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.
**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3]. **isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs). +**isPESEL(str)** | **isDate(input [, options])** | Check if the input is a valid date. e.g. [`2002-07-15`, new Date()].

`options` is an object which can contain the keys `format`, `strictMode` and/or `delimiters`

`format` is a string and defaults to `YYYY/MM/DD`.

`strictMode` is a boolean and defaults to `false`. If `strictMode` is set to true, the validator will reject inputs different from `format`.

`delimiters` is an array of allowed date delimiters and defaults to `['/', '-']`. **isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.

`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`

`locale` determine the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa', 'fa-AF', 'fa-IR', 'fr-FR', 'fr-CA', 'hu-HU', 'id-ID', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pl-Pl', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN']`.
**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'. **isDivisibleBy(str, number)** | check if the string is a number that's divisible by another. diff --git a/src/index.js b/src/index.js index a0e8aa63c..4879019a5 100644 --- a/src/index.js +++ b/src/index.js @@ -77,6 +77,7 @@ import isISIN from './lib/isISIN'; import isISBN from './lib/isISBN'; import isISSN from './lib/isISSN'; import isTaxID from './lib/isTaxID'; +import isPESEL from './lib/isPESEL'; import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePhone'; @@ -142,6 +143,7 @@ const validator = { isIBAN, isBIC, isAlpha, + isPESEL, isAlphaLocales, isAlphanumeric, isAlphanumericLocales, diff --git a/src/lib/isPESEL.js b/src/lib/isPESEL.js new file mode 100644 index 000000000..d18992aa2 --- /dev/null +++ b/src/lib/isPESEL.js @@ -0,0 +1,53 @@ +import assertString from './util/assertString'; +import isInt from './isInt'; + +/** + * Key: Position of digit (starts from 1). + * Value: Weight of digit at specified position. + */ + +const weightOfDigits = { + 1: 1, + 2: 3, + 3: 7, + 4: 9, + 5: 1, + 6: 3, + 7: 7, + 8: 9, + 9: 1, + 10: 3, + 11: 1, +}; + +/** + * PESEL is a national identification number used by Polish government. + * It is assigned to every Polish citzen and is unique. + * + * Reference: https://en.wikipedia.org/wiki/PESEL + * + * @param {string} str + * @return {boolean} + */ + +export default function isPESEL(str) { + assertString(str); + + if (str == null || str.length !== 11 || !isInt(str, { allow_leading_zeroes: true })) { + return false; + } + + const digits = str.split(''); + let sum = 0; + + digits.forEach((digit, index) => { + if (index !== 10) { + sum += Number(digit) * weightOfDigits[index + 1]; + } + }); + + const modulo = sum % 10; + const lastDigit = Number(str.charAt(str.length - 1)); + + return (modulo === 0 && lastDigit === 0) || (lastDigit === 10 - modulo); +} diff --git a/test/validators.js b/test/validators.js index 13e066741..2c74f0d60 100644 --- a/test/validators.js +++ b/test/validators.js @@ -946,6 +946,28 @@ describe('Validators', () => { }); }); + it('should validate PESEL', () => { + test({ + validator: 'isPESEL', + valid: [ + '99012229019', + '09210215408', + '20313034701', + '86051575214', + ], + invalid: [ + '5', + '195', + '', + '12345678901', + '99212229019', + '09210215402', + '20313534701', + '86241579214', + ], + }); + }); + it('should validate isIPRange', () => { test({ validator: 'isIPRange', From 5aff87c68081b195e1c4a732bf6f20e6c547bcbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Wo=CC=81jcik?= Date: Sat, 2 Oct 2021 13:25:32 +0200 Subject: [PATCH 02/13] Move PESEL validation to isIdentityCard --- README.md | 2 +- src/lib/isIdentityCard.js | 42 +++++++++++++++++++++++++++++++++++++++ test/validators.js | 19 ++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 01660b2bd..063fa498b 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ Validator | Description **isHexColor(str)** | check if the string is a hexadecimal color. **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).

Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`). **isIBAN(str)** | check if a string is a IBAN (International Bank Account Number). -**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`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.

Defaults to 'any'. +**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`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.

Defaults to 'any'. **isIMEI(str [, options]))** | check if the string is a valid IMEI number. Imei should be of format `###############` or `##-######-######-#`.

`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. **isIn(str, values)** | check if the string is in a array of allowed values. **isInt(str [, options])** | check if the string is an integer.

`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). diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index 64616340b..d0b80b3c6 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -1,6 +1,48 @@ import assertString from './util/assertString'; +import isInt from './isInt'; const validators = { + PL: (str) => { + assertString(str); + + const weightOfDigits = { + 1: 1, + 2: 3, + 3: 7, + 4: 9, + 5: 1, + 6: 3, + 7: 7, + 8: 9, + 9: 1, + 10: 3, + 11: 1, + }; + + if (str != null && str.length === 11 && isInt(str, { allow_leading_zeroes: true })) { + const digits = str.split(''); + let sum = 0; + + digits.forEach((digit, index) => { + // Ignored because handling else is unnecesary + /* istanbul ignore else */ + if (index !== 10) { + sum += Number(digit) * weightOfDigits[index + 1]; + } + }); + + const modulo = sum % 10; + const lastDigit = Number(str.charAt(str.length - 1)); + + // Ignored because handling else is unnecesary + /* istanbul ignore else */ + if ((modulo === 0 && lastDigit === 0) || (lastDigit === 10 - modulo)) { + return true; + } + } else { + return false; + } + }, ES: (str) => { assertString(str); diff --git a/test/validators.js b/test/validators.js index 2c74f0d60..2fdffd3e8 100644 --- a/test/validators.js +++ b/test/validators.js @@ -4694,6 +4694,25 @@ describe('Validators', () => { it('should validate identity cards', () => { const fixtures = [ + { + locale: 'PL', + valid: [ + '99012229019', + '09210215408', + '20313034701', + '86051575214', + ], + invalid: [ + '5', + '195', + '', + '12345678901', + '99212229019', + '09210215402', + '20313534701', + '86241579214', + ], + }, { locale: 'ES', valid: [ From 2735d4ad29a963a1f79afbc0d7db9e0ab5864a87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Wo=CC=81jcik?= Date: Sat, 2 Oct 2021 13:28:34 +0200 Subject: [PATCH 03/13] Fix consistent-return --- src/lib/isIdentityCard.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index d0b80b3c6..adddfeb8e 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -39,9 +39,9 @@ const validators = { if ((modulo === 0 && lastDigit === 0) || (lastDigit === 10 - modulo)) { return true; } - } else { - return false; } + + return false; }, ES: (str) => { assertString(str); From d956dca58577b140bfacc291f084579f2b2a577d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Wo=CC=81jcik?= Date: Sat, 2 Oct 2021 13:31:30 +0200 Subject: [PATCH 04/13] Fix trailing-space --- src/lib/isIdentityCard.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index adddfeb8e..17c49e122 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -40,7 +40,7 @@ const validators = { return true; } } - + return false; }, ES: (str) => { From 9cf595a1fa09ba8266b32a3de9a1cc36ff204ff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Wo=CC=81jcik?= Date: Sat, 2 Oct 2021 13:45:22 +0200 Subject: [PATCH 05/13] Attempt to improve code coverage --- README.md | 1 - src/index.js | 1 - src/lib/isIdentityCard.js | 2 +- src/lib/isPESEL.js | 53 --------------------------------------- test/validators.js | 22 ---------------- 5 files changed, 1 insertion(+), 78 deletions(-) delete mode 100644 src/lib/isPESEL.js diff --git a/README.md b/README.md index 063fa498b..044c8c354 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,6 @@ Validator | Description **isCreditCard(str)** | check if the string is a credit card. **isCurrency(str [, options])** | check if the string is a valid currency amount.

`options` is an object which defaults to `{symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}`.
**Note:** The array `digits_after_decimal` is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3]. **isDataURI(str)** | check if the string is a [data uri format](https://developer.mozilla.org/en-US/docs/Web/HTTP/data_URIs). -**isPESEL(str)** | **isDate(input [, options])** | Check if the input is a valid date. e.g. [`2002-07-15`, new Date()].

`options` is an object which can contain the keys `format`, `strictMode` and/or `delimiters`

`format` is a string and defaults to `YYYY/MM/DD`.

`strictMode` is a boolean and defaults to `false`. If `strictMode` is set to true, the validator will reject inputs different from `format`.

`delimiters` is an array of allowed date delimiters and defaults to `['/', '-']`. **isDecimal(str [, options])** | check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.

`options` is an object which defaults to `{force_decimal: false, decimal_digits: '1,', locale: 'en-US'}`

`locale` determine the decimal separator and is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa', 'fa-AF', 'fa-IR', 'fr-FR', 'fr-CA', 'hu-HU', 'id-ID', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pl-Pl', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN']`.
**Note:** `decimal_digits` is given as a range like '1,3', a specific value like '3' or min like '1,'. **isDivisibleBy(str, number)** | check if the string is a number that's divisible by another. diff --git a/src/index.js b/src/index.js index 4879019a5..ad904d7f9 100644 --- a/src/index.js +++ b/src/index.js @@ -77,7 +77,6 @@ import isISIN from './lib/isISIN'; import isISBN from './lib/isISBN'; import isISSN from './lib/isISSN'; import isTaxID from './lib/isTaxID'; -import isPESEL from './lib/isPESEL'; import isMobilePhone, { locales as isMobilePhoneLocales } from './lib/isMobilePhone'; diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index 17c49e122..0e2392fe3 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -36,7 +36,7 @@ const validators = { // Ignored because handling else is unnecesary /* istanbul ignore else */ - if ((modulo === 0 && lastDigit === 0) || (lastDigit === 10 - modulo)) { + if ((modulo === 0 && lastDigit === 0) || lastDigit === 10 - modulo) { return true; } } diff --git a/src/lib/isPESEL.js b/src/lib/isPESEL.js deleted file mode 100644 index d18992aa2..000000000 --- a/src/lib/isPESEL.js +++ /dev/null @@ -1,53 +0,0 @@ -import assertString from './util/assertString'; -import isInt from './isInt'; - -/** - * Key: Position of digit (starts from 1). - * Value: Weight of digit at specified position. - */ - -const weightOfDigits = { - 1: 1, - 2: 3, - 3: 7, - 4: 9, - 5: 1, - 6: 3, - 7: 7, - 8: 9, - 9: 1, - 10: 3, - 11: 1, -}; - -/** - * PESEL is a national identification number used by Polish government. - * It is assigned to every Polish citzen and is unique. - * - * Reference: https://en.wikipedia.org/wiki/PESEL - * - * @param {string} str - * @return {boolean} - */ - -export default function isPESEL(str) { - assertString(str); - - if (str == null || str.length !== 11 || !isInt(str, { allow_leading_zeroes: true })) { - return false; - } - - const digits = str.split(''); - let sum = 0; - - digits.forEach((digit, index) => { - if (index !== 10) { - sum += Number(digit) * weightOfDigits[index + 1]; - } - }); - - const modulo = sum % 10; - const lastDigit = Number(str.charAt(str.length - 1)); - - return (modulo === 0 && lastDigit === 0) || (lastDigit === 10 - modulo); -} diff --git a/test/validators.js b/test/validators.js index 2fdffd3e8..3fd078a75 100644 --- a/test/validators.js +++ b/test/validators.js @@ -946,28 +946,6 @@ describe('Validators', () => { }); }); - it('should validate PESEL', () => { - test({ - validator: 'isPESEL', - valid: [ - '99012229019', - '09210215408', - '20313034701', - '86051575214', - ], - invalid: [ - '5', - '195', - '', - '12345678901', - '99212229019', - '09210215402', - '20313534701', - '86241579214', - ], - }); - }); - it('should validate isIPRange', () => { test({ validator: 'isIPRange', From d89022cf96235af6bb11917e9a4e26287b5a30b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Wo=CC=81jcik?= Date: Sat, 2 Oct 2021 13:52:02 +0200 Subject: [PATCH 06/13] Handle else in control sum check --- src/index.js | 1 - src/lib/isIdentityCard.js | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index ad904d7f9..a0e8aa63c 100644 --- a/src/index.js +++ b/src/index.js @@ -142,7 +142,6 @@ const validator = { isIBAN, isBIC, isAlpha, - isPESEL, isAlphaLocales, isAlphanumeric, isAlphanumericLocales, diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index 0e2392fe3..8f7e325d7 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -34,11 +34,11 @@ const validators = { const modulo = sum % 10; const lastDigit = Number(str.charAt(str.length - 1)); - // Ignored because handling else is unnecesary - /* istanbul ignore else */ if ((modulo === 0 && lastDigit === 0) || lastDigit === 10 - modulo) { return true; } + + return false; } return false; From bb6474ef5414981aca67ffb607f49d2dd3ae48a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20W=C3=B3jcik?= Date: Sat, 2 Oct 2021 14:04:19 +0200 Subject: [PATCH 07/13] Remove unnecessary return Co-authored-by: Federico Ciardi --- src/lib/isIdentityCard.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index 8f7e325d7..89c4c0cf7 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -38,7 +38,6 @@ const validators = { return true; } - return false; } return false; From 559352412d2a204b9704072ebe61c504a5aa161a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Wo=CC=81jcik?= Date: Sat, 2 Oct 2021 14:29:03 +0200 Subject: [PATCH 08/13] Fix linter error --- src/lib/isIdentityCard.js | 2 -- test/validators.js | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index 8f7e325d7..2ee29293a 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -37,8 +37,6 @@ const validators = { if ((modulo === 0 && lastDigit === 0) || lastDigit === 10 - modulo) { return true; } - - return false; } return false; diff --git a/test/validators.js b/test/validators.js index 3fd078a75..f446e041b 100644 --- a/test/validators.js +++ b/test/validators.js @@ -4681,9 +4681,11 @@ describe('Validators', () => { '86051575214', ], invalid: [ + 'aa', '5', '195', '', + ' ', '12345678901', '99212229019', '09210215402', From 87ffd17d5af650478ba99fff29796e3dab860c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Wo=CC=81jcik?= Date: Sat, 2 Oct 2021 14:52:42 +0200 Subject: [PATCH 09/13] Add missing tests --- test/validators.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/validators.js b/test/validators.js index f446e041b..60fd96576 100644 --- a/test/validators.js +++ b/test/validators.js @@ -4679,6 +4679,9 @@ describe('Validators', () => { '09210215408', '20313034701', '86051575214', + '77334586883', + '06566860643', + '77552478861', ], invalid: [ 'aa', From 247b02836237d06be2c272c5501b0775a659ee8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Wo=CC=81jcik?= Date: Sat, 2 Oct 2021 15:07:03 +0200 Subject: [PATCH 10/13] Add missing test --- test/validators.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/validators.js b/test/validators.js index 60fd96576..286eb6924 100644 --- a/test/validators.js +++ b/test/validators.js @@ -4680,6 +4680,7 @@ describe('Validators', () => { '20313034701', '86051575214', '77334586883', + '54007481320', '06566860643', '77552478861', ], From e1b64aaa7cccd6175abe4e99bc724de6160a6098 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20W=C3=B3jcik?= Date: Sat, 2 Oct 2021 16:38:47 +0200 Subject: [PATCH 11/13] Replace Array.forEach with Array. reduce Co-authored-by: Sarhan Aissi --- src/lib/isIdentityCard.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index 2ee29293a..feb0a42d3 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -20,16 +20,9 @@ const validators = { }; if (str != null && str.length === 11 && isInt(str, { allow_leading_zeroes: true })) { - const digits = str.split(''); - let sum = 0; - - digits.forEach((digit, index) => { - // Ignored because handling else is unnecesary - /* istanbul ignore else */ - if (index !== 10) { - sum += Number(digit) * weightOfDigits[index + 1]; - } - }); + const digits = str.split('').slice(0, -1); + const sum = digits.reduce((acc, digit, index) => + acc + (Number(digit) * weightOfDigits[index + 1]), 0); const modulo = sum % 10; const lastDigit = Number(str.charAt(str.length - 1)); From 9206a10a447f1e9b61da2ecbf56e93633db9364d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Wo=CC=81jcik?= Date: Sat, 2 Oct 2021 17:03:19 +0200 Subject: [PATCH 12/13] Use reduce instead forEach --- src/lib/isIdentityCard.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index feb0a42d3..7245ecb4a 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -21,8 +21,13 @@ const validators = { if (str != null && str.length === 11 && isInt(str, { allow_leading_zeroes: true })) { const digits = str.split('').slice(0, -1); - const sum = digits.reduce((acc, digit, index) => - acc + (Number(digit) * weightOfDigits[index + 1]), 0); + const sum = digits.reduce((acc, digit, index) => { + if (index !== 10) { + acc += (Number(digit) * weightOfDigits[index + 1]); + } + + return acc; + }, 0); const modulo = sum % 10; const lastDigit = Number(str.charAt(str.length - 1)); From ec4801d6d2f553c61f88bf6b2b5c3e5f9e6722d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wiktor=20Wo=CC=81jcik?= Date: Sat, 2 Oct 2021 17:08:07 +0200 Subject: [PATCH 13/13] Simplify reduce function --- src/lib/isIdentityCard.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/lib/isIdentityCard.js b/src/lib/isIdentityCard.js index 7245ecb4a..8ea15e262 100644 --- a/src/lib/isIdentityCard.js +++ b/src/lib/isIdentityCard.js @@ -16,18 +16,13 @@ const validators = { 8: 9, 9: 1, 10: 3, - 11: 1, + 11: 0, }; if (str != null && str.length === 11 && isInt(str, { allow_leading_zeroes: true })) { const digits = str.split('').slice(0, -1); - const sum = digits.reduce((acc, digit, index) => { - if (index !== 10) { - acc += (Number(digit) * weightOfDigits[index + 1]); - } - - return acc; - }, 0); + const sum = digits.reduce((acc, digit, index) => + acc + (Number(digit) * weightOfDigits[index + 1]), 0); const modulo = sum % 10; const lastDigit = Number(str.charAt(str.length - 1));