Skip to content

Commit

Permalink
adds a max functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwicopple authored Jan 20, 2023
1 parent 7c48c4d commit 00cc025
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions components/Issues.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ToggleItem,
} from "@tremor/react"
import useSWR from "swr"
import { fetcher, useUrlFilters } from "../lib/helpers"
import { fetcher, useUrlFilters, findMax } from "../lib/helpers"

// Basic formatters for the chart values
const dollarFormatter = (value: number) =>
Expand Down Expand Up @@ -99,7 +99,7 @@ FORMAT JSON`,
if (!data) return <div>Loading...</div>

const chartData = data.data
const max = parseInt(chartData[chartData.length - 1]["tally"]) * 1.2
const max = parseInt(findMax(chartData, 'tally'))

return (
<Card>
Expand Down
4 changes: 2 additions & 2 deletions components/PullRequests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ToggleItem,
} from "@tremor/react"
import useSWR from "swr"
import { fetcher, useUrlFilters } from "../lib/helpers"
import { fetcher, useUrlFilters, findMax } from "../lib/helpers"

// Basic formatters for the chart values
const dollarFormatter = (value: number) =>
Expand Down Expand Up @@ -99,7 +99,7 @@ FORMAT JSON`,
if (!data) return <div>Loading...</div>

const chartData = data.data
const max = parseInt(chartData[chartData.length - 1]["tally"]) * 1.2
const max = parseInt(findMax(chartData, 'tally'))

return (
<Card>
Expand Down
10 changes: 10 additions & 0 deletions lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ export const useUrlFilters = (): UrlFilters => {
}
return filters
}

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

0 comments on commit 00cc025

Please sign in to comment.