Created
November 30, 2022 11:12
-
-
Save max-lt/748d81989545a2362796a022d02af5df to your computer and use it in GitHub Desktop.
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
import { AbstractControl, ValidationErrors } from '@angular/forms'; | |
const luhnArr = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]; | |
// https://portal.hardis-group.com/pages/viewpage.action?pageId=120357227 | |
export function checkSIRET(control: AbstractControl): ValidationErrors { | |
if (!control.value) { | |
return null; | |
} | |
const siret = (control.value as string).replace(/\s/g, '') as string; | |
if (!/^\d{14}$/.test(siret)) { | |
return { invalidFormat: { siret } }; | |
} | |
const numbers = siret.split('').map((v) => parseInt(v)); | |
const checksum = numbers.reduce((a, e, i) => a + ((i & 1) ? e : luhnArr[e]), 0); | |
if (checksum % 10) { | |
return { invalidChecksum: { siret } }; | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment