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

superflected

v0.1.0

Published

A port of ActiveSupport's inflector to Node.js

Downloads

74

Readme

superflected

A port of ActiveSupport's inflector to Node.js. Also usable in the browser. Forked from inflected and modified to use TypeScript and improve performance.

Installation

Install via npm:

% npm install superflected

Or via pnpm:

% pnpm add superflected

Usage

The module exports an object with several utility functions.

import { pluralize } from "superflected";

pluralize("Category"); // => 'Categories'

Here is the complete API reference:

pluralize

pluralize(word: string, locale?: string): 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".

pluralize("post"); // => 'posts'
pluralize("octopus"); // => 'octopi'
pluralize("sheep"); // => 'sheep'
pluralize("words"); // => 'words'
pluralize("CamelOctopus"); // => 'CamelOctopi'
pluralize("ley", "es"); // => 'leyes'

singularize

singularize(word: string, locale?: string): 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".

singularize("posts"); // => 'post'
singularize("octopi"); // => 'octopus'
singularize("sheep"); // => 'sheep'
singularize("word"); // => 'word'
singularize("CamelOctopi"); // => 'CamelOctopus'
singularize("leyes", "es"); // => 'ley'

camelize

camelize(term: string, uppercaseFirstLetter?: boolean): string

By default, camelize converts strings to UpperCamelCase. If the second argument is set to false then camelize produces lowerCamelCase.

camelize("foo_bar"); // => 'FooBar'
camelize("foo_bar", false); // => '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:

camelize(underscore("SSLError")); // => 'SslError'

underscore

underscore(camelCasedWord: string): string

Makes an underscored, lowercase form from the expression in the string.

underscore("FooBar"); // => '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:

camelize(underscore("SSLError")); // => 'SslError'

humanize

humanize(lowerCaseAndUnderscoredWord: string, options?: object): 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.

humanize("employee_salary"); // => 'Employee salary'
humanize("author_id"); // => 'Author'
humanize("author_id", { capitalize: false }); // => 'author'

titleize

titleize(sentence: string): 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.

titleize("man from the boondocks"); // => 'Man From The Boondocks'
titleize("x-men: the last stand"); // => 'X Men: The Last Stand'
titleize("TheManWithoutAPast"); // => 'The Man Without A Past'
titleize("raiders_of_the_lost_ark"); // => 'Raiders Of The Lost Ark'

tableize

tableize(className: string): 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.

tableize("RawScaledScorer"); // => 'raw_scaled_scorers'
tableize("egg_and_ham"); // => 'egg_and_hams'
tableize("fancyCategory"); // => 'fancy_categories'

classify

classify(tableName: string): string

Create a class name from a plural table name like Rails does for table names to models.

classify("egg_and_hams"); // => 'EggAndHam'
classify("posts"); // => 'Post'

Singular names are not handled correctly:

classify("business"); // => 'Busines'

dasherize

dasherize(underscoredWord: string): string

Replaces underscores with dashes in the string.

dasherize("puni_puni"); // => 'puni-puni'

foreignKey

foreignKey(className: string, separateClassNameAndIdWithUnderscore?: boolean): string

Creates a foreign key name from a class name. separateClassNameAndIdWithUnderscore sets whether the method should put "_" between the name and "id" (default: true).

foreignKey("Message"); // => 'message_id'
foreignKey("Message", false); // => 'messageid'

ordinal

ordinal(number: number): 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.

ordinal(1); // => 'st'
ordinal(2); // => 'nd'
ordinal(1002); // => 'nd'
ordinal(1003); // => 'rd'
ordinal(-11); // => 'th'
ordinal(-1021); // => 'st'

ordinalize

ordinalize(number: number): string

Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.

ordinalize(1); // => '1st'
ordinalize(2); // => '2nd'
ordinalize(1002); // => '1002nd'
ordinalize(1003); // => '1003rd'
ordinalize(-11); // => '-11th'
ordinalize(-1021); // => '-1021st'

inflections

inflections([string locale])

Get the inflections object for a given locale. The result can be modified to add additional inflection rules.

import { inflections } from "superflected";
const inflect = inflections('en')
inflect.plural(/^(ox)$/i, '$1$2en');
inflect.singular /^(ox)en/i, '$1');

inflect.irregular('octopus', 'octopi');

inflect.uncountable('equipment', 'snow');

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

transliterate(sentence: string, options?: object): string

Replaces non-ASCII characters with an ASCII approximation, or if none exists, a replacement character which defaults to "?".

transliterate("Ærøskøbing"); // => '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:

import { transliterations } from "superflected";
const transliterator = transliterations("de");
transliterator.approximate("ü", "ue");
transliterator.approximate("ö", "oe");

Now you can have different transliterations for each locale:

transliterate("Jürgen"); // => 'Jurgen'
transliterate("Jürgen", { locale: "de" }); // => 'Juergen'

parameterize

parameterize(sentence: string, options?: { separator?: string, preserveCase?: boolean }): string

Replaces special characters in a string so that it may be used as part of a 'pretty' URL.

parameterize("Donald E. Knuth"); // => 'donald-e-knuth'
parameterize("Donald E. Knuth", { separator: "+" }); // => 'donald+e+knuth'

There's a preserveCase option:

parameterize("Donald E. Knuth", { preserveCase: true }); // => 'Donald-E-Knuth'

constantify

constantify(words: string): string

Converts words (camelCased, under_scored, or dasherized) to CONSTANT_CASE.

constantify("bankAccount"); // => 'BANK_ACCOUNT'
constantify("bank-account"); // => 'BANK_ACCOUNT'
constantify("bank_account"); // => 'BANK_ACCOUNT'
constantify("Bank Account"); // => 'BANK_ACCOUNT'

Caching for performance

superflected recomputes the result of any inflections each time you make them by default. If you know ahead of time that you'll be calling with particular high volume inputs, you can use the built in caching helpers to pin results in memory and avoid recomputation. This is just a built in memoizer that is as high performance as possible.

To prepopulate the cache, call populate on the function you'd like to cache.

import { camelize } from "superflected";
camelize.populate("foo_bar"); // will store the result of camelize("foo_bar") in memory
camelize("foo_bar"); // will return the cached result instead of recomputing

Contributing

Here's a quick guide:

  1. Fork the repo and pnpm install.
  2. Run the tests. We only take pull requests with passing tests, and it's great to know that you have a clean slate: pnpm test.
  3. 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!
  4. Make the test pass.
  5. Push to your fork and submit a pull request.

Licence

Released under The MIT License.