Skip to content

Commit

Permalink
fix: localisation cases when values are zeros (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
abelkhay authored May 9, 2023
1 parent f68f393 commit d133282
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/transformer/fields/localisation/localisation.field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,14 @@ describe('localisation field', (): void => {

expect(localisation).toStrictEqual<Localisation>(NO_LOCALISATION);
});

it('should return null when coordinates have 0 as value', (): void => {
const source: DataSource = {
bf_latitude: '0',
bf_longitude: '0'
};
const localisation: Localisation = processLocalisation(source, STANDARD_MATCHING);

expect(localisation).toStrictEqual<Localisation>(NO_LOCALISATION);
});
});
12 changes: 10 additions & 2 deletions src/transformer/fields/localisation/localisation.field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type NoLocalisation = { noLocalisation: true } & null;
export const NO_LOCALISATION: Localisation = null as NoLocalisation;

const NO_LOCALISATION_COLONNE: string = '';
const LOCALISATION_IS_ZERO_VALUES: number = NaN;
const INVALID_NUMBERS_CHARS: RegExp = /[^\d.\s,-]+/gu;

const isColonne = (colonneToTest: Partial<Colonne> & Partial<Dissociation>): colonneToTest is Colonne =>
Expand All @@ -33,8 +34,15 @@ const checkFormatLocalisation = (localisation: LocalisationToValidate): Localisa
const localisationField = (source: DataSource, localisation: Dissociation & Partial<Colonne>): string | undefined =>
(isColonne(localisation) ? source[localisation.colonne] : dissocier(source, localisation))?.toString().replace(',', '.');

const validateLocalisationField = (localisationToValidate: LocalisationToValidate): Localisation =>
isValidLocalisation(localisationToValidate) ? localisationToValidate : checkFormatLocalisation(localisationToValidate);
const validateLocalisationField = (localisationToValidate: LocalisationToValidate): Localisation => {
const localisationToValidateProbablyFalse: LocalisationToValidate =
localisationToValidate.latitude === 0 || localisationToValidate.longitude === 0
? { latitude: LOCALISATION_IS_ZERO_VALUES, longitude: LOCALISATION_IS_ZERO_VALUES }
: localisationToValidate;
return isValidLocalisation(localisationToValidateProbablyFalse)
? localisationToValidateProbablyFalse
: checkFormatLocalisation(localisationToValidateProbablyFalse);
};

const localisationFromMatching = (source: DataSource, matching: LieuxMediationNumeriqueMatching): LocalisationToValidate => ({
latitude: parseFloat(localisationField(source, matching.latitude) ?? NO_LOCALISATION_COLONNE),
Expand Down

0 comments on commit d133282

Please sign in to comment.