Drops.Inflector (drops_inflector v0.1.0)
View SourceString inflection library for Elixir.
This module provides functions for transforming strings in various ways, including pluralization, singularization, camelization, and more.
Based on the dry-inflector Ruby library.
Configuration
You can create custom inflector modules with specific configuration using the __using__
macro:
defmodule MyInflector do
use Drops.Inflector,
plural: [
{"virus", "viruses"},
{"octopus", "octopi"}
],
singular: [
{"thieves", "thief"},
{"octopi", "octopus"}
],
uncountable: [
"drops-inflector",
"equipment"
],
acronyms: [
"API",
"XML",
"HTML"
]
end
MyInflector.pluralize("virus") # => "viruses"
MyInflector.singularize("thieves") # => "thief"
MyInflector.uncountable?("equipment") # => true
MyInflector.camelize("api_access") # => "APIAccess"
Custom inflectors have all the same functions as the main Drops.Inflector
module,
but use your custom rules in addition to the default rules. Custom rules take
precedence over default rules.
Available Functions
All inflector modules (both the main module and custom ones) provide these functions:
camelize_lower/1
- Lower camelCasecamelize_upper/1
- Upper CamelCasecamelize/1
- Alias forcamelize_upper/1
modulize/1
- Convert string to module constantclassify/1
- Convert to class namedasherize/1
- Convert underscores to dashesdemodulize/1
- Extract last part of module namehumanize/1
- Convert to human-readable formforeign_key/1
- Create foreign key nameordinalize/1
- Convert number to ordinalpluralize/1
- Convert to plural formsingularize/1
- Convert to singular formtableize/1
- Convert to table nameunderscore/1
- Convert to snake_caseuncountable?/1
- Check if word is uncountable
Summary
Functions
Macro for creating custom inflector modules with specific configuration.
Alias for camelize_upper/1.
Lower camelize a string.
Upper camelize a string.
Classify a string.
Dasherize a string.
Demodulize a string.
Creates a foreign key name.
Humanize a string.
Find a constant with the name specified in the argument string.
Ordinalize a number.
Pluralize a string.
Singularize a string.
Tableize a string.
Check if the input is an uncountable word.
Underscore a string.
Functions
Macro for creating custom inflector modules with specific configuration.
Examples
defmodule MyInflector do
use Drops.Inflector,
plural: [
{"virus", "viruses"}
],
singular: [
{"thieves", "thief"}
],
uncountable: [
"drops-inflector"
],
acronyms: [
"API"
]
end
MyInflector.pluralize("virus") # => "viruses"
MyInflector.camelize("api_access") # => "APIAccess"
Alias for camelize_upper/1.
Lower camelize a string.
Examples
iex> Drops.Inflector.camelize_lower("data_mapper")
"dataMapper"
iex> Drops.Inflector.camelize_lower("drops/inflector")
"drops.Inflector"
Upper camelize a string.
Examples
iex> Drops.Inflector.camelize_upper("data_mapper")
"DataMapper"
iex> Drops.Inflector.camelize_upper("drops/inflector")
"Drops.Inflector"
Classify a string.
Examples
iex> Drops.Inflector.classify("books")
"Book"
iex> Drops.Inflector.classify("admin.users")
"User"
Dasherize a string.
Examples
iex> Drops.Inflector.dasherize("drops_inflector")
"drops-inflector"
Demodulize a string.
Examples
iex> Drops.Inflector.demodulize("Drops.Inflector")
"Inflector"
iex> Drops.Inflector.demodulize("String")
"String"
Creates a foreign key name.
Examples
iex> Drops.Inflector.foreign_key("Message")
"message_id"
iex> Drops.Inflector.foreign_key("Admin.User")
"user_id"
Humanize a string.
Examples
iex> Drops.Inflector.humanize("drops_inflector")
"Drops inflector"
iex> Drops.Inflector.humanize("author_id")
"Author"
Find a constant with the name specified in the argument string.
Examples
iex> Drops.Inflector.modulize("String")
String
iex> Drops.Inflector.modulize("Enum")
Enum
Ordinalize a number.
Examples
iex> Drops.Inflector.ordinalize(1)
"1st"
iex> Drops.Inflector.ordinalize(2)
"2nd"
iex> Drops.Inflector.ordinalize(3)
"3rd"
iex> Drops.Inflector.ordinalize(10)
"10th"
iex> Drops.Inflector.ordinalize(23)
"23rd"
Pluralize a string.
Examples
iex> Drops.Inflector.pluralize("book")
"books"
iex> Drops.Inflector.pluralize("money")
"money"
Singularize a string.
Examples
iex> Drops.Inflector.singularize("books")
"book"
iex> Drops.Inflector.singularize("money")
"money"
Tableize a string.
Examples
iex> Drops.Inflector.tableize("Book")
"books"
iex> Drops.Inflector.tableize("Admin.User")
"admin_users"
Check if the input is an uncountable word.
Examples
iex> Drops.Inflector.uncountable?("money")
true
iex> Drops.Inflector.uncountable?("book")
false
Underscore a string.
Examples
iex> Drops.Inflector.underscore("drops-inflector")
"drops_inflector"
iex> Drops.Inflector.underscore("DataMapper")
"data_mapper"