class Arrays { /** * Vérifie si les deux tableaux ont le même contenu */ static areEqual(array1: Array, array2: Array) { if (array1.length === array2.length) { return array1.every((element, index) => { if (element === array2[index]) { return true; } return false; }); } return false; } /** * Fournie une fonction to1D qui transforme un tableau 2D en un tableau 1D par concaténation */ /*eslint-disable @typescript-eslint/no-explicit-any*/ static from2d(source: any) { return { to1D: () => { let result: Array = []; source.forEach((elt: Array) => (result = result.concat(elt))); return this.from(result); }, }; } /*eslint-enable @typescript-eslint/no-explicit-any*/ /** * Fournie des méthodes pour manipuler un tableau * A l'écriture cela permet d'avoir des phrases du type: * let data = Arrays.from(mon tableau) * puis data.everyItem().has().same().value().forkey("key"); * Peu mieux faire. ou voir a remplacer un jour par loadHash par exemple. **/ /*eslint-disable @typescript-eslint/no-explicit-any*/ static from(source: any) { return { /**Obtenir le tableau final*/ get: () => source || [], everyItem: () => { return { has: () => { return { same: () => { return { value: () => { return { forKey: (key: string) => { if (source.length < 1) return true; const first = (source[0] || {})[key]; return source.every( (item: any) => (item || {})[key] == first ); }, }; }, }; }, }; }, value: () => { return { forKey: (key: string) => { return Arrays.from(source.map((item: any) => item[key])); }, }; }, copy: () => { return { fromKey: (atKey: string) => { return { toKey: (toKey: string) => { source.forEach((item: any) => { item[toKey] = Array.isArray(item[atKey]) ? [...item[atKey]] : typeof item[atKey] === "object" && item[atKey] != null ? {...item[atKey]} : item[atKey]; }); }, }; }, }; }, }; }, map: (f: (elt: Record) => any) => Arrays.from(source.map(f)), filter: (f: (elt: any) => any) => Arrays.from(source.filter(f)), find: (f: (elt: Record) => any) => source.find(f), some: (f: (elt: Record) => any) => source.some(f), every: (f: (elt: Record) => any) => source.every(f), group: () => { return { byKey: (key: string) => { const result: Array> = []; const keys: Map = new Map(); for (const item of source) { const value = item[key]; if (!keys.has(value)) { const idx = result.length; keys.set(value, idx); const pushable: Record = {items: []}; pushable[key] = value; result.push(pushable); } result[keys.get(value) as number].items.push(item); } return Arrays.from(result); }, }; }, without: () => { return { duplicates: () => { return { forKey: (key: string) => { const set = [...new Set(source.map((item: any) => item[key]))]; return Arrays.from( set.map((value) => source.find((item: any) => item[key] == value) ) as Array> ); }, }; }, itemsIn: (toRemoveFromSource: Array>) => { return { havingSameValue: () => { return { forKey: (key: string) => { const areValuesDifferentForKey = (compared1: Record, key: string) => (compared2: Record) => compared1[key] != compared2[key]; return Arrays.from( source.filter((elt: any) => toRemoveFromSource.every( areValuesDifferentForKey(elt, key) ) ) ); }, }; }, }; }, }; }, }; } /*eslint-enable @typescript-eslint/no-explicit-any*/ } export default Arrays;