Skip to content

Commit

Permalink
Convert string to camel case
Browse files Browse the repository at this point in the history
  • Loading branch information
PheRum committed Jun 15, 2024
1 parent b2f21af commit 6809022
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
12 changes: 12 additions & 0 deletions 6_kyu/Convert string to camel case/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## Convert string to camel case

https://www.codewars.com/kata/517abf86da9663f1d2000003

Complete the method/function so that it converts dash/underscore delimited words into camel casing. The first word within the output should be capitalized only if the original word was capitalized (known as Upper Camel Case, also often referred to as Pascal case). The next words should be always capitalized.

**Examples**
"the-stealth-warrior" gets converted to "theStealthWarrior"

"The_Stealth_Warrior" gets converted to "TheStealthWarrior"

"The_Stealth-Warrior" gets converted to "TheStealthWarrior"
10 changes: 10 additions & 0 deletions 6_kyu/Convert string to camel case/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { toCamelCase } from "./index";

describe("Tests", () => {
it("example", () => {
expect(toCamelCase("")).toBe("");
expect(toCamelCase("the_stealth_warrior")).toBe("theStealthWarrior");
expect(toCamelCase("The-Stealth-Warrior")).toBe("TheStealthWarrior");
expect(toCamelCase("A-B-C")).toBe("ABC");
});
});
10 changes: 10 additions & 0 deletions 6_kyu/Convert string to camel case/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function toCamelCase(str: string): string {
if (!str.length) {
return "";
}

return str
.split(new RegExp("[_\\-]"))
.map((w, index) => (index > 0 ? w[0].toUpperCase() + w.slice(1) : w))
.join("");
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

### Katas solved

`Total`: 108 \
`Total`: 109 \
`8_kyu`: 87 \
`7_kyu`: 14 \
`6_kyu`: 7 \
`6_kyu`: 8 \
`5_kyu`: 0 \
`4_kyu`: 0 \
`3_kyu`: 0 \
Expand Down

0 comments on commit 6809022

Please sign in to comment.