Drops.Inflector (drops_inflector v0.1.0)

View Source

String 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:

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

__using__(opts)

(macro)

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"

camelize(input, opts \\ [])

@spec camelize(
  String.t() | atom(),
  keyword()
) :: String.t()

Alias for camelize_upper/1.

camelize_lower(input, opts \\ [])

@spec camelize_lower(
  String.t() | atom(),
  keyword()
) :: String.t()

Lower camelize a string.

Examples

iex> Drops.Inflector.camelize_lower("data_mapper")
"dataMapper"

iex> Drops.Inflector.camelize_lower("drops/inflector")
"drops.Inflector"

camelize_upper(input, opts \\ [])

@spec camelize_upper(
  String.t() | atom(),
  keyword()
) :: String.t()

Upper camelize a string.

Examples

iex> Drops.Inflector.camelize_upper("data_mapper")
"DataMapper"

iex> Drops.Inflector.camelize_upper("drops/inflector")
"Drops.Inflector"

classify(input, opts \\ [])

@spec classify(
  String.t() | atom(),
  keyword()
) :: String.t()

Classify a string.

Examples

iex> Drops.Inflector.classify("books")
"Book"

iex> Drops.Inflector.classify("admin.users")
"User"

dasherize(input, opts \\ [])

@spec dasherize(
  String.t() | atom(),
  keyword()
) :: String.t()

Dasherize a string.

Examples

iex> Drops.Inflector.dasherize("drops_inflector")
"drops-inflector"

demodulize(input, opts \\ [])

@spec demodulize(
  String.t() | atom(),
  keyword()
) :: String.t()

Demodulize a string.

Examples

iex> Drops.Inflector.demodulize("Drops.Inflector")
"Inflector"

iex> Drops.Inflector.demodulize("String")
"String"

foreign_key(input, opts \\ [])

@spec foreign_key(
  String.t() | atom(),
  keyword()
) :: String.t()

Creates a foreign key name.

Examples

iex> Drops.Inflector.foreign_key("Message")
"message_id"

iex> Drops.Inflector.foreign_key("Admin.User")
"user_id"

humanize(input, opts \\ [])

@spec humanize(
  String.t() | atom(),
  keyword()
) :: String.t()

Humanize a string.

Examples

iex> Drops.Inflector.humanize("drops_inflector")
"Drops inflector"

iex> Drops.Inflector.humanize("author_id")
"Author"

modulize(input, opts \\ [])

@spec modulize(
  String.t() | atom(),
  keyword()
) :: module()

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(number, opts \\ [])

@spec ordinalize(
  integer(),
  keyword()
) :: String.t()

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(input, opts \\ [])

@spec pluralize(
  String.t() | atom(),
  keyword()
) :: String.t()

Pluralize a string.

Examples

iex> Drops.Inflector.pluralize("book")
"books"

iex> Drops.Inflector.pluralize("money")
"money"

singularize(input, opts \\ [])

@spec singularize(
  String.t() | atom(),
  keyword()
) :: String.t()

Singularize a string.

Examples

iex> Drops.Inflector.singularize("books")
"book"

iex> Drops.Inflector.singularize("money")
"money"

tableize(input, opts \\ [])

@spec tableize(
  String.t() | atom(),
  keyword()
) :: String.t()

Tableize a string.

Examples

iex> Drops.Inflector.tableize("Book")
"books"

iex> Drops.Inflector.tableize("Admin.User")
"admin_users"

uncountable?(input, opts \\ [])

@spec uncountable?(
  String.t(),
  keyword()
) :: boolean()

Check if the input is an uncountable word.

Examples

iex> Drops.Inflector.uncountable?("money")
true

iex> Drops.Inflector.uncountable?("book")
false

underscore(input, opts \\ [])

@spec underscore(
  String.t() | atom(),
  keyword()
) :: String.t()

Underscore a string.

Examples

iex> Drops.Inflector.underscore("drops-inflector")
"drops_inflector"

iex> Drops.Inflector.underscore("DataMapper")
"data_mapper"