Skip to content

Commit 17ea220

Browse files
authored
chore: use smooth path utility from vue-data-ui (#2647)
1 parent 350ec07 commit 17ea220

3 files changed

Lines changed: 3 additions & 43 deletions

File tree

app/components/OgImage/Package.takumi.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const KIND_ICONS: Record<string, string> = {
1515
<script setup lang="ts">
1616
import type { JsDelivrFileNode } from '#shared/types'
1717
import { joinURL } from 'ufo'
18-
import { smoothPath, useCharts } from '~/composables/useCharts'
18+
import { useCharts } from '~/composables/useCharts'
19+
import { createSmoothPath } from 'vue-data-ui/utils'
1920
2021
const REPO_PROVIDER_ICONS: Record<string, string> = {
2122
github: 'i-simple-icons:github',
@@ -239,7 +240,7 @@ const sparklineSrc = computed(() => {
239240
y: padY + (1 - (v - min) / range) * (height - padY * 2),
240241
}))
241242
242-
const pathData = smoothPath(points)
243+
const pathData = createSmoothPath(points)
243244
const firstX = points[0]!.x
244245
const lastX = points.at(-1)!.x
245246

app/composables/useCharts.ts

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -66,46 +66,6 @@ function mergeDailyPoints(points: DailyRawPoint[]): DailyRawPoint[] {
6666
.map(([day, value]) => ({ day, value }))
6767
}
6868

69-
function roundToHundredths(value: number): number {
70-
return Math.round(value * 100) / 100
71-
}
72-
73-
/** Catmull-Rom monotone cubic spline — same algorithm as vue-data-ui's smoothPath for OG Images */
74-
export function smoothPath(pts: { x: number; y: number }[]): string {
75-
if (pts.length < 2) return '0,0'
76-
const n = pts.length - 1
77-
const out = [`${roundToHundredths(pts[0]!.x)},${roundToHundredths(pts[0]!.y)}`]
78-
const dx: number[] = []
79-
const dy: number[] = []
80-
const m: number[] = []
81-
const t: number[] = []
82-
83-
for (let i = 0; i < n; i++) {
84-
dx[i] = pts[i + 1]!.x - pts[i]!.x
85-
dy[i] = pts[i + 1]!.y - pts[i]!.y
86-
m[i] = dx[i] === 0 ? 0 : dy[i]! / dx[i]!
87-
}
88-
89-
t[0] = m[0]!
90-
t[n] = m[n - 1]!
91-
for (let i = 1; i < n; i++) {
92-
t[i] = m[i - 1]! * m[i]! <= 0 ? 0 : (2 * m[i - 1]! * m[i]!) / (m[i - 1]! + m[i]!)
93-
}
94-
95-
for (let i = 0; i < n; i++) {
96-
const x0 = pts[i]!.x,
97-
y0 = pts[i]!.y
98-
const x1 = pts[i + 1]!.x,
99-
y1 = pts[i + 1]!.y
100-
const seg = x1 - x0
101-
out.push(
102-
`C ${roundToHundredths(x0 + seg / 3)},${roundToHundredths(y0 + (t[i]! * seg) / 3)} ${roundToHundredths(x1 - seg / 3)},${roundToHundredths(y1 - (t[i + 1]! * seg) / 3)} ${roundToHundredths(x1)},${roundToHundredths(y1)}`,
103-
)
104-
}
105-
106-
return out.join(' ')
107-
}
108-
10969
const npmDailyRangeCache = import.meta.client ? new Map<string, Promise<DailyRawPoint[]>>() : null
11070
const likesEvolutionCache = import.meta.client ? new Map<string, Promise<DailyRawPoint[]>>() : null
11171
const contributorsEvolutionCache = import.meta.client

test/nuxt/components/OgImagePackage.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ vi.mock('~/composables/useCharts', () => ({
2626
useCharts: vi.fn().mockReturnValue({
2727
fetchPackageDownloadEvolution: mockFetchPackageDownloadEvolution,
2828
}),
29-
smoothPath: vi.fn().mockReturnValue(''),
3029
}))
3130

3231
import OgImagePackage from '~/components/OgImage/Package.takumi.vue'

0 commit comments

Comments
 (0)