Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(isLuhnValid): Expose isLuhnValid independently from isCreditCard #1974

Merged
merged 2 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ Validator | Description
**isLicensePlate(str [, locale])** | check if string matches the format of a country's license plate.<br/><br/>(locale is one of `['cs-CZ', 'de-DE', 'de-LI', 'fi-FI', 'pt-BR', 'pt-PT', 'sq-AL', 'sv-SE']` or `any`)
**isLocale(str)** | check if the string is a locale
**isLowercase(str)** | check if the string is lowercase.
**isLuhnValid(str)** | check if the string passes the luhn check.
**isMACAddress(str [, options])** | check if the string is a MAC address.<br/><br/>`options` is an object which defaults to `{no_separators: false}`. If `no_separators` is true, the validator will allow MAC addresses without separators. Also, it allows the use of hyphens, spaces or dots e.g '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'. The options also allow a `eui` property to specify if it needs to be validated against EUI-48 or EUI-64. The accepted values of `eui` are: 48, 64.
**isMagnetURI(str)** | check if the string is a [magnet uri format](https://en.wikipedia.org/wiki/Magnet_URI_scheme).
**isMD5(str)** | check if the string is a MD5 hash.<br/><br/>Please note that you can also use the `isHash(str, 'md5')` function. Keep in mind that MD5 has some collision weaknesses compared to other algorithms (e.g., SHA).
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import isBefore from './lib/isBefore';

import isIn from './lib/isIn';

import isLuhnValid from './lib/isLuhnValid';
import isCreditCard from './lib/isCreditCard';
import isIdentityCard from './lib/isIdentityCard';

Expand Down Expand Up @@ -183,6 +184,7 @@ const validator = {
isAfter,
isBefore,
isIn,
isLuhnValid,
isCreditCard,
isIdentityCard,
isEAN,
Expand Down
22 changes: 2 additions & 20 deletions src/lib/isCreditCard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assertString from './util/assertString';
import isLuhnValid from './isLuhnValid';

/* eslint-disable max-len */
const creditCard = /^(?:4[0-9]{12}(?:[0-9]{3,6})?|5[1-5][0-9]{14}|(222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}|6(?:011|5[0-9][0-9])[0-9]{12,15}|3[47][0-9]{13}|3(?:0[0-5]|[68][0-9])[0-9]{11}|(?:2131|1800|35\d{3})\d{11}|6[27][0-9]{14}|^(81[0-9]{14,17}))$/;
Expand All @@ -10,24 +11,5 @@ export default function isCreditCard(str) {
if (!creditCard.test(sanitized)) {
return false;
}
let sum = 0;
let digit;
let tmpNum;
let shouldDouble;
for (let i = sanitized.length - 1; i >= 0; i--) {
digit = sanitized.substring(i, (i + 1));
tmpNum = parseInt(digit, 10);
if (shouldDouble) {
tmpNum *= 2;
if (tmpNum >= 10) {
sum += ((tmpNum % 10) + 1);
} else {
sum += tmpNum;
}
} else {
sum += tmpNum;
}
shouldDouble = !shouldDouble;
}
return !!((sum % 10) === 0 ? sanitized : false);
return isLuhnValid(str);
}
26 changes: 26 additions & 0 deletions src/lib/isLuhnValid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import assertString from './util/assertString';

export default function isLuhnValid(str) {
assertString(str);
const sanitized = str.replace(/[- ]+/g, '');
let sum = 0;
let digit;
let tmpNum;
let shouldDouble;
for (let i = sanitized.length - 1; i >= 0; i--) {
digit = sanitized.substring(i, (i + 1));
tmpNum = parseInt(digit, 10);
if (shouldDouble) {
tmpNum *= 2;
if (tmpNum >= 10) {
sum += ((tmpNum % 10) + 1);
} else {
sum += tmpNum;
}
} else {
sum += tmpNum;
}
shouldDouble = !shouldDouble;
}
return !!((sum % 10) === 0 ? sanitized : false);
}
28 changes: 28 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4931,6 +4931,34 @@ describe('Validators', () => {
});
});

it('should validate luhn numbers', () => {
test({
validator: 'isLuhnValid',
valid: [
'0',
'5421',
'01234567897',
'0123456789012345678906',
'0123456789012345678901234567891',
'123456789012345678906',
'375556917985515',
'36050234196908',
'4716461583322103',
'4716-2210-5188-5662',
'4929 7226 5379 7141',
],
invalid: [
'',
'1',
'5422',
'foo',
'prefix6234917882863855',
'623491788middle2863855',
'6234917882863855suffix',
],
});
});

it('should validate credit cards', () => {
test({
validator: 'isCreditCard',
Expand Down