philiprehberger-pluralize

Tests Gem Version Last updated

Standalone English pluralization, singularization, and string inflection

Requirements

  • Ruby >= 3.1

Installation

Add to your Gemfile:

gem "philiprehberger-pluralize"

Or install directly:

gem install philiprehberger-pluralize

Usage

require "philiprehberger/pluralize"

Philiprehberger::Pluralize.plural('person')    # => "people"
Philiprehberger::Pluralize.singular('people')  # => "person"
Philiprehberger::Pluralize.count(5, 'item')    # => "5 items"

Possessive Forms

Philiprehberger::Pluralize.possessive('dog')       # => "dog's"
Philiprehberger::Pluralize.possessive('dogs')      # => "dogs'"
Philiprehberger::Pluralize.possessive('James')     # => "James'"
Philiprehberger::Pluralize.possessive('children')  # => "children's"

Hyphenated Compound Words

Philiprehberger::Pluralize.plural('mother-in-law')  # => "mothers-in-law"
Philiprehberger::Pluralize.plural('well-being')     # => "well-beings"

Ordinal Words

Philiprehberger::Pluralize.ordinal(1)   # => "first"
Philiprehberger::Pluralize.ordinal(12)  # => "twelfth"
Philiprehberger::Pluralize.ordinal(21)  # => "twenty-first"

Plural Detection

Philiprehberger::Pluralize.plural?('cats')    # => true
Philiprehberger::Pluralize.plural?('cat')     # => false
Philiprehberger::Pluralize.plural?('people')  # => true

Count with Words

Philiprehberger::Pluralize.count(5, 'item', style: :words)   # => "five items"
Philiprehberger::Pluralize.count(1, 'person', style: :words)  # => "one person"
Philiprehberger::Pluralize.count(20, 'item', style: :words)  # => "20 items"

Rails-style pluralize Helper

Philiprehberger::Pluralize.pluralize(1, 'apple')               # => "1 apple"
Philiprehberger::Pluralize.pluralize(3, 'apple')               # => "3 apples"
Philiprehberger::Pluralize.pluralize(3, 'octopus', 'octopi')   # => "3 octopi"
Philiprehberger::Pluralize.pluralize(2, 'person')              # => "2 people"

Capitalize and Pluralize

Philiprehberger::Pluralize.capitalize_and_pluralize('cat')     # => "Cats"
Philiprehberger::Pluralize.capitalize_and_pluralize('person')  # => "People"
Philiprehberger::Pluralize.capitalize_and_pluralize('child')   # => "Children"

Uncountable Detection

Philiprehberger::Pluralize.uncountable?('sheep')      # => true
Philiprehberger::Pluralize.uncountable?('equipment')  # => true
Philiprehberger::Pluralize.uncountable?('cat')        # => false

Custom Irregular Words

Philiprehberger::Pluralize.irregular('radius', 'radii')
Philiprehberger::Pluralize.plural('radius')    # => "radii"
Philiprehberger::Pluralize.singular('radii')   # => "radius"
Philiprehberger::Pluralize.irregulars          # => {"radius"=>"radii"}

Uncountable Words

Philiprehberger::Pluralize.uncountable('equipment')
Philiprehberger::Pluralize.plural('equipment')  # => "equipment"
Philiprehberger::Pluralize.uncountables         # => ["equipment"]

String Inflections

Philiprehberger::Pluralize.camel_case('hello_world')  # => "HelloWorld"
Philiprehberger::Pluralize.snake_case('HelloWorld')    # => "hello_world"
Philiprehberger::Pluralize.titleize('hello_world')     # => "Hello World"
Philiprehberger::Pluralize.humanize('user_id')         # => "User"

API

Method Description
Pluralize.plural(word) Return the plural form of a word (supports hyphenated compounds)
Pluralize.singular(word) Return the singular form of a word
Pluralize.possessive(word) Generate the possessive form of a word
Pluralize.plural?(word) Return true if the word appears to be plural
Pluralize.ordinal(n) Convert a number (1-100) to its ordinal word
Pluralize.count(n, word, style:) Format a count with singular/plural word (:numeric or :words style)
Pluralize.pluralize(n, singular, plural = nil) Rails-style: format a count with a required singular and optional explicit plural
Pluralize.capitalize_and_pluralize(word) Capitalize a word and return its plural form
Pluralize.uncountable?(word) Return true if the word is uncountable (built-in or custom)
Pluralize.irregular(singular, plural) Register a custom irregular singular/plural pair
Pluralize.uncountable(word) Register a word as uncountable
Pluralize.irregulars List custom-registered irregular pairs (frozen copy)
Pluralize.uncountables List custom-registered uncountable words (frozen copy)
Pluralize.camel_case(str) Convert an underscored or hyphenated string to PascalCase
Pluralize.snake_case(str) Convert a PascalCase or camelCase string to snake_case
Pluralize.titleize(str) Convert an underscored or hyphenated string to Title Case
Pluralize.humanize(str) Convert an underscored string to a human-readable form
Pluralize.reset! Reset all custom irregular and uncountable rules

Development

bundle install
bundle exec rspec
bundle exec rubocop

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT