Skip to content

piotrmurach/strings-case

Repository files navigation

strings logo

Strings::Case

Gem Version Actions CI Build status Maintainability Coverage Status

Convert strings to different cases.

Strings::Case provides string case conversions for Strings utilities.

Features

  • No monkey-patching String class
  • Convert strings to many common cases
  • Support for Unicode characters
  • Preserve acronyms

Installation

Add this line to your application's Gemfile:

gem "strings-case"

And then execute:

$ bundle

Or install it yourself as:

$ gem install strings-case

Contents

1. Usage

Start by creating an instance of the Strings::Case class:

strings = Strings::Case.new

Then, 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"
end

This 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"

2. API

2.1 configure

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"
end

Or, 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"

2.2 camelcase

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"

2.3 constcase

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"

2.4 headercase

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"

2.5 kebabcase | dashcase

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"

2.6 pascalcase

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"

2.7 pathcase

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"

2.8 sentencecase

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"

2.9 snakecase | underscore

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"

2.10 titlecase

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"

3. Extending String class

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
end

Then 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"

Development

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.

Contributing

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.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

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

Copyright (c) 2019 Piotr Murach. See LICENSE for further details.