-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ad8567
commit 76f6c16
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Luhn } from '../src/luhn'; | ||
|
||
describe('Luhn', () => { | ||
const tests: [string, boolean][] = [ | ||
['378282246310005', true], | ||
['371449635398431', true], | ||
['378734493671000', true], | ||
['5610591081018250', true], | ||
['30569309025904', true], | ||
['38520000023237', true], | ||
['6011111111111117', true], | ||
['6011000990139424', true], | ||
['3530111333300000', true], | ||
['3566002020360505', true], | ||
['5555555555554444', true], | ||
['5105105105105100', true], | ||
['4111111111111111', true], | ||
['4012888888881881', true], | ||
['4222222222222', true], | ||
['5019717010103742', true], | ||
['6331101999990016', true], | ||
|
||
['378282246310006', false], | ||
['371449635398432', false], | ||
['378734493671001', false], | ||
['5610591081018251', false], | ||
['30569309025905', false], | ||
['38520000023238', false], | ||
['6011111111111118', false], | ||
['6011000990139425', false], | ||
['3530111333300001', false], | ||
['3566002020360506', false], | ||
['5555555555554445', false], | ||
['5105105105105101', false], | ||
['4111111111111112', false], | ||
['4012888888881882', false], | ||
['4222222222223', false], | ||
['5019717010103743', false], | ||
['6331101999990017', false], | ||
]; | ||
test.each(tests)(`Check %s => %s`, (value, expected) => { | ||
expect(Luhn.validate(value)).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export class Luhn { | ||
static validate(value: string): boolean { | ||
const digits = [...value.replace(/\s/g, '')].reverse(); | ||
|
||
const sum = digits | ||
.map((digit: number | string, index) => { | ||
digit = parseInt(digit as string, 10) * (index % 2 !== 0 ? 2 : 1); | ||
return digit > 9 ? digit - 9 : digit; | ||
}) | ||
.reduce((prev, current) => { | ||
return prev + current; | ||
}); | ||
|
||
return sum > 0 && sum % 10 === 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters