npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@4lolo/underscore.inflection

v1.3.5

Published

ActiveSupport::Inflector, for underscore!

Downloads

10

Readme

underscore.inflection

NPM version Build Status Coverage Status

Another javascript inflector?!

I'll be the first to say it; this isn't the first port of ActiveSupport::Inflector to js. Not by a long shot. But I'll definitely take underscore mixins over extending String.prototype or other clunky implementations any day.

Also, this one has tests!

Inflections

The methods listed below are the ones you'll be using 99% of the time.

pluralize

Signature: _.pluralize(word)

pluralize pluralizes the string passed to it.

// functional style
_.pluralize('word'); // => 'words'

// object-oriented style
_('word').pluralize(); // => 'words'

It also can accept a number as the second parameter. If a number is provided, it will pluralize the word to match the number.

_('word').pluralize(0); // => 'words'
_('word').pluralize(1); // => 'word'
_('word').pluralize(1.5); // => 'words'

Optionally, you can pass true as a third parameter. If found, this will include the count with the output.

_('word').pluralize(0, true); // => '0 words'
_('word').pluralize(1, true); // => '1 word'

singularize

Signature: _.singularize(word)

singularize returns the singular version of the plural passed to it.

// functional style
_.singularize('words'); // => 'word'

// object-oriented style
_('words').singularize(); // => 'word'

gsub

Signature: _.gsub(word, rule, replacement)

gsub is a method that is just slightly different than our standard String#replace. The main differences are that it matches globally every time, and if no substitution is made it returns null. It accepts a string for word and replacement, and rule can be either a string or a regex.

// functional style
_.gsub('word', /wo/, 'ne'); // => 'nerd'

// object-oriented style
_('word').gsub(/wo/, 'ne'); // => 'nerd'

ordinalize

Signature: _.ordinalize(number)

ordinalize adds an ordinal suffix to number.

_.ordinalize(1);    // => '1st'
_.ordinalize("5");  // => '5th'
_.ordinalize(11);   // => '11th'
_.ordinalize(1033); // => '1033rd'
_.ordinalize(-15);  // => '-15th'

titleize

Signature: _.titleize( words )

titleize capitalizes the first letter of each word in the string words. It preserves the existing whitespace.

_.titleize('banana');               // => 'Banana'
_.titleize('banana potato fork');   // => 'Banana Potato Fork'
_.titleize('banana  potato\tfork'); // => 'Banana  Potato\tFork'

Configuring the Inflector

Should you ever need to configure the Inflector beyond the defaults, use these methods to do so:

plural

Signature: _.plural(rule, replacement)

plural creates a new pluralization rule for the inflector. rule can be either a string or a regex.

// functional style with explicit string
_.plural('axis', 'axes');

// object-oriented style with explicit string
_('axis').plural('axes');

// functional style with regex
_.plural(/(ax)is$/i, '$1es');

// object-oriented style with regex
_(/(ax)is$/i).plural('$1es');

singular

Signature: _.singular(rule, replacement)

singular creates a new singularization rule for the inflector. rule can be either a string or a regex.

// functional style with explicit string
_.singular('data', 'datum');

// object-oriented style with explicit string
_('data').singular('datum');

// functional style with regex
_.singular(/(t)a$/i, '$1um');

// object-oriented style with regex
_(/(t)a$/i).singular('$1um');

irregular

Signature: _.irregular(singular, plural)

irregular is a shortcut method to create both a pluralization and singularization rule for the word at the same time. You must supply both the singular form and the plural form as explicit strings.

// functional style
_.irregular('haxor', 'hax0rs!');

// object-oriented style
_('haxor').irregular('hax0rs!');

uncountable

Signature: _.uncountable(word)

uncountable creates a new uncountable rule for word. Uncountable words do not get pluralized or singularized.

// functional style
_.uncountable('equipment');

// object-oriented style
_('equipment').uncountable();

resetInflections

Signature: _.resetInflections()

resetInflections resets the inflector's rules to their initial state, clearing out any custom rules that have been added.

Thanks to...

The Rails team for ActiveSupport

The DocumentCloud team for underscore.js

These other Inflector implementations:

Though no code was taken directly from them, they deserve plenty of props for doing it before me. If underscore isn't your thing, check them out!

Contributors

    66	Jeremy Ruppel
     7	Landon Schropp
     2	Johnathon Sanders
     2	Seggy Umboh
     1	Sam Dornan
     1	Shane Riley
     1	bramski
     1	maratfakhreev
     1	Daniel Perez
     1	trevor
     1	Dayton Nolan
     1	Joseph Spens
     1	Kris Neuharth

License

MIT License