Description

title case 形式规则: 1. 首个单词必然首字母大写 2. 其他单词,在 minor 列表中的,全部小写;否则,首字母大写

Solutions

Julia

using Pipe, Test


function to_title_case(sentence::String, minorWords::String="")::String
    word_list = sentence |> split
    minor_list = minorWords |> split
    if_keep_lower(index, word) = index > 1 && lowercase(word)  lowercase.(minor_list)
    deal_word(index, word) = if_keep_lower(index, word) ? lowercase(word) : titlecase(word)

    @pipe [deal_word(i, w) for (i, w)  enumerate(word_list)] |> join(_, " ")
end
#> to_title_case (generic function with 2 methods)


@testset "Unit Test" begin
    @test to_title_case("a clash of KINGS", "a an the of") == "A Clash of Kings"
    @test to_title_case("THE WIND IN THE WILLOWS", "The In") == "The Wind in the Willows"
    @test to_title_case("the quick brown fox") == "The Quick Brown Fox"
end
#> Test Summary: | Pass  Total
#> Unit Test     |    3      3
#> Test.DefaultTestSet("Unit Test", Any[], 3, false, false)

JavaScript

/**
 * @module Title-Case
 */


const toTitleCase = word =>
    word.split("")
        .map((char, i) => i === 0 ? char.toUpperCase() : char.toLowerCase())
        .join("");

console.log(toTitleCase("tHE"));



function titleCase(sentence, minorWords = "") {
    let lowerWords = sentence.split(" ").map(x => x.toLowerCase());
    let lowerMinorWords = minorWords.split(" ").map(x => x.toLowerCase());

    ifKeepLower = (word, index) => index > 0 && lowerMinorWords.indexOf(word) > -1;
    dealWord = (word, index) => ifKeepLower(word, index) ? word.toLowerCase() : toTitleCase(word);

    return lowerWords.map(dealWord).join(" ");
};

console.log(titleCase('THE WIND IN THE WILLOWS', 'The In'));
#> The
#> The Wind in the Willows