[Proposal] String.capitalize word by word in a sentence

1,122 views
Skip to first unread message

an...@coletiv.com

unread,
Feb 25, 2019, 7:29:53 AM2/25/19
to elixir-lang-core
Hello there!

The String module currently provides a function to capitalize strings -> https://hexdocs.pm/elixir/String.html#capitalize/2

It works if you want to capitalize the first word in a string, but if you want to capitalize every word in a sentence you will just need to create a simple function like so (it is possible to pass the mode too):

def capitalize_per_word(string) do
String.split(string)
|> Enum.map(&String.capitalize/1) |> Enum.join(" ")
end

Since this is easily achievable by a simple function (maybe even simpler than above), does it make sense to include in the String module?

Josh Adams

unread,
Feb 25, 2019, 9:04:25 AM2/25/19
to elixir-l...@googlegroups.com
Since this is easily achievable by a simple function (maybe even simpler than above), does it make sense to include in the String module?

I really don't think it does. Seems far too specialized and easy to produce on your own as you have. Also this is more of a 'sentence' concern than a 'string' concern imo.

an...@coletiv.com

unread,
Feb 25, 2019, 9:37:21 AM2/25/19
to elixir-lang-core
I tend to agree with you Josh, that was my initial thought and still is.

On the other hand, I have a utility function that performs a type of capitalization. At first glance, I thought the mode parameter was related to the capitalization type and not the unicode.

The function is specialized but not necessarily to my project domain. In our Elixir projects we are starting to handle more and more text and this will come in handy, maybe we will create a Text module with text specific functionalities.

To Swift (OO) capitalization is the first letter of every word (https://developer.apple.com/documentation/foundation/nsstring/1416784-capitalized) but in other languages like Kotlin (OO) or Scala the functionality is the same as Elixir.

Paul Clegg

unread,
Feb 25, 2019, 11:51:39 AM2/25/19
to elixir-l...@googlegroups.com
Rails added "titleize" (which does the Cap Every Word thing) -- because it's really useful in that UI space, but it hasn't gone back into Ruby proper.  Might be worth just creating a package of text manipulations that can be readily shared/reused and go that route.

...Paul



--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/576db5c3-2b9a-4e69-bc3e-b8d939d13418%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Augie De Blieck

unread,
Feb 25, 2019, 4:21:41 PM2/25/19
to elixir-l...@googlegroups.com
Rails does it wrong.  At least, by AP Title Case rules, it does it wrong. 

Not every word in a headline should be capitalized. 

So it all depends on whether you want every word in a string capitalized or you want a sentence changed to Title Case. 

There’s a great tool for the latter here:


Pedantically yours,
-Augie

Sent from my iPhone

Paul Clegg

unread,
Feb 25, 2019, 6:09:42 PM2/25/19
to elixir-l...@googlegroups.com
Yeah, English rules of grammar are not just "Cap Every Word".  I couldn't remember if Rails' titleize was that smart or not.  Guess not.

All the more reason to have a library so you can CapEveryWord or Titleize and have different outcomes.

...Paul



Xavier Noria

unread,
Feb 26, 2019, 2:55:01 AM2/26/19
to elixir-l...@googlegroups.com
Active Support's titleize does a bit more than capitalizing each word, but its purpose obviously is not to produce "correct headings for the English language", because that would be a whole and ambitious project in itself. Titleize/titlecase is just a convenience helper that you have to see in the context of the whole suite of inflectors: camelize, underscore, support for acronyms, special treatment of _id suffixes, etc.

To produce headings in English you would probably need to be able to parse the grammar and write something way more sophisticated. Even with a grammar parser, there is not event one set of rules for English. For example, when I looked into this years ago to define guidelines for the Rails guides I picked one specific set of rules:

    When writing headings, capitalize all words except for prepositions, conjunctions, internal articles, and forms of the verb "to be"

But that was may choice and it is not unique for English, so any "titleize" that aims to have those semantics... it is going to be non trivial.

In addition to that, in Catalan or Spanish a title does not even have special case rules, you should be able to capitalize as in standard text. And I bet this is a rabbit hole as soon as you take into account there are more languages than English.

Summary, I don't see this in Elixir core at all.

André Silva

unread,
Feb 26, 2019, 7:12:28 AM2/26/19
to elixir-lang-core
The initial idea is to capitalize every first letter of every word without taking into account any language consideration like what titleize does, that would be on another level and would definitely fall on its own Text package like some suggested.

My proposal discussion is if it makes sense to have two types (or more) of capitalization:

1. Default - begining of string
2. Word - for each word inside string

But as Josh mentioned, the second seems to be more related to a sentence concept and not a string concept and I agree.

I brought the discussion up because some languages like Swift, offer the capitalize per word within the String structure.
Reply all
Reply to author
Forward
0 new messages