Skip to content

Commit 722f03b

Browse files
committed
Add more string utils
1 parent c1f8696 commit 722f03b

File tree

2 files changed

+103
-7
lines changed

2 files changed

+103
-7
lines changed

lib/modules/string/string.utils.test.ts

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { describe, expect, test } from "vitest";
2-
import { upperCaseFirst } from "./string.utils";
2+
import {
3+
camelCaseToSnakeCase,
4+
lowerCaseFirst,
5+
snakeCaseToCamelCase,
6+
upperCaseFirst,
7+
} from "./string.utils";
38

49
describe("upperCaseFirst", () => {
510
test("should return an empty string when input is an empty string", () => {
@@ -27,3 +32,64 @@ describe("upperCaseFirst", () => {
2732
expect(upperCaseFirst(" hello")).toBe(" hello");
2833
});
2934
});
35+
36+
describe("lowerCaseFirst", () => {
37+
test("should return an empty string when input is an empty string", () => {
38+
expect(lowerCaseFirst("")).toBe("");
39+
});
40+
41+
test("should lowercase the first letter of a single character string", () => {
42+
expect(lowerCaseFirst("A")).toBe("a");
43+
});
44+
45+
test("should lowercase the first letter of a multiple character string", () => {
46+
expect(lowerCaseFirst("Hello")).toBe("hello");
47+
});
48+
49+
test("should return the same string if the first character is already lowercase", () => {
50+
expect(lowerCaseFirst("hello")).toBe("hello");
51+
});
52+
53+
test("should handle strings with special characters correctly", () => {
54+
expect(lowerCaseFirst("!Hello")).toBe("!Hello");
55+
expect(lowerCaseFirst("123Hello")).toBe("123Hello");
56+
});
57+
58+
test("should handle strings with leading whitespace correctly", () => {
59+
expect(lowerCaseFirst(" Hello")).toBe(" Hello");
60+
});
61+
});
62+
63+
describe("camelCaseToSnakeCase", () => {
64+
test("should convert camelCase to snake_case", () => {
65+
expect(camelCaseToSnakeCase("helloWorld")).toBe("hello_world");
66+
});
67+
68+
test("should handle strings with special characters correctly", () => {
69+
expect(camelCaseToSnakeCase("helloWorld!")).toBe("hello_world!");
70+
expect(camelCaseToSnakeCase("helloWorld123")).toBe("hello_world123");
71+
});
72+
73+
test("should handle strings with leading whitespace correctly", () => {
74+
expect(camelCaseToSnakeCase(" helloWorld")).toBe(" hello_world");
75+
});
76+
});
77+
78+
describe("snakeCaseToCamelCase", () => {
79+
test("should convert snake_case to camelCase", () => {
80+
expect(snakeCaseToCamelCase("hello_world")).toBe("HelloWorld");
81+
});
82+
83+
test("should convert snake_case to lowerCamelCase", () => {
84+
expect(snakeCaseToCamelCase("hello_world", true)).toBe("helloWorld");
85+
});
86+
87+
test("should handle strings with special characters correctly", () => {
88+
expect(snakeCaseToCamelCase("hello_world!")).toBe("HelloWorld!");
89+
expect(snakeCaseToCamelCase("hello_world123")).toBe("HelloWorld123");
90+
});
91+
92+
test("should handle strings with leading whitespace correctly", () => {
93+
expect(snakeCaseToCamelCase(" hello_world")).toBe(" helloWorld");
94+
});
95+
});

lib/modules/string/string.utils.ts

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
1-
/**
2-
* Capitalize the first letter of a string
3-
*
4-
* @param str The input string
5-
* @returns The string with the first letter capitalized
6-
*/
1+
/** Capitalize the first letter of a string */
72
export function upperCaseFirst(str: string): string {
83
if (!str) return str;
94
return str.charAt(0).toUpperCase() + str.slice(1);
105
}
6+
7+
/** Lowercase the first letter of a string */
8+
export function lowerCaseFirst(str: string): string {
9+
if (typeof str !== "string") return str;
10+
return str.charAt(0).toLowerCase() + str.slice(1);
11+
}
12+
13+
/** Converts "HelloWorld" to "hello_world" */
14+
export function camelCaseToSnakeCase(str: string): string {
15+
return lowerCaseFirst(str).replace(
16+
/[A-Z]/g,
17+
(letter) => `_${letter.toLowerCase()}`
18+
);
19+
}
20+
21+
/** Converts "hello-world" or "hello_world" to "HelloWorld" */
22+
export function snakeCaseToCamelCase(
23+
str: string,
24+
lowerCamelCase = false
25+
): string {
26+
const camelCase = str.replace(/([-_][a-zA-Z])/g, (group) =>
27+
group.toUpperCase().replace("-", "").replace("_", "")
28+
);
29+
if (lowerCamelCase) return lowerCaseFirst(camelCase);
30+
return upperCaseFirst(camelCase);
31+
}
32+
33+
/** Returns a string based on the current (or given) timestamp */
34+
export function getTimeAsCharacters(timestamp = Date.now()): string {
35+
return timestamp
36+
.toString()
37+
.split("")
38+
.map((n) => (Number(n) + 10).toString(36))
39+
.join("");
40+
}

0 commit comments

Comments
 (0)