underscore.inflections
Port of ActiveSupport::Inflector::Inflections for underscore.js
Last updated 7 years ago by geetarista .
Repository · Original npm · Tarball · package.json
$ cnpm install underscore.inflections 
SYNC missed versions from official npm registry.

underscore.inflections Build Status

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:

<script src="underscore.js" type="text/javascript"></script>
<script src="underscore.inflections.js" type="text/javascript"></script>

Node.js

First, install the mixin through npm:

npm install underscore.inflections

Require underscore.js and underscore.inflections:

var _ = require('underscore');
_.mixing(require('underscore.inflections'));

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:

_.singularize('posts');    //=> 'post'
_.singularize('octopi');   //=> 'octopus'
_.singularize('sheep');    //=> 'sheep'
_.singularize('words');    //=> 'words'

Pluralize

Converts a word to its pluralized form.

Examples:

_.singularize('post');      //=> 'posts'
_.singularize('octopus');   //=> 'octopi'
_.singularize('sheep');     //=> 'sheep'
_.singularize('words');     //=> 'words'

Customizing

Singular

Adds a rule for singularizing a word.

Example:

_.singular(/^(ox)en/i, '\1');

Plural

Adds a rule for pluralizing a word.

Example:

_.plural(/^(ox)$/i, '\1en');

Irregular

Adds a rule for an irregular word.

Example:

_.irregular('person', 'people');

Uncountable

Adds a rule for an uncountable word or words.

Example:

_.uncountable(['fish', 'sheep']);

License

MIT. See LICENSE.

Current Tags

  • 0.2.1                                ...           latest (7 years ago)

8 Versions

  • 0.2.1                                ...           7 years ago
  • 0.2.0                                ...           7 years ago
  • 0.1.5                                ...           7 years ago
  • 0.1.4                                ...           8 years ago
  • 0.1.3                                ...           8 years ago
  • 0.1.2                                ...           8 years ago
  • 0.1.1                                ...           8 years ago
  • 0.1.0                                ...           8 years ago

Copyright 2014 - 2016 © taobao.org |