Skip to content

Commit d7316ea

Browse files
committed
feat: add strings helpers
1 parent c6fde33 commit d7316ea

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/helpers/strings.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { camelCase, capitalize, pascalCase, titleCase } from "./strings.js";
4+
5+
describe("camelCase", () => {
6+
it("should convert a string to camelCase", () => {
7+
expect(camelCase("this is a string")).toBe("thisIsAString");
8+
});
9+
});
10+
11+
describe("capitalize", () => {
12+
it("should capitalize the first letter of a string", () => {
13+
expect(capitalize("this is a string")).toBe("This is a string");
14+
});
15+
});
16+
17+
describe("pascalCase", () => {
18+
it("should convert a string to PascalCase", () => {
19+
expect(pascalCase("this is a string")).toBe("ThisIsAString");
20+
});
21+
});
22+
23+
describe("titleCase", () => {
24+
it("should convert a string to Title Case", () => {
25+
expect(titleCase("this is a string")).toBe("This Is A String");
26+
});
27+
});

src/helpers/strings.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const camelCase = (str: string): string => {
2+
return str
3+
.split(" ")
4+
.map((word, index) => {
5+
if (index === 0) {
6+
return word.toLowerCase();
7+
}
8+
return word.charAt(0).toUpperCase() + word.slice(1);
9+
})
10+
.join("");
11+
};
12+
13+
export const capitalize = (str: string): string => {
14+
return str.charAt(0).toUpperCase() + str.slice(1);
15+
};
16+
17+
export const pascalCase = (str: string): string => {
18+
return capitalize(camelCase(str));
19+
};
20+
21+
export const titleCase = (str: string): string => {
22+
return str
23+
.split(" ")
24+
.map((word) => capitalize(word))
25+
.join(" ");
26+
};

0 commit comments

Comments
 (0)