Skip to content

Commit

Permalink
Merge pull request #73 from koblas/fr_siret
Browse files Browse the repository at this point in the history
fix: fr/siret handle La Poste special case
  • Loading branch information
koblas committed Jul 4, 2023
2 parents dbfef33 + 4183e5c commit a1875fc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/fr/siret.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,30 @@ describe('fr/siret', () => {
expect(result).toEqual('732 829 320 00074');
});

it('validate:73282932000074', () => {
const result = validate('73282932000074');

expect(result.isValid && result.compact).toEqual('73282932000074');
test.each([
'73282932000074',
'356 000 000 09075',
'35600000000048',
'35600000049837',
])('validate:%s', value => {
const result = validate(value);

expect(result.isValid).toEqual(true);
});
// it('validate:73282932000074', () => {
// const result = validate('73282932000074');

// expect(result.isValid && result.compact).toEqual('73282932000074');
// });

it('validate:12345678', () => {
const result = validate('12345678');

expect(result.error).toBeInstanceOf(InvalidLength);
});

it('validate:73282932000075', () => {
const result = validate('73282932000075');
test.each(['35600000049838', '73282932000075'])('validate:%s', value => {
const result = validate(value);

expect(result.error).toBeInstanceOf(InvalidChecksum);
});
Expand Down
19 changes: 16 additions & 3 deletions src/fr/siret.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
*
* The SIRET (Système d'Identification du Répertoire des Établissements)
* is a 14 digit number used to identify French companies' establishments
* and facilities. The Luhn checksum is used to validate the numbers.
* and facilities. The Luhn checksum is used to validate the numbers (except
* for La Poste).
*
* Sources:
* https://fr.wikipedia.org/wiki/Système_d'identification_du_répertoire_des_établissements
*
* ENTITY
*/

import * as exceptions from '../exceptions';
import { strings } from '../util';
import { Validator, ValidateReturn } from '../types';
import { luhnChecksumValidate } from '../util/checksum';
import { luhnChecksumValidate, weightedSum } from '../util/checksum';

function clean(input: string): ReturnType<typeof strings.cleanUnicode> {
return strings.cleanUnicode(input, ' -/');
Expand Down Expand Up @@ -50,7 +54,16 @@ const impl: Validator = {
return { isValid: false, error: new exceptions.InvalidFormat() };
}

if (!luhnChecksumValidate(value)) {
if (value.startsWith('356000000') && value !== '35600000000048') {
const sum = weightedSum(value, {
weights: [1],
modulus: 5,
});
console.log('HERE', value, sum);
if (sum !== 0) {
return { isValid: false, error: new exceptions.InvalidChecksum() };
}
} else if (!luhnChecksumValidate(value)) {
return { isValid: false, error: new exceptions.InvalidChecksum() };
}

Expand Down

0 comments on commit a1875fc

Please sign in to comment.