title case 形式规则: 1. 首个单词必然首字母大写 2. 其他单词,在 minor 列表中的,全部小写;否则,首字母大写
using Pipe, Test
function to_title_case(sentence::String, minorWords::String="")::String
= sentence |> split
word_list = minorWords |> split
minor_list 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)
/**
* @module Title-Case
*/
const toTitleCase = word =>
.split("")
word.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());
= (word, index) => index > 0 && lowerMinorWords.indexOf(word) > -1;
ifKeepLower = (word, index) => ifKeepLower(word, index) ? word.toLowerCase() : toTitleCase(word);
dealWord
return lowerWords.map(dealWord).join(" ");
;
}
console.log(titleCase('THE WIND IN THE WILLOWS', 'The In'));
#> The
#> The Wind in the Willows