All Projects → piotrmurach → strings-case

piotrmurach / strings-case

Licence: MIT license
Convert strings between different cases.

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to strings-case

strings-ansi
Handle ANSI escape codes in strings
Stars: ✭ 17 (-73.85%)
Mutual labels:  rubygem, strings
strings-truncation
Truncate strings with fullwidth characters and ANSI codes.
Stars: ✭ 45 (-30.77%)
Mutual labels:  rubygem, strings
Str metrics
Ruby gem (native extension in Rust) providing implementations of various string metrics
Stars: ✭ 68 (+4.62%)
Mutual labels:  rubygem, strings
solidity-standard-library
Solidity standard library (Array, random, math, string)
Stars: ✭ 61 (-6.15%)
Mutual labels:  strings
dkdeploy-typo3-cms
dkdeploy-typo3-cms provides functionality for fully automated deployments targeting TYPO3 CMS applications
Stars: ✭ 16 (-75.38%)
Mutual labels:  rubygem
common
Metarhia Common Library
Stars: ✭ 55 (-15.38%)
Mutual labels:  strings
dry-logger
[WIP] Logging library
Stars: ✭ 16 (-75.38%)
Mutual labels:  rubygem
ruby-lokalise-api
Lokalise API v2 Ruby interface.
Stars: ✭ 17 (-73.85%)
Mutual labels:  rubygem
material-sass
A rubygem of Material Design for Bootstrap 4
Stars: ✭ 55 (-15.38%)
Mutual labels:  rubygem
regXwild
⏱ Superfast ^Advanced wildcards++? | Unique algorithms that was implemented on native unmanaged C++ but easily accessible in .NET via Conari (with caching of 0x29 opcodes +optimizations) etc.
Stars: ✭ 20 (-69.23%)
Mutual labels:  strings
android-localization-helper
A python script that helps you create strings.xml for all languages in different hierarchical folder(using Google Translation API)
Stars: ✭ 19 (-70.77%)
Mutual labels:  strings
sidekiq queue metrics
Records stats of each sidekiq queue and exposes APIs to retrieve them
Stars: ✭ 29 (-55.38%)
Mutual labels:  rubygem
jekyll-gzip
Generate gzipped assets and files for your Jekyll site at build time
Stars: ✭ 34 (-47.69%)
Mutual labels:  rubygem
convert
The smallest & fastest library for really easy, totally type-safe unit conversions in TypeScript & JavaScript.
Stars: ✭ 47 (-27.69%)
Mutual labels:  conversions
ocr space
Free Online OCR for Ruby - Convert images to text
Stars: ✭ 62 (-4.62%)
Mutual labels:  rubygem
honey format
Makes working with CSVs as smooth as honey.
Stars: ✭ 13 (-80%)
Mutual labels:  rubygem
tty-link
Hyperlinks in your terminal
Stars: ✭ 30 (-53.85%)
Mutual labels:  rubygem
poeditor-cli
POEditor CLI
Stars: ✭ 29 (-55.38%)
Mutual labels:  strings
ruby terraform
A simple Ruby wrapper for invoking terraform commands.
Stars: ✭ 92 (+41.54%)
Mutual labels:  rubygem
ostrich
An SMT Solver for string constraints
Stars: ✭ 18 (-72.31%)
Mutual labels:  strings
strings logo

Strings::Case

Gem Version Actions CI Build status Maintainability Coverage Status Inline docs

Convert strings to different cases.

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

Motivation

Popular solutions that deal with transforming string cases work well in simple cases.(Sorry ;-) With more complex strings you may get unexpected results:

ActiveSupport::Inflector.underscore("supports IPv6 on iOS 14.4?")
# => "supports i_pv6 on i_os 14.4?"

In contrast, Strings::Case aims to be able to transform any string to expected case:

Strings::Case.snakecase("supports IPv6 on iOS 14.4?")
# => "supports_i_pv6_on_i_os_14_4"

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

Features

  • No monkey-patching String class
  • Converts any string to specified case
  • Supports Unicode characters
  • Provides many common case transformations
  • Allows to preserve casing of acronyms

Contents

1. Usage

The Strings::Case is a class with methods for converting between string cases:

strings = Strings::Case.new
strings.snakecase("FooBarBaz")
# => "foo_bar_baz"

As a convenience, you can call methods directly on a class:

Strings::Case.snakecase("FooBarBaz")
# => "foo_bar_baz"

It will transform any string into expected case:

strings.snakecase("supports IPv6 on iOS?")
# => "supports_i_pv6_on_i_os"

You can also specify acronyms as a method parameter:

strings.snakecase("supports IPv6 on iOS?", acronyms: %w[IPv6 iOS])
# => "supports_ipv6_on_ios"

To make acronyms available for all conversions, configure them once on an instance:

strings.configure do |config|
  config.acronym "IPv6"
  config.acronym "iOS"
end

strings.snakecase("supports IPv6 on iOS?")
# => "supports_ipv6_on_ios"

It also supports converting Unicode characters:

strings.snakecase("ЗдравствуйтеПривет")
# => "здравствуйте_привет"

Here is a quick summary of available transformations:

