Description

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).

Examples

"the-stealth-warrior" gets converted to "theStealthWarrior"
"The_Stealth_Warrior" gets converted to "TheStealthWarrior"

Solutions

R

library(tidyverse)
toCamelCase <- function(string) {
    string %>%
        str_replace_all("[-_]\\w", function(x) {
            toupper(str_sub(x, 2))
        })
}


toCamelCase("the-stealth-warrior")
#> [1] "theStealthWarrior"

JavaScript

function toCamelCase(str) {
    // return str.replace(/[-_]\w/g, w => w[1].toUpperCase());

    // 用()分组,匹配结果返回array,第一个元素永远都是完全匹配结果,第二个元素开始是括号中pattern匹配的局部
    return str.replace(/[-_](.)/g, (_, c) => c.toUpperCase());
}