stdnum
Version:
Standard Number Validation
69 lines (55 loc) • 1.71 kB
text/typescript
/**
* SIREN (a French company identification number).
*
* The SIREN (Système d'Identification du Répertoire des Entreprises) is a 9
* digit number used to identify French companies. The Luhn checksum is used
* to validate the numbers.
*
* ENTITY
*/
import * as exceptions from '../exceptions';
import { strings } from '../util';
import { Validator, ValidateReturn } from '../types';
import { luhnChecksumValidate } from '../util/checksum';
function clean(input: string): ReturnType<typeof strings.cleanUnicode> {
return strings.cleanUnicode(input, ' .');
}
const impl: Validator = {
name: 'French Company Identification Number',
localName: "Système d'Identification du Répertoire des Entreprises",
abbreviation: 'SIREN',
compact(input: string): string {
const [value, err] = clean(input);
if (err) {
throw err;
}
return value;
},
format(input: string): string {
const [value] = clean(input);
return strings.splitAt(value, 3, 6).join(' ');
},
validate(input: string): ValidateReturn {
const [value, error] = clean(input);
if (error) {
return { isValid: false, error };
}
if (value.length !== 9) {
return { isValid: false, error: new exceptions.InvalidLength() };
}
if (!strings.isdigits(value)) {
return { isValid: false, error: new exceptions.InvalidFormat() };
}
if (!luhnChecksumValidate(value)) {
return { isValid: false, error: new exceptions.InvalidChecksum() };
}
return {
isValid: true,
compact: value,
isIndividual: false,
isCompany: true,
};
},
};
export const { name, localName, abbreviation, validate, format, compact } =
impl;