@gouvfr-anct/mednum
Version:
✨ Permet de transformer une source de données vers le schéma des lieux de mediation numerique
381 lines (380 loc) • 14.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanOperations = void 0;
const setPhoneCodeWhenDomTom = (codePostal) => {
const codePost = codePostal?.toString()?.slice(0, 3) ?? '';
switch (codePost) {
case '971':
return '+590';
case '972':
return '+596';
case '973':
return '+594';
case '974':
return '+262';
default:
return '+33';
}
};
const replaceNewlineInWebsites = (field) => ({
name: 'replace newline in websites',
selector: /\n/,
field,
fix: (toFix) => toFix.replace(/\n/, '')
});
const replaceDoubleDotBySingleDotInWebsites = (field) => ({
name: ': instead of . after www',
selector: /www:/g,
field,
fix: (toFix) => toFix.replace(/www:/g, 'www.')
});
const removeWebsitesStartingWithAt = (field) => ({
name: 'remove url starting by at',
selector: /^@/,
field
});
const removeWebsitesWithAccentedCharacters = (field) => ({
name: 'websites with accented characters',
// eslint-disable-next-line no-control-regex
selector: /[^\x00-\x7F]+/g,
field
});
const removeMissingExtensionWebsites = (field) => ({
name: 'missing extension websites',
selector: /^.*(?<!\.\w+\/?)$/,
field
});
const fixMissingHttpWebsitesWithMultipleUrl = (field) => ({
name: 'missing http websites with multiple url',
selector: /\|((?!http[s]?:\/\/)[^|]+)/g,
field,
fix: (toFix) => toFix.replace(/\|((?!http[s]?:\/\/)[^|]+)/g, '|http://$1')
});
const fixMissingHttpWebsites = (field) => ({
name: 'missing http websites',
selector: /^(?!http).*/,
field,
fix: (toFix) => `http://${toFix}`
});
const fixUppercaseWebsites = (field) => ({
name: 'uppercase in websites',
selector: /[A-Z]/,
field,
fix: (toFix) => toFix.toLowerCase()
});
const fixMisplacedColonInWebsite = (field) => ({
name: 'missing colon websites',
selector: /https\/\/:/,
field,
fix: (toFix) => toFix.replace(/https\/\/:/, 'https://')
});
const fixMissingColonWebsites = (field) => ({
name: 'missing colon websites',
selector: /(https?)(\/\/)/,
field,
fix: (toFix) => toFix.replace(/(https?)(\/\/)/, '$1:$2')
});
const fixMultipleUrlNotSeparatedWebsites = (field) => ({
name: 'missing separator between url',
selector: /(https?:\/\/[^|]+(?!\|))(https?:\/\/)/g,
field,
fix: (toFix) => toFix.replace(/(https?:\/\/[^|]+(?!\|))(https?:\/\/)/g, '$1|$2')
});
const fixDuplicateHttpWebsites = (field) => ({
name: 'duplicate http websites',
selector: /^https?:\/\/https?:\/\/.*/,
field,
fix: (toFix) => toFix.replace(/^https?:\/\/https?:\/\//, 'https://')
});
const fixWebsitesSeparator = (field) => ({
name: 'websites separator',
selector: /;|\s(?:ou|\/|;)\s/,
field,
fix: (toFix) => toFix.replace(/;|\s(?:ou|\/|;)\s/, '|')
});
const fixWebsitesWithSingleSlash = (field) => ({
name: 'website without colon and slash',
selector: /^https?:\/[a-zA-Z0-9]/,
field,
fix: (toFix) => toFix.replace(/:\/([a-zA-Z0-9])/, '://$1')
});
const fixWebsitesWithoutColonAndSlash = (field) => ({
name: 'website without colon and slash',
selector: /^https?\/www/,
field,
fix: (toFix) => toFix.replace(/^https?\/www/, 'https://www')
});
const fixWebsitesWithComaInsteadOfDot = (field) => ({
name: 'website with coma instead of dot',
selector: /^http:\/\/www,/,
field,
fix: (toFix) => toFix.replace(/^http:\/\/www,/, 'http://www.')
});
const fixWebsitesWithMissingSlashAfterHttp = (field) => ({
name: 'website with coma instead of dot',
selector: /^http:\/[^/]/,
field,
fix: (toFix) => toFix.replace(/^http:\//, 'http://')
});
const removeWebsitesWithSpaces = (field) => ({
name: 'websites with spaces',
selector: /\s/,
field
});
const fixWebsitesWithCodedSpacesAndParenthese = (field) => ({
name: 'websites with coded spaces',
selector: /[()]/g,
field,
fix: (toFix) => toFix.replace(/[()]/g, (match) => (match === '(' ? '%28' : '%29'))
});
const fixDetailsInParenthesisInPhone = (field) => ({
name: 'trailing details in phone',
selector: /\s\(.*\)$/g,
field,
fix: (toFix) => toFix.replace(/\s\(.*\)$/g, '')
});
const fixHeadingDetailsInPhone = (field) => ({
name: 'heading details in phone',
selector: /^\D{3,}/g,
field,
fix: (toFix) => toFix.replace(/^\D{3,}/g, '')
});
const fixTrailingDetailsInPhone = (field) => ({
name: 'trailing details in phone',
selector: /\s[A-Za-z].*$/g,
field,
fix: (toFix) => toFix.replace(/\s[A-Za-z].*$/g, '')
});
const fixWrongCharsInPhone = (field) => ({
name: 'wrong chars in phone',
selector: /(?!\w|\+)./g,
field,
fix: (toFix) => toFix.replace(/(?!\w|\+)./g, '')
});
const fixUnexpectedPhoneList = (field) => ({
name: 'unexpected phone list',
selector: /\d{10}\/\/?\d{10}/,
field,
fix: (toFix) => toFix.split('/')[0] ?? ''
});
const fixPhoneWithoutStarting0 = (field, codePostal) => ({
name: 'phone without starting 0',
selector: /^[1-9]\d{8}$/,
field,
fix: (toFix) => setPhoneCodeWhenDomTom(codePostal) + toFix
});
const fixShortCafPhone = (field) => ({
name: 'short CAF phone',
selector: /3230/,
field,
fix: () => '+33969322121'
});
const fixShortAssuranceRetraitePhone = (field) => ({
name: 'short assurance retraite phone',
selector: /3960/,
field,
fix: () => '+33971103960'
});
const fixMissingPlusCharAtStartingPhone = (field) => ({
name: 'fix missing + at starting phone number',
selector: /^(33|262|590|594|596)(\d+)/,
field,
fix: (toFix) => toFix.replace(/^(33|262|590|594|596)(\d+)/, '+$1$2')
});
const fixReplaceLeading0With33InPhoneNumberStatingWithPlus = (field) => ({
name: 'fix missing + at starting phone number',
selector: /^\+0(\d{9})/,
field,
fix: (toFix) => toFix.replace(/^\+0(\d{9})/, '+33$1')
});
const removeTooFewDigitsInPhone = (field) => ({
name: 'too few digits in phone',
selector: /^.{0,9}$/,
field
});
const removeTooManyDigitsInPhone = (field) => ({
name: 'too many digits in phone',
selector: /^0.{10,}/,
field
});
const removeOnly0ValueInPhone = (field) => ({
name: 'fake number in phone',
selector: /^0{10}$/,
field
});
const removeNoValidNumbersInPhone = (field) => ({
name: 'fake number in phone',
selector: /^[1-9]\d{9}$/,
field
});
const removeStartingByTwoZeroInPhone = (field) => ({
name: 'fake number in phone',
selector: /^00.+/,
field
});
const keepFirstNumberIfMultiple = (field) => ({
name: 'keep only the first phone number',
selector: /\n/,
field,
fix: (toFix) => /^(?<phone>[^\n]+)/u.exec(toFix)?.groups?.['phone'] ?? ''
});
const fixSpaceBeforeDotInEmail = (field) => ({
name: 'remove space before dot.',
selector: /\w\s\./,
field,
fix: (toFix) => toFix.replace(/(\w)\s\./, '$1.')
});
const fixSpaceInEmail = (field) => ({
name: 'remove space in email.',
selector: /^\w+@\w+\s\w+\.\w+$/,
field,
fix: (toFix) => toFix.replace(/\s/, '')
});
const removeTextPrecededByWrongCharacterInEmail = (field) => ({
name: 'text preceded by wrong wharacter',
selector: /^\w+[;\s]/,
field,
fix: (toFix) => toFix.replace(/^\w+[;\s]/, '')
});
const removeEmailStartingWithWww = (field) => ({
name: 'email starts with www.',
selector: /^www\./,
field
});
const removeEmailStartingWithAt = (field) => ({
name: 'email starts with @',
selector: /^@/,
field
});
const fixEmailWithTwoArobase = (field) => ({
name: 'email with two @',
selector: /@@/,
field,
fix: (toFix) => toFix.replace(/@@/, '@')
});
const trimEmail = (field) => ({
name: 'email starts with mailto:',
selector: /^\s+|\s+$/,
field,
fix: (toFix) => toFix.trim()
});
const fixEmailStartingWithMailToOrColon = (field) => ({
name: 'email starts with mailto: or colon',
selector: /^mailto:|:/,
field,
fix: (toFix) => toFix.replace(/^mailto:|:/, '')
});
const fixUnexpectedEmailLabel = (field) => ({
name: 'unexpected email label',
selector: /\S\s:\s\S/,
field,
fix: (toFix) => toFix.split(/\s:\s/)[1] ?? ''
});
const fixStartingWithDotEmail = (field) => ({
name: 'email starting with dot',
selector: /^\.([^@]+)@/,
field,
fix: (toFix) => toFix.replace(/^\.([^@]+)@/, '$1@')
});
const fixUnexpectedEmailSeparator = (field) => ({
name: 'unexpected email separator',
selector: /\S\s?(?:et|ou|;|\s|\/)\s?\S/,
field,
fix: (toFix) => toFix.replace(/\s?(?:et|ou|;|\s|\/)\s?/g, '|')
});
const fixObfuscatedAtInEmail = (field) => ({
name: 'obfuscated @ in email',
selector: /\[a\]/g,
field,
fix: (toFix) => toFix.replace('[a]', '@')
});
const removeMissingAtInEmail = (field) => ({
name: 'missing @ in email',
selector: /^[^@]+$/g,
field
});
const fixMissingEmailExtension = (field) => ({
name: 'missing dot suffix in email',
selector: /\.[a-z]{2,3}$/,
field,
negate: true
});
const removeDashEmail = (field) => ({
name: 'dash email',
selector: /^-+$/,
field
});
const removeMultipleAtEmail = (field) => ({
name: 'multiple at',
selector: /@.+@/,
field
});
const fixMissingAccentuatedEInEmail = (field) => ({
name: 'fix accentuated chars',
selector: /[éè]/,
field,
fix: (toFix) => toFix.replace(/[éè]/g, 'e')
});
const fixMissingAccentuatedCInEmail = (field) => ({
name: 'fix accentuated chars',
selector: /ç/,
field,
fix: (toFix) => toFix.replace(/ç/g, 'c')
});
const cleanOperationIfAny = (cleanOperator, colonne, codePostal) => (colonne == null ? [] : [cleanOperator(colonne, codePostal)]);
const cleanOperations = (matching, codePostal) => [
...cleanOperationIfAny(replaceNewlineInWebsites, matching.site_web?.colonne),
...cleanOperationIfAny(replaceDoubleDotBySingleDotInWebsites, matching.site_web?.colonne),
...cleanOperationIfAny(removeWebsitesStartingWithAt, matching.site_web?.colonne),
...cleanOperationIfAny(fixWebsitesSeparator, matching.site_web?.colonne),
...cleanOperationIfAny(fixDuplicateHttpWebsites, matching.site_web?.colonne),
...cleanOperationIfAny(fixUppercaseWebsites, matching.site_web?.colonne),
...cleanOperationIfAny(fixMultipleUrlNotSeparatedWebsites, matching.site_web?.colonne),
...cleanOperationIfAny(removeWebsitesWithAccentedCharacters, matching.site_web?.colonne),
...cleanOperationIfAny(removeMissingExtensionWebsites, matching.site_web?.colonne),
...cleanOperationIfAny(fixWebsitesWithSingleSlash, matching.site_web?.colonne),
...cleanOperationIfAny(fixWebsitesWithoutColonAndSlash, matching.site_web?.colonne),
...cleanOperationIfAny(fixWebsitesWithComaInsteadOfDot, matching.site_web?.colonne),
...cleanOperationIfAny(fixWebsitesWithMissingSlashAfterHttp, matching.site_web?.colonne),
...cleanOperationIfAny(removeWebsitesWithSpaces, matching.site_web?.colonne),
...cleanOperationIfAny(fixWebsitesWithCodedSpacesAndParenthese, matching.site_web?.colonne),
...cleanOperationIfAny(fixMissingHttpWebsites, matching.site_web?.colonne),
...cleanOperationIfAny(fixMissingHttpWebsitesWithMultipleUrl, matching.site_web?.colonne),
...cleanOperationIfAny(fixMisplacedColonInWebsite, matching.site_web?.colonne),
...cleanOperationIfAny(fixMissingColonWebsites, matching.site_web?.colonne),
...cleanOperationIfAny(removeStartingByTwoZeroInPhone, matching.telephone?.colonne),
...cleanOperationIfAny(removeNoValidNumbersInPhone, matching.telephone?.colonne),
...cleanOperationIfAny(fixUnexpectedPhoneList, matching.telephone?.colonne),
...cleanOperationIfAny(fixDetailsInParenthesisInPhone, matching.telephone?.colonne),
...cleanOperationIfAny(fixHeadingDetailsInPhone, matching.telephone?.colonne),
...cleanOperationIfAny(fixTrailingDetailsInPhone, matching.telephone?.colonne),
...cleanOperationIfAny(fixWrongCharsInPhone, matching.telephone?.colonne),
...cleanOperationIfAny(fixPhoneWithoutStarting0, matching.telephone?.colonne, codePostal),
...cleanOperationIfAny(fixShortCafPhone, matching.telephone?.colonne),
...cleanOperationIfAny(removeDashEmail, matching.courriels?.colonne),
...cleanOperationIfAny(fixShortAssuranceRetraitePhone, matching.telephone?.colonne),
...cleanOperationIfAny(removeTooFewDigitsInPhone, matching.telephone?.colonne),
...cleanOperationIfAny(removeTooManyDigitsInPhone, matching.telephone?.colonne),
...cleanOperationIfAny(removeOnly0ValueInPhone, matching.telephone?.colonne),
...cleanOperationIfAny(keepFirstNumberIfMultiple, matching.telephone?.colonne),
...cleanOperationIfAny(fixMissingPlusCharAtStartingPhone, matching.telephone?.colonne),
...cleanOperationIfAny(fixReplaceLeading0With33InPhoneNumberStatingWithPlus, matching.telephone?.colonne),
...cleanOperationIfAny(fixSpaceBeforeDotInEmail, matching.courriels?.colonne),
...cleanOperationIfAny(fixSpaceInEmail, matching.courriels?.colonne),
...cleanOperationIfAny(removeEmailStartingWithWww, matching.courriels?.colonne),
...cleanOperationIfAny(removeEmailStartingWithAt, matching.courriels?.colonne),
...cleanOperationIfAny(trimEmail, matching.courriels?.colonne),
...cleanOperationIfAny(fixMissingAccentuatedEInEmail, matching.courriels?.colonne),
...cleanOperationIfAny(fixMissingAccentuatedCInEmail, matching.courriels?.colonne),
...cleanOperationIfAny(fixEmailWithTwoArobase, matching.courriels?.colonne),
...cleanOperationIfAny(fixStartingWithDotEmail, matching.courriels?.colonne),
...cleanOperationIfAny(fixEmailStartingWithMailToOrColon, matching.courriels?.colonne),
...cleanOperationIfAny(fixUnexpectedEmailLabel, matching.courriels?.colonne),
...cleanOperationIfAny(removeTextPrecededByWrongCharacterInEmail, matching.courriels?.colonne),
...cleanOperationIfAny(fixUnexpectedEmailSeparator, matching.courriels?.colonne),
...cleanOperationIfAny(fixObfuscatedAtInEmail, matching.courriels?.colonne),
...cleanOperationIfAny(fixMissingEmailExtension, matching.courriels?.colonne),
...cleanOperationIfAny(removeMissingAtInEmail, matching.courriels?.colonne),
...cleanOperationIfAny(removeMultipleAtEmail, matching.courriels?.colonne)
];
exports.cleanOperations = cleanOperations;