Skip to content

solnic/drops_inflector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Elixir Drops 💦

Inflector

CI Hex pm hex.pm downloads

Inflection utils for Elixir.

Installation

def deps do
  [
    {:drops_inflector, "~> 0.1.0"}
  ]
end

Links

Basic Usage

The Drops.Inflector module provides a comprehensive set of string inflection functions:

# Pluralization and singularization
Drops.Inflector.pluralize("book")        # => "books"
Drops.Inflector.pluralize("person")      # => "people"
Drops.Inflector.singularize("books")     # => "book"
Drops.Inflector.singularize("children")  # => "child"

# Case transformations
Drops.Inflector.camelize("data_mapper")        # => "DataMapper"
Drops.Inflector.camelize_lower("data_mapper")  # => "dataMapper"
Drops.Inflector.underscore("DataMapper")       # => "data_mapper"
Drops.Inflector.dasherize("drops_inflector")   # => "drops-inflector"

# Human-friendly transformations
Drops.Inflector.humanize("drops_inflector")  # => "Drops inflector"
Drops.Inflector.humanize("author_id")        # => "Author"

# Database and class name transformations
Drops.Inflector.tableize("User")           # => "users"
Drops.Inflector.classify("admin_users")    # => "AdminUser"
Drops.Inflector.foreign_key("Message")     # => "message_id"

# Module operations
Drops.Inflector.demodulize("Drops.Inflector")  # => "Inflector"
Drops.Inflector.modulize("String")             # => String

# Number ordinalization
Drops.Inflector.ordinalize(1)   # => "1st"
Drops.Inflector.ordinalize(22)  # => "22nd"
Drops.Inflector.ordinalize(103) # => "103rd"

# Check if a word is uncountable
Drops.Inflector.uncountable?("money")       # => true
Drops.Inflector.uncountable?("information") # => true
Drops.Inflector.uncountable?("book")        # => false

Custom Inflectors

You can create custom inflector modules with specific rules using the use macro. Custom rules take precedence over default rules:

defmodule MyInflector do
  use Drops.Inflector,
    plural: [
      {"virus", "viruses"},
      {"octopus", "octopi"}
    ],
    singular: [
      {"thieves", "thief"},
      {"octopi", "octopus"}
    ],
    uncountable: [
      "equipment",
      "software"
    ],
    acronyms: [
      "API",
      "XML",
      "HTML"
    ]
end

# Use your custom inflector
MyInflector.pluralize("virus")     # => "viruses"
MyInflector.singularize("thieves") # => "thief"
MyInflector.uncountable?("equipment") # => true

# Acronyms are properly handled in camelization
MyInflector.camelize("api_access")     # => "APIAccess"
MyInflector.camelize_lower("xml_data") # => "xmlData"

# All other functions work the same way
MyInflector.camelize("data_mapper")    # => "DataMapper"
MyInflector.tableize("User")           # => "users"

Acronym Support

Drops.Inflector includes built-in support for common acronyms like API, JSON, HTTP, etc. You can also define custom acronyms that will be properly handled during camelization:

defmodule APIInflector do
  use Drops.Inflector,
    acronyms: ["API", "XML", "HTML", "CSS"]
end

APIInflector.camelize("api_access")        # => "APIAccess"
APIInflector.camelize("xml_http_request")  # => "XMLHTTPRequest"
APIInflector.camelize_lower("html_css")    # => "htmlCSS"

The default inflector already includes these acronyms: API, CSRF, CSV, DB, HMAC, HTTP, JSON, OpenSSL.

Multiple Custom Inflectors

You can create multiple inflectors with different rules:

defmodule MedicalInflector do
  use Drops.Inflector,
    plural: [
      {"virus", "viruses"},
      {"bacterium", "bacteria"}
    ]
end

defmodule TechInflector do
  use Drops.Inflector,
    plural: [
      {"server", "servers"},
      {"database", "databases"}
    ],
    uncountable: [
      "software",
      "hardware"
    ]
end

MedicalInflector.pluralize("virus")    # => "viruses"
TechInflector.pluralize("server")      # => "servers"
TechInflector.uncountable?("software") # => true

About

Inflection utils for Elixir

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages