Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
intl-pluralrules
Advanced tools
The intl-pluralrules npm package is a polyfill for the ECMAScript Internationalization API's PluralRules object. It provides a way to get the plural form of a number for a given locale, which is useful for internationalizing applications.
Basic Usage
This feature allows you to create an instance of Intl.PluralRules for a specific locale and use it to determine the plural form of a number.
const IntlPluralRules = require('intl-pluralrules');
const pr = new IntlPluralRules('en-US');
console.log(pr.select(1)); // 'one'
console.log(pr.select(2)); // 'other'
Using Different Locales
This feature demonstrates how to use the package with different locales. In this example, the locale is set to French (fr-FR).
const IntlPluralRules = require('intl-pluralrules');
const prFrench = new IntlPluralRules('fr-FR');
console.log(prFrench.select(1)); // 'one'
console.log(prFrench.select(2)); // 'other'
Handling Multiple Plural Categories
This feature shows how the package can handle multiple plural categories, which is particularly useful for languages like Arabic that have more than two plural forms.
const IntlPluralRules = require('intl-pluralrules');
const prArabic = new IntlPluralRules('ar-EG');
console.log(prArabic.select(0)); // 'zero'
console.log(prArabic.select(1)); // 'one'
console.log(prArabic.select(2)); // 'two'
console.log(prArabic.select(3)); // 'few'
console.log(prArabic.select(11)); // 'many'
console.log(prArabic.select(100)); // 'other'
The make-plural package provides similar functionality by generating pluralization functions for different locales. It is more customizable and can be used to generate plural rules for a wide range of languages.
The cldrjs package is a JavaScript library for using Unicode CLDR (Common Locale Data Repository) data. It provides comprehensive localization support, including pluralization rules, and is more extensive in scope compared to intl-pluralrules.
The globalize package is a library for internationalization and localization in JavaScript. It uses CLDR data and provides a wide range of features including number formatting, date and time formatting, and pluralization. It is more feature-rich compared to 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
.
npm install intl-pluralrules
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.
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'
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.FAQs
Intl.PluralRules polyfill
The npm package intl-pluralrules receives a total of 123,997 weekly downloads. As such, intl-pluralrules popularity was classified as popular.
We found that intl-pluralrules demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.