string_inflection
Crystal String Inflection
Yet another library for string inflection written in Crystal.
Installation
Add this to your application's shard.yml
:
dependencies:
string_inflection:
github: mosop/string_inflection
Usage
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"
The Case Module
Would you like shorthand? You can use Case
instead of the long name StringInflection
.
require "string_inflection/case"
Case.camel("foo bar")
String#to
The special extension String#to
makes things object-oriented.
require "string_inflection/string/to"
Then you can:
"foo bar".to.camel # => "fooBar"
If you don't intend pollute the String's namespace with the method #to, the StringInflection.define_inflector
macro helps you.
class String
StringInflection.define_inflector name: "inflectTo"
end
And you can:
"foo bar".inflectTo.camel
Mixins
You can mix the inflector methods into your own namespace.
module Case
extend StringInflection::StaticMethods
end
This code defines all the inflector methods into the Case class. So you can:
Case.camel("foo bar")
You can also define the inflector methods as instance methods.
class String
StringInflection.define_instance_methods self
end
And you can:
"foo bar".camel
Calling the StringInflection.define_instance_methods(object)
macro, the inflector methods will be defined like:
class String
def camel
StringInflection.camel(self)
end
end
Releases
- v0.1.1
- Case
- String#to
- StringInflection.define_inflector
Development
[WIP]
Contributing
- Fork it ( https://github.com/mosop/string_inflection/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- mosop - creator, maintainer