Skip to content

Commit

Permalink
fix the max functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwicopple committed Jan 20, 2023
1 parent 4469724 commit ca722bb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion components/Issues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ FORMAT JSON`,
if (!data) return <div>Loading...</div>

const chartData = data.data
const max = parseInt(findMax(chartData, 'tally').toString())
const max = findMax(chartData, "tally")

return (
<Card>
Expand Down
2 changes: 1 addition & 1 deletion components/PullRequests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ FORMAT JSON`,
if (!data) return <div>Loading...</div>

const chartData = data.data
const max = parseInt(findMax(chartData, 'tally').toString())
const max = findMax(chartData, "tally")

return (
<Card>
Expand Down
6 changes: 4 additions & 2 deletions components/Stars.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ FORMAT JSON`,
// console.log("data", data)
// console.log("error", error)

if (!data) return <div>Loading...</div>
if (!data || !data.data) return <div>Loading...</div>

const starHistory = data.data
const max = parseInt(starHistory[starHistory.length - 1]["stars"])
const max = starHistory.length
? parseInt(starHistory[starHistory.length - 1]["stars"])
: 0
return (
<Card>
<div className="md:flex justify-between">
Expand Down
10 changes: 6 additions & 4 deletions lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export const useUrlFilters = (): UrlFilters => {
}

// Find the max value from an array of objects (or just an array of numbers)
export function findMax(objects: any[], key?: string) {
export function findMax(objects: any[], key?: string): number {
let max = 0
for (let i = 0; i < objects.length; i++) {
const val = key? objects[i][key]: objects[i]
if (val > max) max = val
const val: number = key ? objects[i][key] : objects[i]
const valInt = parseInt(val.toString())
if (valInt > max) max = valInt
}
console.log("max", max)
return max
}
}

1 comment on commit ca722bb

@vercel
Copy link

@vercel vercel bot commented on ca722bb Jan 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.