inflected
A port of ActiveSupport's inflector to Node.js. Also usable in the browser.
Installation
Install via npm:
% npm install inflected
Or via yarn:
% yarn add inflected
The UMD build is also available on unpkg, adding a Inflector
object to the global scope.
Usage
The module exports an object with several utility functions.
var pluralize = ; // => 'Categories'
Here is the complete API reference:
pluralize
string
Returns the plural form of the word in the string.
If passed an optional locale
parameter, the word will be pluralized using rules defined for that language. By default, this parameter is set to "en".
// => 'posts' // => 'octopi' // => 'sheep' // => 'words' // => 'CamelOctopi' // => 'leyes'
singularize
string
The reverse of pluralize
, returns the singular form of a word in a string.
If passed an optional locale
parameter, the word will be singularized using rules defined for that language. By default, this parameter is set to "en".
// => 'post' // => 'octopus' // => 'sheep' // => 'word' // => 'CamelOctopus' // => 'ley'
camelize
string
By default, camelize
converts strings to UpperCamelCase. If the second argument is set to false
then camelize
produces lowerCamelCase.
// => 'FooBar' // => 'fooBar'
As a rule of thumb you can think of camelize
as the inverse of underscore
, though there are cases where that does not hold:
// => 'SslError'
underscore
string
Makes an underscored, lowercase form from the expression in the string.
// => 'foo_bar'
As a rule of thumb you can think of underscore
as the inverse of camelize
, though there are cases where that does not hold:
// => 'SslError'
humanize
string
Capitalizes the first word, turns underscores into spaces, and strips a trailing "_id" if present.
Like titleize
, this is meant for creating pretty output.
The capitalization of the first word can be turned off by setting the capitalize
option key to false
. By default, this option is true
.
// => 'Employee salary' // => 'Author' // => 'author'
titleize
string
Capitalizes all the words and replaces some characters in the string to create a nicer looking title. titleize
is meant for creating pretty output.
// => 'Man From The Boondocks' // => 'X Men: The Last Stand' // => 'The Man Without A Past' // => 'Raiders Of The Lost Ark'
tableize
string
Create the name of a table like Rails does for models to table names. This method uses the pluralize
method on the last word in the string.
// => 'raw_scaled_scorers' // => 'egg_and_hams' // => 'fancy_categories'
classify
string
Create a class name from a plural table name like Rails does for table names to models.
// => 'EggAndHam' // => 'Post'
Singular names are not handled correctly:
// => 'Busines'
dasherize
string
Replaces underscores with dashes in the string.
// => 'puni-puni'
foreignKey
string
Creates a foreign key name from a class name. separateClassNameAndIdWithUnderscore
sets whether the method should put "_" between the name and "id" (default: true
).
// => 'message_id' // => 'messageid'
ordinal
string
Returns the suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
// => 'st' // => 'nd' // => 'nd' // => 'rd' // => 'th' // => 'st'
ordinalize
string
Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
// => '1st' // => '2nd' // => '1002nd' // => '1003rd' // => '-11th' // => '-1021st'
inflections
Inflections
A singleton instance of the internal Inflections class is yielded by this function, which can then be used to specify additional inflection rules. If passed an optional locale, rules for other languages can be specified. The default locale is "en". Only rules for English are provided by this library.
;
New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the pluralization and singularization rules that is run. This guarantees that your rules run before any of the rules that may already have been loaded.
transliterate
string
Replaces non-ASCII characters with an ASCII approximation, or if none exists, a replacement character which defaults to "?".
// => 'AEroskobing'
Default approximations are provided for Western/Latin characters, e.g, "ø", "ñ", "é", "ß", etc.
This method is I18n-aware, so you can set up custom approximations for a locale. This can be useful, for example, to transliterate German's "ü" and "ö" to "ue" and "oe", or to add support for transliterating Russian to ASCII.
In order to make your custom transliterations available, you must set them using the approximate
helper function:
;
Now you can have different transliterations for each locale:
// => 'Jurgen' // => 'Juergen'
parameterize
string
Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
// => 'donald-e-knuth' // => 'donald+e+knuth'
constantify
string
Converts words (camelCased, under_scored, or dasherized) to CONSTANT_CASE.
// => 'BANK_ACCOUNT' // => 'BANK_ACCOUNT' // => 'BANK_ACCOUNT' // => 'BANK_ACCOUNT'
Contributing
Here's a quick guide:
- Fork the repo and
make install
. - Run the tests. We only take pull requests with passing tests, and it's great to know that you have a clean slate:
make test
. - Add a test for your change. Only refactoring and documentation changes require no new tests. If you are adding functionality or are fixing a bug, we need a test!
- Make the test pass.
- Push to your fork and submit a pull request.
Licence
Released under The MIT License.