Skip to content

Commit 850618a

Browse files
committed
refactor: reusable titleCase fn
1 parent fba0dc5 commit 850618a

File tree

3 files changed

+11
-18
lines changed

3 files changed

+11
-18
lines changed

docs/app/components/BadgeGenerator.vue

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ watch([pkg, type], () => {
1111
isValid.value = true
1212
})
1313
14-
const formatLabel = (str: string) => {
15-
if (!str || typeof str !== 'string') return ''
16-
return str
17-
.split('-')
18-
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
19-
.join(' per ')
20-
}
21-
2214
const copyToClipboard = async () => {
2315
const markdown = `[![Open on npmx.dev](https://npmx.dev/api/registry/badge/${type.value}/${pkg.value})](https://npmx.dev/package/${pkg.value})`
2416
copy(markdown)
@@ -53,7 +45,7 @@ const copyToClipboard = async () => {
5345
class="w-full h-10.5 px-4 py-2 rounded-lg border border-gray-200 dark:border-white/10 bg-white dark:bg-black/20 focus:ring-2 focus:ring-emerald-500/20 focus:border-emerald-500 outline-none text-sm transition-all appearance-none cursor-pointer"
5446
>
5547
<option v-for="t in BADGE_TYPES" :key="t" :value="t" class="dark:bg-gray-900">
56-
{{ formatLabel(t) }}
48+
{{ titleCase(t) }}
5749
</option>
5850
</select>
5951
<span

docs/app/components/BadgeGeneratorParameters.vue

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,6 @@ watch([pkg, type, queryParams], () => {
5858
isValid.value = true
5959
})
6060
61-
const formatLabel = (str: string) => {
62-
if (!str || typeof str !== 'string') return ''
63-
return str
64-
.split('-')
65-
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
66-
.join(' per ')
67-
}
68-
6961
const copyToClipboard = async () => {
7062
const markdown = `[![Open on npmx.dev](${badgeUrl.value})](https://npmx.dev/package/${pkg.value})`
7163
copy(markdown)
@@ -99,7 +91,7 @@ const copyToClipboard = async () => {
9991
class="w-full h-10.5 px-4 py-2 rounded-lg border border-gray-200 dark:border-white/10 bg-white dark:bg-black/20 focus:ring-2 focus:ring-emerald-500/20 focus:border-emerald-500 outline-none text-sm transition-all appearance-none cursor-pointer"
10092
>
10193
<option v-for="t in BADGE_TYPES" :key="t" :value="t" class="dark:bg-gray-900">
102-
{{ formatLabel(t) }}
94+
{{ titleCase(t) }}
10395
</option>
10496
</select>
10597
<span

docs/shared/utils/badges.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ export const BADGE_TYPES = Object.freeze([
2121
'score',
2222
'name',
2323
] as const)
24+
25+
export type BadgeType = (typeof BADGE_TYPES)[number]
26+
27+
export function titleCase(str: string) {
28+
return str
29+
.split('-')
30+
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
31+
.join(' per ')
32+
}

0 commit comments

Comments
 (0)