@gouvfr-anct/mednum
Version:
✨ Permet de transformer une source de données vers le schéma des lieux de mediation numerique
44 lines (43 loc) • 2.94 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.processLocalisation = exports.NO_LOCALISATION = void 0;
const lieux_de_mediation_numerique_1 = require("@gouvfr-anct/lieux-de-mediation-numerique");
const proj4_1 = __importDefault(require("proj4"));
exports.NO_LOCALISATION = null;
const NO_LOCALISATION_COLONNE = '';
const LOCALISATION_IS_ZERO_VALUES = NaN;
const INVALID_NUMBERS_CHARS = /[^\d.\s,-]+/gu;
const isColonne = (colonneToTest) => colonneToTest.colonne != null;
const dissocier = (source, localisation) => source[localisation.dissocier.colonne]
?.toString()
?.replace(INVALID_NUMBERS_CHARS, '')
?.split(localisation.dissocier.séparateur)
.filter((coord) => coord !== '')[localisation.dissocier.partie];
const checkFormatLocalisation = (localisation) => {
if (localisation === exports.NO_LOCALISATION || Number.isNaN(localisation.latitude) || Number.isNaN(localisation.longitude))
return exports.NO_LOCALISATION;
proj4_1.default.defs('EPSG:9793', '+proj=lcc +lat_0=46.5 +lon_0=3 +lat_1=49 +lat_2=44 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs');
const [longitude, latitude] = (0, proj4_1.default)('EPSG:9793', 'EPSG:4326', [localisation.longitude, localisation.latitude]);
return latitude == null || longitude == null ? exports.NO_LOCALISATION : (0, lieux_de_mediation_numerique_1.Localisation)({ latitude, longitude });
};
const localisationField = (source, localisation) => (isColonne(localisation) ? source[localisation.colonne] : dissocier(source, localisation))?.toString().replace(',', '.');
const validateLocalisationField = (localisationToValidate) => {
const localisationToValidateProbablyFalse = localisationToValidate.latitude === 0 || localisationToValidate.longitude === 0
? { latitude: LOCALISATION_IS_ZERO_VALUES, longitude: LOCALISATION_IS_ZERO_VALUES }
: localisationToValidate;
return (0, lieux_de_mediation_numerique_1.isValidLocalisation)(localisationToValidateProbablyFalse)
? localisationToValidateProbablyFalse
: checkFormatLocalisation(localisationToValidateProbablyFalse);
};
const localisationFromMatching = (source, { latitude, longitude }) => ({
latitude: parseFloat(localisationField(source, latitude) ?? NO_LOCALISATION_COLONNE),
longitude: parseFloat(localisationField(source, longitude) ?? NO_LOCALISATION_COLONNE)
});
const isValidLocalisationatching = (matching) => matching.latitude != null && matching.longitude != null;
const processLocalisation = async (source, matching, geocodeAddress) => !isValidLocalisationatching(matching)
? geocodeAddress()
: validateLocalisationField(localisationFromMatching(source, matching));
exports.processLocalisation = processLocalisation;