github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/frontend/bv/src/shared/logic/utils.ts (about) 1 import { 2 Date_, 3 ModePaiment, 4 ModePaimentLabels, 5 OptionsCamp, 6 PublicDocument, 7 ResponsableLegal, 8 Sexe, 9 Time 10 } from "@/shared/logic/types"; 11 12 export const MaxFileSize = 5000000; // bytes 13 14 export const isNullTime = (s: string) => 15 !s || s.length < 10 || s.substr(0, 10) == "0001-01-01"; 16 17 export function formatDateTime(dateString: Time): string { 18 if (isNullTime(dateString)) return ""; 19 return new Date(dateString).toLocaleTimeString("fr-FR", { 20 day: "numeric", 21 month: "short", 22 year: "numeric", 23 hour: "2-digit", 24 minute: "2-digit" 25 }); 26 } 27 28 export function formatDateObject(date: Date): string { 29 return date.toLocaleDateString("fr-FR", { timeZone: "UTC" }); 30 } 31 32 export function formatDate(dateString: Date_): string { 33 if (isNullTime(dateString)) return ""; 34 return formatDateObject(new Date(dateString)); 35 } 36 37 export function descriptionDocument(doc: PublicDocument) { 38 var dt = formatDateTime(doc.date_heure_modif); 39 dt = dt ? `, modifié le <i>${dt}</i>` : ""; 40 return `<i>${doc.nom_client}</i>${dt}`; 41 } 42 43 export function age(debut: Date, dateNaissance: Date_) { 44 const naissance = new Date(dateNaissance); 45 if (!debut || !naissance) { 46 return -1; 47 } 48 const diff = debut.getFullYear() - naissance.getFullYear(); 49 const isNaissanceBefore = 50 naissance.getMonth() < debut.getMonth() || 51 (naissance.getMonth() == debut.getMonth() && 52 naissance.getDate() <= debut.getDate()); 53 if (isNaissanceBefore) { 54 return diff; 55 } 56 return diff - 1; 57 } 58 59 export function formatModePaiement(mode: ModePaiment) { 60 return ModePaimentLabels[mode] || ""; 61 } 62 63 export function isDocumentNull(doc: PublicDocument | null) { 64 return doc == null || !doc.id_crypted; 65 } 66 67 export const nullTimeString = "0001-01-01T00:00:00Z" as Time; 68 export const nullDateString = "0001-01-01" as Date_; 69 70 export function isNullDateString(date: string) { 71 if (date.length < 10) return true; // invalide 72 if (date.length == 10) { 73 date = date + "T00:00"; 74 } 75 return date.startsWith("0001-01-01T00:00"); 76 } 77 78 export function nullDocument(): PublicDocument { 79 return { 80 id_crypted: "", 81 nom_client: "", 82 taille: 0, 83 date_heure_modif: nullTimeString, 84 url_miniature: "", 85 url_download: "" 86 }; 87 } 88 89 /** Comme id_crypted change entre deux modifications 90 * cet objet garde la trace de l'id original */ 91 export interface ModifDocument { 92 idCrypted: string; 93 document: PublicDocument; 94 } 95 96 export function nullResponsableInscription(): ResponsableLegal { 97 return { 98 adresse: "", 99 code_postal: "", 100 date_naissance: nullDateString, 101 lienid: { crypted: "", id: 0, valid: false }, 102 mail: "", 103 nom: "", 104 pays: "", 105 prenom: "", 106 sexe: Sexe.SAucun, 107 tels: [], 108 ville: "" 109 }; 110 } 111 112 interface camp { 113 options: OptionsCamp; 114 } 115 export function hasBus(c: camp | null) { 116 return c && c.options.bus.actif; 117 } 118 119 export function hasMaterielSki(c: camp | null) { 120 return c && c.options.materiel_ski && c.options.materiel_ski.actif; 121 } 122 123 const reSepTel = /[ -/;\t]/g; 124 125 function splitBySize(a: string) { 126 const b = []; 127 for (var i = 2; i < a.length; i += 2) { 128 // length 2, for example 129 b.push(a.slice(i - 2, i)); 130 } 131 b.push(a.slice(a.length - (2 - (a.length % 2)))); // last fragment 132 return b; 133 } 134 135 export function formatTel(tel: string) { 136 tel = tel.replace(reSepTel, ""); 137 if (tel.length < 8) { 138 return splitBySize(tel).join(" "); 139 } // numéro incomplet, on insert des espaces 140 const start = tel.length - 8; // 8 derniers chiffres 141 const chunks = [tel.substr(0, start)]; 142 for (let i = 0; i < 4; i++) { 143 chunks.push(tel.substr(start + 2 * i, 2)); 144 } 145 return chunks.join(" "); 146 } 147 148 const securiteSocialSplitIndexes = [1, 3, 5, 7, 10, 13, 15, 0]; 149 const N = securiteSocialSplitIndexes.length; 150 export function formatSecuriteSociale(s: string) { 151 s = s.replace(/\s/g, ""); 152 const chunks = securiteSocialSplitIndexes.map((_, i) => { 153 const indexStart = i == 0 ? 0 : securiteSocialSplitIndexes[i - 1]; 154 const indexEnd = i == N - 1 ? undefined : securiteSocialSplitIndexes[i]; 155 const chunk = s.substring(indexStart, indexEnd); 156 return chunk; 157 }); 158 return chunks.filter(c => !!c).join(" "); 159 } 160 161 // Décode le contenu de la balise "meta" contenant des données additionnelles 162 export function decodeServerPayload<T>(): T | null { 163 const meta = document.head.querySelector('meta[name="serverPayload"]'); 164 if (meta == null) return null; 165 const json = (meta as HTMLMetaElement).content; 166 try { 167 return JSON.parse(json); 168 } catch { 169 return null; 170 } 171 } 172 173 const patternMail = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 174 export function isEmailValid(mail: string) { 175 return patternMail.test(mail); 176 } 177 178 const isZero = <T extends string | number>(a: T) => a == "" || a == 0; 179 180 export function enumToOptions<T extends string | number>( 181 enums: { [key in T]: string } 182 ) { 183 const out: { value: T; text: string }[] = []; 184 for (const value in enums) { 185 const text = enums[value]; 186 out.push({ value, text }); 187 } 188 return out.sort((a, b) => { 189 if (isZero(a.value)) { 190 return -1; 191 } 192 return a.text.localeCompare(b.text); 193 }); 194 }