Skip to content

Commit 6c05d77

Browse files
committed
chore: Implement toUpperCamelCase
1 parent 36cc531 commit 6c05d77

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lib/util/util.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { describe, expect, test } from "bun:test";
2+
import { toUpperCamelCase } from "./util";
3+
4+
describe("toUpperCamelCase", () => {
5+
test.each([
6+
["foo/bar", "FooBar"],
7+
["foo/bar/baz", "FooBarBaz"],
8+
["foo/bar/baz/qux", "FooBarBazQux"],
9+
10+
["foo//bar", "FooBar"],
11+
["foo//bar//baz", "FooBarBaz"],
12+
["foo//bar//baz//qux", "FooBarBazQux"],
13+
14+
["foo-bar", "FooBar"],
15+
["foo-bar-baz", "FooBarBaz"],
16+
["foo-bar-baz-qux", "FooBarBazQux"],
17+
18+
["foo--bar", "FooBar"],
19+
["foo--bar--baz", "FooBarBaz"],
20+
["foo--bar--baz--qux", "FooBarBazQux"],
21+
22+
["foo_bar", "FooBar"],
23+
["foo_bar_baz", "FooBarBaz"],
24+
["foo_bar_baz_qux", "FooBarBazQux"],
25+
26+
["foo__bar", "FooBar"],
27+
["foo__bar__baz", "FooBarBaz"],
28+
["foo__bar__baz__qux", "FooBarBazQux"],
29+
])("converts %s to %s", (input, expected) => {
30+
expect(toUpperCamelCase(input)).toBe(expected);
31+
});
32+
});

lib/util/util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function toUpperCamelCase(str: string): string {
2+
return str
3+
.split(/[-_\/]/)
4+
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
5+
.join("");
6+
}

0 commit comments

Comments
 (0)