Skip to content

Instantly share code, notes, and snippets.

@max-lt
Created November 30, 2022 11:12
Show Gist options
  • Save max-lt/748d81989545a2362796a022d02af5df to your computer and use it in GitHub Desktop.
Save max-lt/748d81989545a2362796a022d02af5df to your computer and use it in GitHub Desktop.
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