Untitled
unknown
plain_text
a year ago
3.8 kB
5
Indexable
interface ReferenceModel { date: string | null; scores: { value: number; description: string }[]; } interface SummaryData { reference: { date: string; scores: { value: number; description: string }[]; }; history: ReferenceModel[]; } const initializeReferenceObject = (): ReferenceModel => ({ date: null, scores: [], }); const referenceObject: ReferenceModel = initializeReferenceObject(); const isInitialDate = (): boolean => referenceObject.date === null; const clearMapAndIndex = (): void => { index = 0; map = {}; }; const addZeroedScore = (type: string): void => { const categoryDescription = type === 'variable' ? index : index + 1; const modelObject = { value: 0.0, description: String(categoryDescription), }; referenceObject.scores.push(modelObject); const categoryArrayRef = arrayRef[index]; map[categoryArrayRef] = true; index += 1; }; const isIndexLessThanArrayRef = (): boolean => index < arrayRef.length; const isCategoryDifferentFromArrayRef = (category?: string): boolean => !category || category !== arrayRef[index]; const checkMissingScores = (type: string, category?: string): void => { while (isIndexLessThanArrayRef() && isCategoryDifferentFromArrayRef(category)) { addZeroedScore(type); } }; const shouldNotRepeatCategory = (type: string, model: any): boolean => { return !(model.category in map); }; const addCategoryScore = (type: string, model: any): void => { const categoryDescription = type === 'variable' ? index : index + 1; const modelObject = { value: parseFloat(model.percentage.toFixed(2)), description: String(categoryDescription), }; referenceObject.scores.push(modelObject); map[model.category] = true; index += 1; }; const publicDistributionReference = (dataDistribution: any, type: string) => { dataDistribution.reference.forEach((model: any) => { if (!(model.category in map)) { const categoryDescription = type === 'variable' ? index : index + 1; const modelObject = { value: parseFloat(model.percentage.toFixed(2)), description: String(categoryDescription), }; data.reference.date = model.safra; data.reference.scores.push(modelObject); map[model.category] = true; arrayRef.push(model.category); index += 1; } }); return data; }; const publicDistributionHistory = (dataDistribution: any, type: string): void => { if (dataDistribution.history.length) { clearMapAndIndex(); dataDistribution.history.forEach((model: any) => { const safra = String(model.safra); if (!isInitialDate() && referenceObject.date !== safra) { checkMissingScores(type, model.category); data.history.push({ ...referenceObject }); referenceObject.date = safra; referenceObject.scores = []; clearMapAndIndex(); } if (shouldNotRepeatCategory(type, model)) { if (isInitialDate()) { referenceObject.date = safra; } if (!isInitialDate() && arrayRef[index] !== model.category) { checkMissingScores(type, model.category); } if (arrayRef[index] === model.category) { addCategoryScore(type, model); } } }); checkMissingScores(type); data.history.push({ ...referenceObject }); } }; export const publicDistributionSummary = (dataDistribution: any, type: string) => { const data: SummaryData = { reference: { date: '', scores: [], }, history: [], }; let index = 0; let map: Record<string, boolean> = {}; const arrayRef: string[] = []; publicDistributionReference(dataDistribution, type); publicDistributionHistory(dataDistribution, type); return data; };
Editor is loading...
Leave a Comment