Convert strings to different cases.
Strings::Case provides string case conversions for Strings utilities.
- No monkey-patching String class
- Convert strings to many common cases
- Support for Unicode characters
- Preserve acronyms
Add this line to your application's Gemfile:
gem "strings-case"And then execute:
$ bundle
Or install it yourself as:
$ gem install strings-case
Start by creating an instance of the Strings::Case class:
strings = Strings::Case.newThen, use one of many case conversion methods, for example,
the snakecase method:
strings.snakecase("FooBarBaz")
# => "foo_bar_baz"As a convenience, case conversion methods are also available on class:
Strings::Case.snakecase("FooBarBaz")
# => "foo_bar_baz"Case conversion methods will transform any string into an expected case:
strings.snakecase("supports IPv6 on iOS?")
# => "supports_i_pv6_on_i_os"The methods also support converting Unicode characters:
strings.snakecase("ЗдравствуйтеПривет")
# => "здравствуйте_привет"To preserve acronyms for all case conversions, configure them once on an instance:
strings.configure do |config|
config.acronym "IPv6"
config.acronym "iOS"
endThis will preserve acronyms for any case conversion method:
strings.snakecase("supports IPv6 on iOS?")
# => "supports_ipv6_on_ios"Or, use the acronyms keyword in a case conversion method:
strings.snakecase("supports IPv6 on iOS?", acronyms: %w[IPv6 iOS])
# => "supports_ipv6_on_ios"Here is a quick summary of available case conversions:
| Case Type | Result |
|---|---|
camelcase("foo bar baz") |
"fooBarBaz" |
constcase("foo bar baz") |
"FOO_BAR_BAZ" |
headercase("foo bar baz") |
"Foo-Bar-Baz" |
kebabcase("foo bar baz") |
"foo-bar-baz" |
pascalcase("foo bar baz") |
"FooBarBaz" |
pathcase("foo bar baz") |
"foo/bar/baz" |
sentencecase("foo bar baz") |
"Foo bar baz" |
snakecase("foo bar baz") |
"foo_bar_baz" |
titlecase("foo bar baz") |
"Foo Bar Baz" |
Use the acronyms keyword at initialization to add acronyms for all
case conversions.
For example, to add HTTP and XML acronyms:
strings = Strings::Case.new(acronyms: %w[HTTP XML])After initialization, use the configure method to add acronyms
in a block with the acronym method:
strings.configure do |config|
config.acronym "HTTP"
config.acronym "XML"
# or config.acronym "HTTP", "XML"
endOr, use the configure method with the acronyms keyword:
strings.configure(acronyms: %w[HTTP XML])This will result in a conversion preserving acronyms:
strings.camelcase("xml_http_request")
# => "XMLHTTPRequest"Use the camelcase method to convert a string into a camel case. It will
lowercase first and capitalise all remaining words, joining them by
removing any space. For example:
strings.camelcase("PostgreSQL adapter")
# => "postgreSqlAdapter"Use the acronyms keyword to preserve acronyms:
strings.camelcase("PostgreSQL adapter", acronyms: ["PostgreSQL"])
# => "PostgreSQLAdapter"Use the constcase method to convert a string into a constant case. It will
uppercase all words and separate them with an underscore _. For example:
strings.constcase("PostgreSQL adapter")
# => "POSTGRE_SQL_ADAPTER"Use the acronyms keyword to preserve acronyms:
strings.constcase("PostgreSQL adapter", acronyms: ["PostgreSQL"])
# => "POSTGRESQL_ADAPTER"Use the headercase method to convert a string into a header case. It will
capitalise all words and separate them with a hyphen -. For example:
strings.headercase("PostgreSQL adapter")
# => "Postgre-Sql-Adapter"Use the acronyms keyword to preserve acronyms:
strings.headercase("PostgreSQL adapter", acronyms: ["PostgreSQL"])
# => "PostgreSQL-Adapter"Use the kebabcase or dashcase method to convert a string into a kebab case.
It will lowercase all words and separate them with a dash - like a words
kebab on a skewer. For example:
strings.kebabcase("PostgreSQL adapter")
# => "postgre-sql-adapter"Use the acronyms keyword to preserve acronyms:
strings.dashcase("PostgreSQL adapter", acronyms: ["PostgreSQL"])
# => "postgresql-adapter"Use the pascalcase method to convert a string into a Pascal case. It will
capitalise all words and join them by removing any space. For example:
strings.pascalcase("PostgreSQL adapter")
# => "PostgreSqlAdapter"Use the acronyms keyword to preserve acronyms:
strings.pascalcase("PostgreSQL adapter", acronyms: ["PostgreSQL"])
# => "PostgreSQLAdapter"Use the pathcase to convert a string into a path case. It will lowercase
all words and join them with a forward slash /. For example:
strings.pathcase("PostgreSQL adapter")
# => "postgre/sql/adapter"Use the acronyms keyword to preserve acronyms:
strings.pathcase("PostgreSQL adapter", acronyms: ["PostgreSQL"])
# => "postgresql/adapter"Use the separator keyword to change the default forward slash / path
separator. For example, to use backslash \ as a path separator:
strings.pathcase("PostgreSQL adapter", separator: "\\")
# => "postgre\\sql\\adapter"Use the sentencecase to convert a string into a sentence case. It will
capitalise first and lowercase all remaining words, separating them with
space. For example:
strings.sentencecase("PostgreSQL adapter")
# => "Postgre sql adapter"Use the acronyms keyword to preserve acronyms:
strings.sentencecase("PostgreSQL adapter", acronyms: ["PostgreSQL"])
# => "PostgreSQL adapter"Use the snakecase or underscore method to convert a string into
a snake case. It will lowercase all words and separate them with
an underscore _. For example:
strings.snakecase("PostgreSQL adapter")
# => "postgre_sql_adapter"Use the acronyms keyword to preserve acronyms:
strings.underscore("PostgreSQL adapter", acronyms: ["PostgreSQL"])
# => "postgresql_adapter"Use titlecase to convert a string into a title case. It will capitalise all
words and separate them with space. For example:
strings.titlecase("PostgreSQL adapter")
# => "Postgre Sql Adapter"Use the acronyms keyword to preserve acronyms:
strings.titlecase("PostgreSQL adapter", acronyms: ["PostgreSQL"])
# => "PostgreSQL Adapter"Polluting core Ruby classes globally may have unintended consequences.
Instead, consider adding the required methods to the String class
using refinements.
For example, to extend the String class with only the snakecase method:
module MyStringExt
refine String do
def snakecase(*args)
Strings::Case.snakecase(self, *args)
end
end
endThen using refinement will make the snakecase method available
for any string:
using MyStringExt
"foo bar baz".snakecase
# => "foo_bar_baz"Load Strings::Case::Extensions refinement to extend the String class
with all case conversion methods:
require "strings/case/extensions"
using Strings::Case::Extensions
"foo bar baz".camelcase
# => "fooBarBaz"
"foo bar baz".snakecase
# => "foo_bar_baz"After checking out the repo, run bin/setup to install dependencies.
Then, run rake spec to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install.
To release a new version, update the version number in version.rb, and
then run bundle exec rake release, which will create a git tag for
the version, push git commits and tags, and push the .gem file to
rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/strings-case. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Strings::Case project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
Copyright (c) 2019 Piotr Murach. See LICENSE for further details.
