Add the following code to your project's shard.yml under:
dependencies
to use in production
- OR -
development_dependencies
to use in development
Yet another Crystal library for string inflection.
Add this to your application's shard.yml
:
dependencies:
string_inflection:
github: mosop/string_inflection
require "string_inflection"
StringInflection.camel("foo bar") # => "fooBar"
StringInflection.pascal("foo bar") # => "FooBar"
StringInflection.snake("foo bar") # => "foo_bar"
StringInflection.kebab("foo bar") # => "foo-bar"
StringInflection.plural("child") # => "children"
StringInflection.singular("data") # => "datum"
Or do you like shorthand? So you can use the Case
module.
require "string_inflection/case"
Case.camel("foo bar") # => "fooBar"
Case.pascal("foo bar") # => "FooBar"
Case.snake("foo bar") # => "foo_bar"
Case.kebab("foo bar") # => "foo-bar"
Case.plural("child") # => "children"
Case.singular("data") # => "datum"
With the :up
option, singular
/ plural
replaces a string with upper case letters.
StringInflection.plural("CHILD", up: true) # => "CHILDREN"
StringInflection.singular("DATA", up: true) # => "DATUM"
Note: :up
affects only a replaced substring.
StringInflection.plural("child", up: true) # => "childREN"
StringInflection.singular("data", up: true) # => "datUM"
The special extension String#to
makes things object-oriented.
require "string_inflection/string/to"
Then you can:
"foo bar".to.camel # => "fooBar"
"foo bar".to.pascal # => "FooBar"
"foo bar".to.snake # => "foo_bar"
"foo bar".to.kebab # => "foo-bar"
"child".to.plural # => "children"
"data".to.singular # => "datum"
[WIP]
You can define inflection methods with your own names under your own namespaces.
The handy database by Kevin Atkinson and other authors is significantly useful to generate irregular singular/plural forms. You can see the license in README.
[WIP]