Skip to content

Commit 12d11ee

Browse files
add string utilities
1 parent b9f8e9b commit 12d11ee

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@curiousleaf/utils",
33
"description": "A lightweight set of utilities",
4-
"version": "1.1.3",
4+
"version": "1.1.4",
55
"license": "MIT",
66
"type": "module",
77
"author": {

src/string/string.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { lcFirst, ucFirst } from "./string"
3+
4+
describe("ucFirst", () => {
5+
test("should uppercase the first character of a string", () => {
6+
expect(ucFirst("hello")).toBe("Hello")
7+
expect(ucFirst("world")).toBe("World")
8+
})
9+
10+
test("should handle empty strings", () => {
11+
expect(ucFirst("")).toBe("")
12+
})
13+
14+
test("should handle single character strings", () => {
15+
expect(ucFirst("a")).toBe("A")
16+
expect(ucFirst("z")).toBe("Z")
17+
})
18+
19+
test("should handle non-string inputs", () => {
20+
expect(ucFirst(null as any)).toBe("")
21+
expect(ucFirst(undefined as any)).toBe("")
22+
expect(ucFirst(123 as any)).toBe("")
23+
})
24+
25+
test("should preserve the rest of the string", () => {
26+
expect(ucFirst("hello world")).toBe("Hello world")
27+
expect(ucFirst("HELLO")).toBe("HELLO")
28+
})
29+
})
30+
31+
describe("lcFirst", () => {
32+
test("should lowercase the first character of a string", () => {
33+
expect(lcFirst("Hello")).toBe("hello")
34+
expect(lcFirst("World")).toBe("world")
35+
})
36+
37+
test("should handle empty strings", () => {
38+
expect(lcFirst("")).toBe("")
39+
})
40+
41+
test("should handle single character strings", () => {
42+
expect(lcFirst("A")).toBe("a")
43+
expect(lcFirst("Z")).toBe("z")
44+
})
45+
46+
test("should handle non-string inputs", () => {
47+
expect(lcFirst(null as any)).toBe("")
48+
expect(lcFirst(undefined as any)).toBe("")
49+
expect(lcFirst(123 as any)).toBe("")
50+
})
51+
52+
test("should preserve the rest of the string", () => {
53+
expect(lcFirst("Hello World")).toBe("hello World")
54+
expect(lcFirst("hello")).toBe("hello")
55+
})
56+
})

src/string/string.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Utility functions for working with strings.
3+
*/
4+
5+
/**
6+
* Uppercases the first character in the `string`.
7+
* @param string - The string to uppercase the first character of.
8+
* @returns The string with the first character in uppercase.
9+
*/
10+
export const ucFirst = (string: string) => {
11+
if (typeof string !== "string") {
12+
return ""
13+
}
14+
15+
if (string.length === 0) {
16+
return string
17+
}
18+
19+
return string[0]?.toUpperCase() + string.slice(1)
20+
}
21+
22+
/**
23+
* Lowercases the first character in the `string`.
24+
* @param string - The string to lowercase the first character of.
25+
* @returns The string with the first character in lowercase.
26+
*/
27+
export const lcFirst = (string: string) => {
28+
if (typeof string !== "string") {
29+
return ""
30+
}
31+
32+
if (string.length === 0) {
33+
return string
34+
}
35+
36+
return string[0]?.toLowerCase() + string.slice(1)
37+
}

0 commit comments

Comments
 (0)