Do we need String.decapitalize in the standard library?

565 views
Skip to first unread message

Andrey N. Ronin

unread,
Apr 20, 2016, 12:29:30 PM4/20/16
to elixir-lang-core
String.decapitalize when you need to convert to lowercase a first letter in the word. Maybe it's not the most desired feature but sometimes it can be useful

For example String.decapitalize("UpperCaseString") => "upperCaseString"


My use case is like that:

This is what i get from docker remote api: 

%{"Command" => "String", "Created" => 1459328030, "HostConfig" => %{"NetworkMode" => "default"} ...}

and i'm using exconstructor to convert it to the struct like this %Container{command: "String", created: 1459328030, host_config:  ...}
but right now i can't because keys in upper camel case format. But maybe i'm wrong, anyway, i think this feature can be useful.

What do you think?

Adam Kittelson

unread,
Apr 20, 2016, 12:44:24 PM4/20/16
to elixir-l...@googlegroups.com
It’s a useful feature, but I’m not sure it necessarily needs to be in the standard library. There are libraries on hex.pm that can do it, e.g. https://hex.pm/packages/inflex can do it via Inflex.camelize(some_string, :lower)

Adam

--
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/071cf9eb-90e8-4b00-b48f-7c8ca8e8327c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ben Wilson

unread,
Apr 20, 2016, 11:11:21 PM4/20/16
to elixir-lang-core
Honestly it's primary usefulness in my book is when you need to convert snake_case into thisStyleCamelCase. Right now Macro.camelize always does it ThisWay and then you have to go out of your way to make it thisWay. With decapitalize you could do snake_case |> Macro.camelize |> String.decapitalize and move on.

Just a thought.

Alexei Sholik

unread,
Apr 21, 2016, 1:39:46 AM4/21/16
to elixir-lang-core
def decapitalize(string) when is_binary(string) do
  {first, rest} = String.split_at(s, 1)
  String.downcase(first) <> rest
end

José Valim

unread,
Apr 21, 2016, 1:43:33 AM4/21/16
to elixir-l...@googlegroups.com
It would be an OK addition as it mirrors the capitalize function. Although we should likely call it "uncapitalize" instead of "decapitalize".



José Valim
Skype: jv.ptec
Founder and Director of R&D

Peter Hamilton

unread,
Apr 21, 2016, 1:52:38 AM4/21/16
to elixir-l...@googlegroups.com
Agree about naming uncapitalize. Decapitalize make me think of decapitate, or removing the head. Essentially, already captured in Kernel.tl/2 :)

Andrey N. Ronin

unread,
Apr 21, 2016, 3:01:37 AM4/21/16
to elixir-lang-core
uncapitalize is ok, but maybe more rational will be Macro.camelize(some_string, :lower) and Macro.camelize(some_string, :upper) something like in that library https://hex.pm/packages/inflex suggested by Adam Kittelson?

четверг, 21 апреля 2016 г., 9:52:38 UTC+4 пользователь Peter Hamilton написал:

Ben Wilson

unread,
Apr 21, 2016, 3:12:11 PM4/21/16
to elixir-lang-core
uncapitalize is more broadly applicable than Macro.camelize(some_string, :lower) so we definitely should at least have uncapitalize. The camelize :lower was proposed before, I don't seem to recall why it was rejected.

Adam Kittelson

unread,
Apr 21, 2016, 3:22:29 PM4/21/16
to elixir-l...@googlegroups.com
It would be an OK addition as it mirrors the capitalize function. Although we should likely call it "uncapitalize" instead of "decapitalize".

Since the String.capitalize function capitalizes the first letter and downcases the remainder of the string wouldn’t mirroring that function downcase the first letter and upcase the remainder of the string?

e.g. since String.capitalize(“UpperCaseString”) would return “Uppercasestring” then String.de/upcapitalize(“upperCaseString”) would return “uPPERCASESTRING” if it is a mirror of what capitalize does which doesn’t seem like it’d help Andrey’s use case.

(Incidentally I frequently implement a capitalize_first util function in my apps because I rarely want the remainder of my string downcased the way String.capitalize does.)

Adam


Andrey N. Ronin

unread,
Apr 22, 2016, 5:07:19 AM4/22/16
to elixir-lang-core
btw, in my use case in exconstructor there is such a function is used

camel_str = Macro.camelize(str) |> lcfirst

defp lcfirst(str) do
  first = String.slice(str, 0..0) |> String.downcase
  first <> String.slice(str, 1..-1)
end

so i fixed it so:

camel_str = Macro.camelize(str) |> lcfirst
up_camel_str = Macro.camelize(str)    
:)

and so I'm more inclined to this option Macro.camelize(:upper | :lower) but if it has already been discussed, then String.decapitalize 

and yeah, word decapitalize is more correctly reflects what the function does, since it only lowercase the first char

четверг, 21 апреля 2016 г., 23:22:29 UTC+4 пользователь Adam Kittelson написал:

Daniel Perez

unread,
Apr 22, 2016, 12:28:39 PM4/22/16
to elixir-lang-core
I am not sure this addition is a very good idea because as Adam noted,
it would probably not mirror `capitalize` and could therefore be confusing.

If we want to add this, I think that we should allow `capitalize` to accept an
option to disable the downcasing of the rest of the string and
have `uncapitalize` mirror the behavior.

Daniel Perez

Tallak Tveide

unread,
Apr 22, 2016, 4:50:44 PM4/22/16
to elixir-lang-core
I am -1 on this. It is quite easy to implement as a function, or get from a library

I think the real question is wether this function is useful for supporting a convention that does not appear in Elixir code.

Any addition adds value, but also subtracts from the sum of existing stuff. I believe th net valu hr is probably negative.

Reply all
Reply to author
Forward
0 new messages