Skip to content

Commit fc6d7ea

Browse files
committed
fix: address CodeRabbit review comments
- Add index clamping in generateSparkline() to prevent floating-point edge cases - Update getRepositoryUrl() to handle string repository values (common in npm metadata) - Add unit tests for string repository handling
1 parent 7e2890e commit fc6d7ea

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

server/utils/markdown.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ export function generateSparkline(data: number[]): string {
2020
return data
2121
.map(val => {
2222
const normalized = (val - min) / range
23-
const index = Math.round(normalized * (SPARKLINE_CHARS.length - 1))
23+
// Clamp index to valid bounds to prevent floating-point edge cases
24+
const rawIndex = Math.round(normalized * (SPARKLINE_CHARS.length - 1))
25+
const index = Math.min(SPARKLINE_CHARS.length - 1, Math.max(0, rawIndex))
2426
return SPARKLINE_CHARS[index]
2527
})
2628
.join('')
@@ -64,17 +66,24 @@ function isHttpUrl(url: string): boolean {
6466
}
6567
}
6668

67-
function getRepositoryUrl(repository?: {
68-
type?: string
69-
url?: string
70-
directory?: string
71-
}): string | null {
72-
if (!repository?.url) return null
73-
let url = normalizeGitUrl(repository.url)
69+
function getRepositoryUrl(
70+
repository?:
71+
| {
72+
type?: string
73+
url?: string
74+
directory?: string
75+
}
76+
| string,
77+
): string | null {
78+
if (!repository) return null
79+
// Handle both string and object forms of repository field
80+
const repoUrl = typeof repository === 'string' ? repository : repository.url
81+
if (!repoUrl) return null
82+
let url = normalizeGitUrl(repoUrl)
7483
// Skip non-HTTP URLs after normalization
7584
if (!isHttpUrl(url)) return null
76-
// Append directory for monorepo packages
77-
if (repository.directory) {
85+
// Append directory for monorepo packages (only available in object form)
86+
if (typeof repository !== 'string' && repository.directory) {
7887
url = joinURL(`${url}/tree/HEAD`, repository.directory)
7988
}
8089
return url

test/unit/server/utils/markdown.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,40 @@ describe('markdown utils', () => {
179179
)
180180
})
181181

182+
it('handles string repository URL', () => {
183+
const pkg = createMockPkg({
184+
repository: 'https://github.com/user/repo' as any,
185+
})
186+
const version = createMockVersion()
187+
188+
const result = generatePackageMarkdown({ pkg, version })
189+
190+
expect(result).toContain('- [Repository](https://github.com/user/repo)')
191+
})
192+
193+
it('normalizes string repository with git+ prefix', () => {
194+
const pkg = createMockPkg({
195+
repository: 'git+https://github.com/user/repo.git' as any,
196+
})
197+
const version = createMockVersion()
198+
199+
const result = generatePackageMarkdown({ pkg, version })
200+
201+
expect(result).toContain('- [Repository](https://github.com/user/repo)')
202+
})
203+
204+
it('handles GitHub shorthand string repository', () => {
205+
const pkg = createMockPkg({
206+
repository: 'github:user/repo' as any,
207+
})
208+
const version = createMockVersion()
209+
210+
const result = generatePackageMarkdown({ pkg, version })
211+
212+
// github: shorthand is not a valid HTTP URL, so should be skipped
213+
expect(result).not.toContain('- [Repository]')
214+
})
215+
182216
it('includes homepage link when different from repo', () => {
183217
const pkg = createMockPkg({
184218
repository: { url: 'https://github.com/user/repo' },

0 commit comments

Comments
 (0)