A JavaScript port of the popular Ruby inflection library. This library provides a comprehensive set of string transformation functions for handling word inflections, case conversions, and more.
npm install inflection-js
- Pluralize and singularize words
- Convert between different case styles (camelCase, snake_case, etc.)
- Humanize strings
- Titleize strings
- And much more!
import {
pluralize,
singularize,
camelize,
underscore,
humanize,
capitalize,
dasherize,
titleize,
demodulize,
tableize,
classify,
foreignKey,
ordinalize
} from 'inflection-js';
// Pluralization
pluralize('person'); // => 'people'
pluralize('octopus'); // => 'octopuses'
// Singularization
singularize('people'); // => 'person'
singularize('octopuses'); // => 'octopus'
// Case conversion
camelize('hello_world'); // => 'helloWorld'
camelize('hello_world', true); // => 'helloWorld'
underscore('helloWorld'); // => 'hello_world'
// Humanization
humanize('hello_world'); // => 'Hello world'
humanize('hello_world', true); // => 'hello world'
// Capitalization
capitalize('hello'); // => 'Hello'
// Dasherization
dasherize('hello_world'); // => 'hello-world'
// Titleization
titleize('hello world'); // => 'Hello World'
// Module handling
demodulize('MyModule::MyClass'); // => 'MyClass'
// Table/class name conversion
tableize('MyClass'); // => 'my_classes'
classify('my_classes'); // => 'MyClass'
// Foreign key generation
foreignKey('MyClass'); // => 'my_class_id'
// Ordinalization
ordinalize('1'); // => '1st'
ordinalize('2'); // => '2nd'
ordinalize('3'); // => '3rd'
ordinalize('4'); // => '4th'
Converts a singular word to its plural form.
Converts a plural word to its singular form.
Converts an underscored string to camelCase.
Converts a camelCase string to underscore format.
Converts an underscored string to a human-readable format.
Capitalizes the first letter of a string.
Converts underscores to dashes in a string.
Converts a string to title case.
Removes module names from a string, leaving only the class name.
Converts a class name to a table name.
Converts a table name to a class name.
Creates a foreign key name from a class name.
Converts a number to its ordinal form.
MIT
Contributions are welcome! Please feel free to submit a Pull Request.