Case Type Result
Strings::Case.camelcase("foo bar baz") "fooBarBaz"
Strings::Case.constcase("foo bar baz") "FOO_BAR_BAZ"
Strings::Case.headercase("foo bar baz") "Foo-Bar-Baz"
Strings::Case.kebabcase("foo bar baz") "foo-bar-baz"
Strings::Case.pascalcase("foo bar baz") "FooBarBaz"
Strings::Case.pathcase("foo bar baz") "foo/bar/baz"
Strings::Case.sentencecase("foo bar baz") "Foo bar baz"
Strings::Case.snakecase("foo bar baz") "foo_bar_baz"
Strings::Case.titlecase("foo bar baz") "Foo Bar Baz"

2. API

2.1 configure

To make acronyms available for all conversions, configure them once on an instance:

strings = Strings::Case.new
strings.configure do |config|
  config.acronym "HTTP"
  config.acronym "XML"

  # or config.acronym "HTTP", "XML"
end

This will result in a conversion preserving acronyms like so:

strings.camelcase("xml_http_request")
# => "XMLHTTPRequest"

2.2 camelcase

To convert a string into a camel case, that is, a case with all the words capitilized apart from the first one and compouned together without any space use camelcase method. For example:

Strings::Case.camelcase("HTTP Response Code")
# => "httpResponseCode"

To preserve the acronyms use the :acronyms option:

Strings::Case.camelcase("HTTP Response Code", acronyms: ["HTTP"])
# => "HTTPResponseCode"

2.3 constcase

To convert a string into a constant case, that is, a case with all the words uppercased and separated by underscore character use constcase. For example:

Strings::Case.constcase("HTTP Response Code")
# => "HTTP_RESPONSE_CODE"

2.4 headercase

To covert a string into a header case, that is, a case with all the words capitalized and separated by a hypen use headercase. For example:

Strings::Case.headercase("HTTP Response Code")
# => "Http-Response-Code"

To preserve the acronyms use the :acronyms option:

Strings::Case.headercase("HTTP Response Code", acronyms: ["HTTP"])
# => "HTTP-Response-Code"

2.5 kebabcase | dashcase

To convert a string into a kebab case, that is, a case with all the words lowercased and separted by a dash, like a words kebabab on a skewer, use kebabcase or dashcase methods. For example:

Strings::Case.kebabcase("HTTP Response Code")
# => "http-response-code"

To preserve the acronyms use the :acronyms option:

Strings::Case.dashcase("HTTP Response Code", acronyms: ["HTTP"])

expect(dashed).to eq("HTTP-response-code")

2.6 pascalcase

To convert a string into a pascal case, that is, a case with all the words capitilized and compounded together without a space, use pascalcase method. For example:

Strings::Case.pascalcase("HTTP Response Code")
# => "HttpResponseCode"

To preserve the acronyms use the :acronyms option:

Strings::Case.pascalcase("HTTP Response Code")
# => "HTTPResponseCode"

2.7 pathcase

To convert a string into a file path use pathcase:

Strings::Case.pathcase("HTTP Response Code")
# => "http/response/code"

To preserve the acronyms use the :acronyms option:

Strings::Case.pathcase("HTTP Response Code", acronyms: ["HTTP"])
# => "HTTP/response/code"

By default the / is used as a path separator. To change this use a :separator option. For example, on Windows the file path separator is \:

Strings::Case.pathcase("HTTP Response Code", separator: "\\")
# => "http\response\code"

2.8 sentencecase

To turn a string into a sentence use sentencecase:

Strings::Case.sentencecase("HTTP Response Code")
# => "Http response code"

To preserve the HTTP acronym use the :acronyms option:

Strings::Case.sentencecase("HTTP Response Code", acronyms: ["HTTP"])
# => "HTTP response code"

2.9 snakecase | underscore

To convert a string into a snake case by lowercasing all the characters and separating them with an _ use snakecase or underscore methods. For example:

Strings::Case.snakecase("HTTP Response Code")
# => "http_response_code"

To preserve acronyms in your string use the :acronyms option. For example:

Strings::Case.snakecase("HTTP Response Code", acronyms: ["HTTP"])
# => "HTTP_response_code"

2.10 titlecase

To convert a string into a space delimited words that have their first letter capitalized use titlecase. For example:

Strings::Case.titlecase("HTTPResponseCode")
# => "Http Response Code"

To preserve the HTTP acronym use the :acronyms option:

Strings::Case.titlecase("HTTP response code", acronyms: ["HTTP"])
# => "HTTP Response Code"

3. Extending String class

Though it is highly discouraged to pollute core Ruby classes, you can add the required methods to String class by using refinements.

For example, if you wish to only extend strings with snakecase method do:

module MyStringExt
  refine String do
    def snakecase(*args)
      Strings::Case.snakecase(self, *args)
    end
  end
end

Then snakecase method will be available for any strings where refinement is applied:

using MyStringExt

"foo bar baz".snakecase
# => "foo_bar_baz"

However, if you want to include all the Strings::Case methods, you can use provided extensions file:

require "strings/case/extensions"

using Strings::Case::Extensions

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].