Skip to content

Commit e277359

Browse files
committed
fix(luhn): empty input should return false
1 parent 2026120 commit e277359

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/luhn.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export function luhn(input: number | bigint | string): boolean {
22
const value = typeof input === 'string' ? input : String(input);
33

4-
// input contained non digit characters.
5-
if (/[^\d]/.test(value)) {
4+
// value is empty or contained non digit characters.
5+
if (!value.length || /\D/.test(value)) {
66
return false;
77
}
88

test/luhn.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ describe('luhn()', () => {
1818
expect(luhn(4000000000001001)).toBeFalsy();
1919
});
2020

21-
it.each(['not a credit card number', '-1', Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY])(
22-
'returns false when giving bad input: %s',
21+
it.each(['', ' ', 'not a credit card number', '-1', Number.POSITIVE_INFINITY, Number.NEGATIVE_INFINITY])(
22+
'returns false when giving bad input: "%s"',
2323
(input) => {
2424
expect(luhn(input)).toBeFalsy();
2525
},

0 commit comments

Comments
 (0)