Skip to content

Instantly share code, notes, and snippets.

@edwingustafson
Created February 20, 2019 23:16
Show Gist options
  • Save edwingustafson/9915e458079c808f0251b98ca540704f to your computer and use it in GitHub Desktop.
Save edwingustafson/9915e458079c808f0251b98ca540704f to your computer and use it in GitHub Desktop.
Luhn check as a single Typescript expression
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