Skip to content

Commit

Permalink
fix(luhn): empty input should return false
Browse files Browse the repository at this point in the history
  • Loading branch information
webdeveric committed Dec 18, 2024
1 parent 2026120 commit e277359
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/luhn.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export function luhn(input: number | bigint | string): boolean {
const value = typeof input === 'string' ? input : String(input);

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

Expand Down
4 changes: 2 additions & 2 deletions test/luhn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ describe('luhn()', () => {
expect(luhn(4000000000001001)).toBeFalsy();
});

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

0 comments on commit e277359

Please sign in to comment.