Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
intl-list-format
Advanced tools
A fully spec-compliant polyfill for 'Intl.ListFormat'
This is a 1:1 implementation of the Intl.ListFormat
draft spec proposal ECMA-402, or the ECMAScript® Internationalization API Specification.
The Intl.ListFormat
object is a constructor for objects that enable language-sensitive list formatting.
It is a really useful low-level primitive to build on top of which avoids the need to parse lots of CLDR raw data at the expense of your users and their internet connections.
It builds upon another member of the Intl
family: Intl.getCanonicalLocales
, so this must be polyfilled. See this section for an overview.
This implementation passes all 134 Test262 Conformance tests from the Official ECMAScript Conformance Test Suite.
Some highlights of this polyfill include:
$ npm install intl-list-format
$ yarn add intl-list-format
The polyfill will check for the existence of Intl.ListFormat
and will only be applied if the runtime doesn't already support it.
To include it, add this somewhere:
import "intl-list-format";
// Or with commonjs:
require("intl-list-format");
However, it is strongly suggested that you only include the polyfill for runtimes that don't already support Intl.ListFormat
.
One way to do so is with an async import:
if (!("ListFormat" in Intl)) {
await import("intl-list-format");
// or with commonjs:
require("intl-list-format");
}
Alternatively, you can use Polyfill.app which uses this polyfill and takes care of only loading the polyfill if needed as well as adding the language features that the polyfill depends on (See dependencies).
By default, no CLDR locale data is loaded. Instead, you decide what data you want.
To load data, you can import it via the /locale-data
subfolder that comes with the NPM package:
With ES modules:
// Load the polyfill
import "intl-list-format";
// Load data for the 'en' locale
import "intl-list-format/locale-data/en";
And naturally, it also works with commonjs:
// Load the polyfill
require("intl-list-format");
// Load data for the 'en' locale
require("intl-list-format/locale-data/en");
Remember, if you're also depending on a polyfilled version of Intl.getCanonicalLocales
, you will need to import that polyfill beforehand.
The following examples are taken directly from the original proposal
// Create a list formatter in your locale
// with default values explicitly passed in.
const lf = new Intl.ListFormat("en", {
localeMatcher: "best fit", // other values: "lookup"
type: "conjunction", // "conjunction", "disjunction" or "unit"
style: "long" // other values: "short" or "narrow"
});
lf.format(["Motorcycle", "Truck", "Car"]);
// > "Motorcycle, Truck, and Car"
const lf = new Intl.ListFormat("en");
lf.formatToParts(["Foo", "Bar", "Baz"]);
// > [
// > {type: "element", value: "Foo"},
// > {type: "literal", value: ", "},
// > {type: "element", value: "Bar"},
// > {type: "literal", value: ", and "},
// > {type: "element", value: "Baz"}
// > ]
const lf = new Intl.ListFormat("en", {type: "unit", style: "narrow"});
lf.resolvedOptions();
// > {locale: "en", style: "narrow", type: "unit"}
Intl.ListFormat.supportedLocalesOf(["foo", "bar", "en-US"]);
// > ["en-US"]
This polyfill is distributed in ES3-compatible syntax, but is using some additional APIs and language features which must be available:
Array.prototype.includes
Object.create
String.prototype.replace
Symbol.toStringTag
,WeakMap
Intl.getCanonicalLocales
For by far the most browsers, these features will already be natively available. Generally, I would highly recommend using something like Polyfill.app which takes care of this stuff automatically.
Do you want to contribute? Awesome! Please follow these recommendations.
Frederik Wessberg Twitter: @FredWessberg Lead Developer |
Become a backer and get your name, avatar, and Twitter handle listed here.
The default locale will be equal to the locale file you load first.
Nope!
MIT © Frederik Wessberg (@FredWessberg) (Website)
FAQs
A fully spec-compliant polyfill for 'Intl.ListFormat'
The npm package intl-list-format receives a total of 6,359 weekly downloads. As such, intl-list-format popularity was classified as popular.
We found that intl-list-format 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.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.