Skip to content

Commit e79cb46

Browse files
committed
feat: added unslugify util
1 parent b5be3c0 commit e79cb46

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,20 @@ console.log(toKebabCase("React Testing Library")); // react-testing-library
147147
console.log(toKebabCase("Aliessa Dedase")); // aliessa-dedase
148148
```
149149

150+
### `unslugify`
151+
152+
Convert slugs to Sentence Case
153+
154+
**Usage:**
155+
156+
```typescript
157+
import { unslugify } from "largs-utils";
158+
159+
unslugify("my-awesome-title"); // "My Awesome Title"
160+
unslugify("hello_world"); // "Hello World"
161+
unslugify(" spaced--out__slug "); // "Spaced Out Slug"
162+
```
163+
150164
#### Support my enthusiasm
151165

152166
If you like this util, feel free to buy me a coffee ☕!

src/__tests__/unslugify.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { unslugify } from "../unslugify";
2+
3+
describe("unslugify", () => {
4+
it("converts kebab-case to capitalized words", () => {
5+
expect(unslugify("my-awesome-title")).toBe("My Awesome Title");
6+
});
7+
8+
it("converts snake_case to capitalized words", () => {
9+
expect(unslugify("hello_world_test")).toBe("Hello World Test");
10+
});
11+
12+
it("handles mixed separators and spacing", () => {
13+
expect(unslugify(" this--is__a_test ")).toBe("This Is A Test");
14+
});
15+
16+
it("handles single word", () => {
17+
expect(unslugify("example")).toBe("Example");
18+
});
19+
20+
it("returns empty string if input is empty", () => {
21+
expect(unslugify("")).toBe("");
22+
});
23+
});

src/unslugify.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const unslugify = (slug: string): string =>
2+
slug
3+
.replace(/[-_]/g, " ")
4+
.replace(/\s+/g, " ")
5+
.trim()
6+
.replace(
7+
/\w\S*/g,
8+
(word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
9+
);

0 commit comments

Comments
 (0)