Skip to content

Commit

Permalink
minification logic update
Browse files Browse the repository at this point in the history
  • Loading branch information
theDevSoham committed Apr 29, 2023
1 parent a201b71 commit af32d0b
Showing 1 changed file with 62 additions and 13 deletions.
75 changes: 62 additions & 13 deletions src/static/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,35 @@ const getChart1Data = (): ChartDataType => {
*/
const getChart2Data = (): ChartDataType => {
/*
*logic to convert the string data to number for *minimum* Alcohol, if there is one, and return the data
*logic: get the collective data for Alcohol and Magnesium from all the data and return minimum magnesium for each alcohol data
*/

const alcohol = data.map((item) => {
if (typeof item.Alcohol === 'string') {
return parseFloat(item.Alcohol)
/*
* Get the collective data for Alcohol and Magnesium from all the data
*/
const collectiveData = data.map((item) => {
return {
Alcohol:
typeof item.Alcohol === 'string'
? parseFloat(item.Alcohol)
: item.Alcohol,
Magnesium:
typeof item.Magnesium === 'string'
? parseFloat(item.Magnesium)
: item.Magnesium
}

return item.Alcohol
})

/*
*logic to convert the string data to number for *minimum* Magnesium, if there is one, and return the data
* Get minimum magnesium for each alcohol data. Process: structureAlcoholMagnesiumData -> structures the alcohol and magnesium data as a key value pair => minifyData -> get the minimum magnesium for each alcohol data
*/
const magnesium = data.map((item) => {
if (typeof item.Magnesium === 'string') {
return Math.floor(parseFloat(item.Magnesium))
}

return Math.floor(item.Magnesium)
})
const processedData = minifyData(
structureAlcoholMagnesiumData(collectiveData)
)
const alcohol = Array.from(processedData.keys())
const magnesium = Array.from(processedData.values())

return {
horizontal: alcohol,
vertical: magnesium,
Expand All @@ -80,4 +88,45 @@ const getChart2Data = (): ChartDataType => {
}
}

/*
*Function to structure data into a map with alcohol as key and magnesium as value
*/

const structureAlcoholMagnesiumData = (data: any): Map<number, number[]> => {
return data.reduce((acc: Map<number, number[]>, item: any) => {
const key = item.Alcohol
const value = item.Magnesium

if (acc.has(key)) {
const currentValues = acc.get(key) ?? []
acc.set(key, [...currentValues, value])
} else {
acc.set(key, [value])
}

return acc
}, new Map())
}

/*
*Function to get the minimum magnesium for each alcohol data
*/
const minifyData = (structured: Map<number, number[]>): Map<number, number> => {
const minified = new Map<number, number>()

structured.forEach((value, key) => {
minified.set(
key,
value.reduce((acc: number, item: number) => {
if (item < acc) {
acc = item
}
return acc
}, value[0])
)
})

return minified
}

export { getChart1Data, getChart2Data }

0 comments on commit af32d0b

Please sign in to comment.