underscore.inflections
Port of ActiveSupport::Inflector::Inflections for Underscore.js.
Works with browser or Node.js.
Introduction
I created this underscore mixin after trying out every other inflection library out there. I've created this as a direct port of Rails' version as much as possible.
Note that right now, this only handles methods found in ActiveSupport::Inflector::Inflections since that's all I need right now. I may eventually split this out into separate inflector mixins that match all of ActiveSupport's.
Setup
Browser
Include both underscore.js and underscore.inflections on your page:
Node.js
First, install the mixin through npm:
npm install underscore.inflections
Require underscore.js and underscore.inflections:
var _ = ;_;
Note: When using underscore in Node's REPL, be sure to choose a variable other than _
, as that is a special symbol used for showing the last return value.
Usage
Singularize
Converts a word to its singular form.
Examples:
_; //=> 'post'_; //=> 'octopus'_; //=> 'sheep'_; //=> 'words'
Pluralize
Converts a word to its pluralized form.
Examples:
_; //=> 'posts'_; //=> 'octopi'_; //=> 'sheep'_; //=> 'words'
Customizing
Singular
Adds a rule for singularizing a word.
Example:
_;
Plural
Adds a rule for pluralizing a word.
Example:
_;
Irregular
Adds a rule for an irregular word.
Example:
_;
Uncountable
Adds a rule for an uncountable word or words.
Example:
_;
Acronym
Makes the following inflection methods aware of acronyms: _.camelize, _.underscore, _.humanize, _.titleize See inflections_test for a full specifications of the subtleties
_;_; //=> 'FBIFile'_; //=> 'fbi_file'
Camelize
Example:
_; //=> 'MakeMeTall'
When passed false as second parameter it does not capitalize the first word
Example:
_; //=> 'makeMeTall'
Underscore
Separate camel cased strings with underscores
Example:
_; //=> 'i_need_some_space'
Humanize
Format underscored strings for human friendly consumption
Example:
_; //=> 'I just want to be understood'
You can also add humanizing rules by calling _.human
Example:
_;_; //=> 'Jargon count'
titleize
Title case a underscored or camel cased string
Example:
_; //=> 'Three Blind Mice'_; //=> 'Jack And Jill'
License
MIT. See LICENSE
.