Skip to content

Commit 6e8c4d6

Browse files
committed
feat: added camelToSentenceCase util
1 parent 92fb48a commit 6e8c4d6

File tree

5 files changed

+61
-2
lines changed

5 files changed

+61
-2
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ npm install largs-utils
1010

1111
## Functions
1212

13+
### `camelToSentenceCase`
14+
15+
Converts a camelCase or PascalCase string into a readable sentence with spaces.
16+
17+
**Usage:**
18+
19+
```typescript
20+
import { camelToSentenceCase } from "largs-utils";
21+
22+
console.log(camelToSentenceCase("helloWorld")); // "Hello World"
23+
console.log(camelToSentenceCase("ThisIsATest")); // "This Is A Test"
24+
console.log(camelToSentenceCase("JSONParser")); // "JSON Parser"
25+
console.log(camelToSentenceCase("APITestCase")); // "API Test Case"
26+
```
27+
1328
### `coercedGet`
1429

1530
Safely retrieves a value from an object with default handling.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { camelToSentenceCase } from "..";
2+
3+
describe("camelToSentenceCase", () => {
4+
test("should convert camelCase to sentence case", () => {
5+
expect(camelToSentenceCase("helloWorld")).toBe("Hello World");
6+
expect(camelToSentenceCase("thisIsATest")).toBe("This Is A Test");
7+
expect(camelToSentenceCase("convertThisFunction")).toBe(
8+
"Convert This Function"
9+
);
10+
});
11+
12+
test("should handle PascalCase correctly", () => {
13+
expect(camelToSentenceCase("HelloWorld")).toBe("Hello World");
14+
expect(camelToSentenceCase("ThisIsATest")).toBe("This Is A Test");
15+
});
16+
17+
test("should handle acronyms within words", () => {
18+
expect(camelToSentenceCase("JSONParser")).toBe("JSON Parser");
19+
expect(camelToSentenceCase("APITestCase")).toBe("API Test Case");
20+
});
21+
22+
test("should handle single words without modification", () => {
23+
expect(camelToSentenceCase("hello")).toBe("Hello");
24+
expect(camelToSentenceCase("Test")).toBe("Test");
25+
});
26+
27+
test("should handle already spaced text without adding extra spaces", () => {
28+
expect(camelToSentenceCase("Already Spaced Text")).toBe(
29+
"Already Spaced Text"
30+
);
31+
});
32+
33+
test("should handle empty strings gracefully", () => {
34+
expect(camelToSentenceCase("")).toBe("");
35+
});
36+
37+
test("should handle strings with numbers", () => {
38+
expect(camelToSentenceCase("version2Update")).toBe("Version2Update");
39+
});
40+
});

src/camelToSentenceCase.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const camelToSentenceCase = (str: string): string => {
2+
const temp = str.replace(/([A-Z])(?=[A-Z][a-z])|([a-z])(?=[A-Z])/g, "$& ");
3+
return temp.charAt(0).toUpperCase() + temp.slice(1);
4+
};

src/coercedGet.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Use this to traverse data without getting undefined errors
21
export const coercedGet = (
32
obj: Record<string, any>,
43
path: string,

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export { camelToSentenceCase } from "./camelToSentenceCase";
12
export { coercedGet } from "./coercedGet";
2-
export { shuffleArray } from "./shuffleArray";
33
export { isValidEmail } from "./isValidEmail";
4+
export { shuffleArray } from "./shuffleArray";

0 commit comments

Comments
 (0)