looker/underscore.inflections

Name: underscore.inflections

Owner: looker

Description: Port of ActiveSupport::Inflector::Inflections for Underscore.js

Forked from: geetarista/underscore.inflections

Created: 2017-02-14 21:04:05.0

Updated: 2017-02-14 21:04:06.0

Pushed: 2017-02-14 21:18:16.0

Homepage:

Size: 125

Language: CoffeeScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

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:

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

First, install the mixin through npm:

install underscore.inflections

Require underscore.js and underscore.inflections:

_ = require('underscore');
xin(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:

ngularize('posts');    //=> 'post'
ngularize('octopi');   //=> 'octopus'
ngularize('sheep');    //=> 'sheep'
ngularize('words');    //=> 'words'
Pluralize

Converts a word to its pluralized form.

Examples:

uralize('post');      //=> 'posts'
uralize('octopus');   //=> 'octopi'
uralize('sheep');     //=> 'sheep'
uralize('words');     //=> 'words'
Customizing
Singular

Adds a rule for singularizing a word.

Example:

ngular(/^(ox)en/i, '\1');
Plural

Adds a rule for pluralizing a word.

Example:

ural(/^(ox)$/i, '\1en');
Irregular

Adds a rule for an irregular word.

Example:

regular('person', 'people');
Uncountable

Adds a rule for an uncountable word or words.

Example:

countable(['fish', 'sheep']);
Acronym

Makes the following inflection methods aware of acronyms: .camelize, .underscore, .humanize, .titleize See inflections_test for a full specifications of the subtleties

ronym("FBI");
melize("fbi_file"); //=> 'FBIFile'
derscore("FBIFile"); //=> 'fbi_file'
Camelize

Example:

melize('make_me_tall');  //=> 'MakeMeTall'

When passed false as second parameter it does not capitalize the first word

Example:

melize('make_me_tall', false);  //=> 'makeMeTall'
Underscore

Separate camel cased strings with underscores

Example:

derscore('INeedSomeSpace');  //=> 'i_need_some_space'
Humanize

Format underscored strings for human friendly consumption

Example:

manize('i_just_want_to_be_understood');  //=> 'I just want to be understood'

You can also add humanizing rules by calling `_.human`

Example:

man(/_cnt$/,'_count');
manize('jargon_cnt');  //=> 'Jargon count'
titleize

Title case a underscored or camel cased string

Example:

tleize('three_blind_mice');  //=> 'Three Blind Mice'
tleize('JackAndJill'); //=> 'Jack And Jill'
License

MIT. See LICENSE.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.