Untitled
unknown
plain_text
2 years ago
5.9 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 isInitialDate = (referenceObject: ReferenceModel):
boolean => referenceObject.date === null;
const clearMapAndIndex = () => {
return {
index: 0,
map: {}
}
};
const addZeroedScore = (
type: string,
referenceObject: ReferenceModel,
map: Record<string, boolean>,
arrayRef: string[],
index: number
): 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 = (
arrayRef: string[],
index: number
): boolean => index < arrayRef.length;
const isCategoryDifferentFromArrayRef = (
arrayRef: string[],
index: number,
category?: string
): boolean =>
!category || category !== arrayRef[index];
const checkMissingScores = (
type: string,
referenceObject: ReferenceModel,
map: Record<string, boolean>,
arrayRef: string[],
index: number,
category?: string
): void => {
while (
isIndexLessThanArrayRef(arrayRef, index) &&
isCategoryDifferentFromArrayRef(arrayRef, index, category)
) {
addZeroedScore(
type,
referenceObject,
map,
arrayRef,
index
);
}
};
const shouldNotRepeatCategory = (
model: any,
map: Record<string, boolean>
): boolean => {
return !(model.category in map);
};
const updateReferenceDate = (
safra: string,
referenceObject: ReferenceModel
): void => {
if (isInitialDate(referenceObject)) {
referenceObject.date = safra;
}
};
const processDifferentCategory = (
type: string,
model: any,
referenceObject: ReferenceModel,
map: Record<string, boolean>,
arrayRef: string[],
index: number
): void => {
if (!isInitialDate(referenceObject) && arrayRef[index] !== model.category) {
checkMissingScores(
type,
referenceObject,
map,
arrayRef,
index,
model.category
);
}
};
const addCategoryScore = (
type: string,
model: any,
referenceObject: ReferenceModel,
map: Record<string, boolean>,
index: number
): 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 processNotRepeatedCategory = (
type: string,
model: any,
safra: string,
referenceObject: ReferenceModel,
map: Record<string, boolean>,
arrayRef: string[],
index: number
): void => {
if (shouldNotRepeatCategory(model, map)) {
updateReferenceDate(
safra,
referenceObject
);
processDifferentCategory(
type,
model,
referenceObject,
map,
arrayRef,
index
);
if (arrayRef[index] === model.category) {
addCategoryScore(
type,
model,
referenceObject,
map,
index
);
}
}
};
const publicDistributionReference = (
dataDistribution: any,
type: string,
data: SummaryData,
map: Record<string, boolean>,
arrayRef: string[],
index: number
) => {
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 processHistoryModel = (
dataDistribution: any,
type: string,
data: SummaryData,
referenceObject: ReferenceModel,
map: Record<string, boolean>,
arrayRef: string[],
index: number
): void => {
if (dataDistribution.history.length) {
clearMapAndIndex();
dataDistribution.history.forEach((model: any) => {
const safra = String(model.safra);
if (!isInitialDate(referenceObject) && referenceObject.date !== safra) {
checkMissingScores(
type,
referenceObject,
map,
arrayRef,
index,
model.category
);
data.history.push({ ...referenceObject });
referenceObject.date = safra;
referenceObject.scores = [];
clearMapAndIndex();
}
processNotRepeatedCategory(
type,
model,
safra,
referenceObject,
map,
arrayRef,
index
);
});
checkMissingScores(
type,
referenceObject,
map,
arrayRef,
index
);
data.history.push({ ...referenceObject });
}
};
export const publicDistributionSummary = (dataDistribution: any, type: string) => {
const data: SummaryData = {
reference: {
date: '',
scores: [],
},
history: [],
};
let index: number = 0;
let map: Record<string, boolean> = {};
const arrayRef: string[] = [];
const referenceObject: ReferenceModel = initializeReferenceObject();
publicDistributionReference(
dataDistribution,
type,
data,
map,
arrayRef,
index
);
processHistoryModel(
dataDistribution,
type,
data,
referenceObject,
map,
arrayRef,
index
);
return data;
};Editor is loading...
Leave a Comment