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

intl-pluralrules

v2.0.1

Published

Intl.PluralRules polyfill

Downloads

1,243,416

Readme

intl-pluralrules

A spec-compliant implementation & polyfill for Intl.PluralRules, including the selectRange(start, end) method introduced in Intl.NumberFormat v3. Also useful if you need proper support for minimumFractionDigits, which are only supported in Chrome 77 & later.

For a polyfill without selectRange() and with IE 11 support, please use intl-pluralrules@1.

Installation

npm install intl-pluralrules

Polyfill

To use as a polyfill, just import it to ensure that Intl.PluralRules is available in your environment:

import 'intl-pluralrules'

If Intl.PluralRules already exists, includes a selectRange() method, and supports multiple locales, the polyfill will not be loaded.

Ponyfill

A complete implementation of PluralRules is available as intl-pluralrules/plural-rules, if you'd prefer using it without modifying your Intl object, or if you wish to use it rather than your environment's own:

import PluralRules from 'intl-pluralrules/plural-rules'

new PluralRules('en').select(1) // 'one'
new PluralRules('en', { minimumSignificantDigits: 3 }).select(1) // 'other'
new PluralRules('en').selectRange(0, 1) // 'other'
new PluralRules('fr').selectRange(0, 1) // 'one'

Factory

In order to support all available locales, their data needs to be included in the package. This means that when minified and gzipped, the above-documented usage adds about 7kB to your application's production size. If this is a concern, you can use intl-pluralrules/factory and make-plural to build a PluralRules class with locale support limited to only what you actually use.

Thanks to tree-shaking, this example that only supports English and French minifies & gzips to 1472 bytes. Do note that this size difference is only apparent with minified production builds.

import getPluralRules from 'intl-pluralrules/factory'
import { en, fr } from 'make-plural/plurals'
import { en as enCat, fr as frCat } from 'make-plural/pluralCategories'
import { en as enRange, fr as frRange } from 'make-plural/ranges'

const sel = { en, fr }
const getSelector = lc => sel[lc]

const cat = { en: enCat, fr: frCat }
const getCategories = (lc, ord) => cat[lc][ord ? 'ordinal' : 'cardinal']

const range = { en: enRange, fr: frRange }
const getRangeSelector = lc => range[lc]

const PluralRules = getPluralRules(
  Intl.NumberFormat, // Not available in IE 10
  getSelector,
  getCategories,
  getRangeSelector
)
export default PluralRules

All arguments of getPluralRules(NumberFormat, getSelector, getCategories, getRangeSelector) are required.

  • NumberFormat should be Intl.NumberFormat, or an implementation which supports at least the "en" locale and all of the min/max digit count options.
  • getSelector(lc) should return a function(n, ord) returning the plural category of n, using cardinal plural rules (by default), or ordinal rules if ord is true. n may be a number, or the formatted string representation of a number. This may be called with any user-provided string lc, and should return undefined for invalid or unsupported locales.
  • getCategories(lc, ord) should return the set of available plural categories for the locale, either for cardinals (by default), or ordinals if ord is true. This function will be called only with values for which getSelector returns a function.
  • getRangeSelector(lc) should return a function(start, end) returning the plural category of the range. start and end are the plural categories of the corresponding values.