-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvertStringToCamelCase.js
More file actions
46 lines (32 loc) · 1.22 KB
/
Copy pathconvertStringToCamelCase.js
File metadata and controls
46 lines (32 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Converts a dash or underscore-delimited string to camelCase.
* Example: "the-stealth-warrior" -> "theStealthWarrior"
*/
function toCamelCase(str) {
// Matches a hyphen or underscore followed by a word character.
// Flags: 'i' (ignore case), 'g' (global/all occurrences).
const regExp = /[-_]\w/ig;
// String.replace() calls the callback for every match found.
return str.replace(regExp, function(match) {
// 'match' will be strings like "-s" or "_w".
// .charAt(1) picks the letter after the delimiter and uppercases it.
return match.charAt(1).toUpperCase();
});
}
/**
* Refactored: Modern, concise, and handles edge cases.
*/
const toCamelCase = (str) => {
if (!str) return ""; // Guard clause for empty/null strings
// Regex matches delimiter (dash/underscore) and captures the following letter
return str.replace(/[-_]([a-z0-9])/ig, (_, letter) =>
letter.toUpperCase()
);
};
// Example Usage
console.log(toCamelCase("the-stealth-warrior")); // "theStealthWarrior"
console.log(toCamelCase("The_Stealth_Warrior")); // "TheStealthWarrior"
// Alternative solution with just two lines of code
function toCamelCase(str) {
return str.replace(/[_-]\w/gi, ch => ch[1].toUpperCase());
}