|
| 1 | +import { CleanOperation } from './clean-operation' |
| 2 | + |
| 3 | +const fixDetailsInParenthesisInPhone = (field: string): CleanOperation => ({ |
| 4 | + name: 'trailing details in phone', |
| 5 | + selector: /\s\(.*\)$/g, |
| 6 | + field, |
| 7 | + fix: (toFix: string): string => toFix.replace(/\s\(.*\)$/g, ''), |
| 8 | +}) |
| 9 | + |
| 10 | +const fixHeadingDetailsInPhone = (field: string): CleanOperation => ({ |
| 11 | + name: 'heading details in phone', |
| 12 | + selector: /^\D{3,}/g, |
| 13 | + field, |
| 14 | + fix: (toFix: string): string => toFix.replace(/^\D{3,}/g, ''), |
| 15 | +}) |
| 16 | + |
| 17 | +const fixTrailingDetailsInPhone = (field: string): CleanOperation => ({ |
| 18 | + name: 'trailing details in phone', |
| 19 | + selector: /\s[A-Za-z].*$/g, |
| 20 | + field, |
| 21 | + fix: (toFix: string): string => toFix.replace(/\s[A-Za-z].*$/g, ''), |
| 22 | +}) |
| 23 | + |
| 24 | +const fixWrongCharsInPhone = (field: string): CleanOperation => ({ |
| 25 | + name: 'wrong chars in phone', |
| 26 | + selector: /(?!\w|\+)./g, |
| 27 | + field, |
| 28 | + fix: (toFix: string): string => toFix.replace(/(?!\w|\+)./g, ''), |
| 29 | +}) |
| 30 | + |
| 31 | +const fixUnexpectedPhoneList = (field: string): CleanOperation => ({ |
| 32 | + name: 'unexpected phone list', |
| 33 | + selector: /\d{10}\/\/?\d{10}/, |
| 34 | + field, |
| 35 | + fix: (toFix: string): string => toFix.split('/')[0] ?? '', |
| 36 | +}) |
| 37 | + |
| 38 | +const fixShortCafPhone = (field: string): CleanOperation => ({ |
| 39 | + name: 'short CAF phone', |
| 40 | + selector: /3230/, |
| 41 | + field, |
| 42 | + fix: (): string => '+33969322121', |
| 43 | +}) |
| 44 | + |
| 45 | +const fixShortAssuranceRetraitePhone = (field: string): CleanOperation => ({ |
| 46 | + name: 'short assurance retraite phone', |
| 47 | + selector: /3960/, |
| 48 | + field, |
| 49 | + fix: (): string => '+33971103960', |
| 50 | +}) |
| 51 | + |
| 52 | +const fixMissingPlusCharAtStartingPhone = (field: string): CleanOperation => ({ |
| 53 | + name: 'fix missing + at starting phone number', |
| 54 | + selector: /^(33|262|590|594|596)(\d+)/, |
| 55 | + field, |
| 56 | + fix: (toFix: string): string => |
| 57 | + toFix.replace(/^(33|262|590|594|596)(\d+)/, '+$1$2'), |
| 58 | +}) |
| 59 | + |
| 60 | +const fixReplaceLeading0With33InPhoneNumberStatingWithPlus = ( |
| 61 | + field: string, |
| 62 | +): CleanOperation => ({ |
| 63 | + name: 'fix missing + at starting phone number', |
| 64 | + selector: /^\+0(\d{9})/, |
| 65 | + field, |
| 66 | + fix: (toFix: string): string => toFix.replace(/^\+0(\d{9})/, '+33$1'), |
| 67 | +}) |
| 68 | + |
| 69 | +const removeTooFewDigitsInPhone = (field: string): CleanOperation => ({ |
| 70 | + name: 'too few digits in phone', |
| 71 | + selector: /^.{0,9}$/, |
| 72 | + field, |
| 73 | +}) |
| 74 | + |
| 75 | +const removeTooManyDigitsInPhone = (field: string): CleanOperation => ({ |
| 76 | + name: 'too many digits in phone', |
| 77 | + selector: /^0.{10,}/, |
| 78 | + field, |
| 79 | +}) |
| 80 | + |
| 81 | +const removeOnly0ValueInPhone = (field: string): CleanOperation => ({ |
| 82 | + name: 'fake number in phone', |
| 83 | + selector: /^0{10}$/, |
| 84 | + field, |
| 85 | +}) |
| 86 | + |
| 87 | +const removeNoValidNumbersInPhone = (field: string): CleanOperation => ({ |
| 88 | + name: 'fake number in phone', |
| 89 | + selector: /^[1-9]\d{9}$/, |
| 90 | + field, |
| 91 | +}) |
| 92 | + |
| 93 | +const removeStartingByTwoZeroInPhone = (field: string): CleanOperation => ({ |
| 94 | + name: 'fake number in phone', |
| 95 | + selector: /^00.+/, |
| 96 | + field, |
| 97 | +}) |
| 98 | + |
| 99 | +const keepFirstNumberIfMultiple = (field: string): CleanOperation => ({ |
| 100 | + name: 'keep only the first phone number', |
| 101 | + selector: /\n/, |
| 102 | + field, |
| 103 | + fix: (toFix: string): string => |
| 104 | + /^(?<phone>[^\n]+)/u.exec(toFix)?.groups?.phone ?? '', |
| 105 | +}) |
| 106 | + |
| 107 | +const cleanOperationIfAny = ( |
| 108 | + cleanOperator: (colonne: string) => CleanOperation, |
| 109 | + telephone: string | null, |
| 110 | +): CleanOperation[] => (telephone == null ? [] : [cleanOperator(telephone)]) |
| 111 | + |
| 112 | +export const cleanTelephone = (telephone: string | null): CleanOperation[] => [ |
| 113 | + ...cleanOperationIfAny(removeStartingByTwoZeroInPhone, telephone), |
| 114 | + ...cleanOperationIfAny(removeNoValidNumbersInPhone, telephone), |
| 115 | + ...cleanOperationIfAny(fixUnexpectedPhoneList, telephone), |
| 116 | + ...cleanOperationIfAny(fixDetailsInParenthesisInPhone, telephone), |
| 117 | + ...cleanOperationIfAny(fixHeadingDetailsInPhone, telephone), |
| 118 | + ...cleanOperationIfAny(fixTrailingDetailsInPhone, telephone), |
| 119 | + ...cleanOperationIfAny(fixWrongCharsInPhone, telephone), |
| 120 | + ...cleanOperationIfAny(fixShortCafPhone, telephone), |
| 121 | + ...cleanOperationIfAny(fixShortAssuranceRetraitePhone, telephone), |
| 122 | + ...cleanOperationIfAny(removeTooFewDigitsInPhone, telephone), |
| 123 | + ...cleanOperationIfAny(removeTooManyDigitsInPhone, telephone), |
| 124 | + ...cleanOperationIfAny(removeOnly0ValueInPhone, telephone), |
| 125 | + ...cleanOperationIfAny(keepFirstNumberIfMultiple, telephone), |
| 126 | + ...cleanOperationIfAny(fixMissingPlusCharAtStartingPhone, telephone), |
| 127 | + ...cleanOperationIfAny( |
| 128 | + fixReplaceLeading0With33InPhoneNumberStatingWithPlus, |
| 129 | + telephone, |
| 130 | + ), |
| 131 | +] |
| 132 | + |
| 133 | +const canFixTelephone = ( |
| 134 | + telephone: string | null, |
| 135 | + cleanOperation: CleanOperation, |
| 136 | +): telephone is string => |
| 137 | + telephone != null && cleanOperation.selector.test(telephone) |
| 138 | + |
| 139 | +const applyOperation = |
| 140 | + (cleanOperation: CleanOperation) => (telephone: string) => |
| 141 | + cleanOperation.fix ? cleanOperation.fix(telephone) : null |
| 142 | + |
| 143 | +const toFixedTelephone = ( |
| 144 | + telephone: string | null, |
| 145 | + cleanOperation: CleanOperation, |
| 146 | +): string | null => |
| 147 | + canFixTelephone(telephone, cleanOperation) |
| 148 | + ? applyOperation(cleanOperation)(telephone) |
| 149 | + : telephone |
| 150 | + |
| 151 | +const toInternationalFormat = (phone: string): string => |
| 152 | + /^0\d{9}$/.test(phone) ? `+33${phone.slice(1)}` : phone |
| 153 | + |
| 154 | +export const fixTelephone = (telephone: string | null) => { |
| 155 | + const fixed = cleanTelephone(telephone).reduce(toFixedTelephone, telephone) |
| 156 | + return fixed == null ? null : toInternationalFormat(fixed) |
| 157 | +} |
0 commit comments