|
interface SocioDemographyItem { |
|
age: string; |
|
gender: string; |
|
percentage: number; |
|
social_group: string; |
|
} |
|
|
|
export interface SummaryItem { |
|
label: string; |
|
usage: number; |
|
} |
|
|
|
export function generateSummary(data: SocioDemographyItem[]): SummaryItem[] { |
|
const summaryData: SummaryItem[] = []; |
|
|
|
function calculateUsage(age: string, gender: string): number { |
|
const filteredData = data.filter( |
|
(item) => item.age === age && item.gender.toUpperCase() === gender |
|
); |
|
const totalPercentage = filteredData.reduce( |
|
(total, item) => total + item.percentage, |
|
0 |
|
); |
|
return totalPercentage; |
|
} |
|
|
|
const ageGroups = ["15-24", "25-34", "35-49", "50-64", "65-PLUS"]; |
|
const genders = ["MALE", "FEMALE"]; |
|
|
|
for (const gender of genders) { |
|
for (const age of ageGroups) { |
|
const label = `${gender} ${age}`; |
|
const usage = calculateUsage(age, gender); |
|
summaryData.push({ label, usage }); |
|
} |
|
} |
|
|
|
return summaryData; |
|
} |
|
|