Created
February 20, 2019 23:16
-
-
Save edwingustafson/9915e458079c808f0251b98ca540704f to your computer and use it in GitHub Desktop.
Luhn check as a single Typescript expression
This file contains hidden or 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
const luhn: (candidate: string) => boolean = (candidate: string) => | |
parseInt(candidate[candidate.length - 1]) === candidate. | |
slice(0, candidate.length - 1). | |
split(''). | |
map((c: string) => parseInt(c)). | |
reverse(). | |
map((n: number, index: number) => index % 2 === 0 ? 2 * n : n). | |
map((n: number) => n > 9 ? n - 9 : n). | |
reduce((a: number, b: number) => a + b) | |
* 9 % 10 | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment