What is intl-format-cache?
The intl-format-cache npm package is a utility for caching instances of Intl formatters, such as Intl.DateTimeFormat, Intl.NumberFormat, and Intl.RelativeTimeFormat. This can improve performance by reusing formatter instances instead of creating new ones each time formatting is needed.
What are intl-format-cache's main functionalities?
Caching DateTimeFormat instances
This feature allows you to cache instances of Intl.DateTimeFormat. The code sample demonstrates how to create and reuse a DateTimeFormat instance for formatting dates in 'en-US' locale.
const { getDateTimeFormat } = require('intl-format-cache');
const dateTimeFormat = getDateTimeFormat('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
console.log(dateTimeFormat.format(new Date()));
Caching NumberFormat instances
This feature allows you to cache instances of Intl.NumberFormat. The code sample demonstrates how to create and reuse a NumberFormat instance for formatting numbers as currency in 'en-US' locale.
const { getNumberFormat } = require('intl-format-cache');
const numberFormat = getNumberFormat('en-US', { style: 'currency', currency: 'USD' });
console.log(numberFormat.format(123456.789));
Caching RelativeTimeFormat instances
This feature allows you to cache instances of Intl.RelativeTimeFormat. The code sample demonstrates how to create and reuse a RelativeTimeFormat instance for formatting relative time in 'en-US' locale.
const { getRelativeTimeFormat } = require('intl-format-cache');
const relativeTimeFormat = getRelativeTimeFormat('en-US');
console.log(relativeTimeFormat.format(-1, 'day'));
Other packages similar to intl-format-cache
intl
The intl package provides a polyfill for the ECMAScript Internationalization API, which includes Intl.DateTimeFormat, Intl.NumberFormat, and Intl.RelativeTimeFormat. Unlike intl-format-cache, it focuses on providing the functionality where it is not natively available, rather than caching instances.
formatjs
The formatjs package is a collection of libraries for internationalization in JavaScript, including react-intl and intl-messageformat. It provides more comprehensive solutions for formatting dates, numbers, and messages, but does not specifically focus on caching formatter instances like intl-format-cache.
globalize
The globalize package provides a comprehensive internationalization library based on the Unicode CLDR data. It supports formatting dates, numbers, and messages, and offers more extensive localization features compared to intl-format-cache, but does not specifically focus on caching formatter instances.
Intl Format Cache
A memoizer factory for Intl format constructors.
Overview
This is a helper package used within Yahoo's FormatJS suite. It provides a factory for creating memoizers of Intl
format constructors: Intl.NumberFormat
, Intl.DateTimeFormat
, IntlMessageFormat
, and IntlRelativeFormat
.
Creating instances of these Intl
formats is an expensive operation, and the APIs are designed such that developers should re-use format instances instead of always creating new ones. This package is simply to make it easier to create a cache of format instances of a particular type to aid in their reuse.
Under the hood, this package creates a cache key based on the arguments passed to the memoized constructor (it will even order the keys of the options
argument) it uses JSON.stringify()
to create the string key. If the runtime does not have built-in or polyfilled support for JSON
, new instances will be created each time the memoizer function is called.
Usage
This package works as an ES6 or Node.js module, in either case it has a single default export function; e.g.:
import memoizeFormatConstructor from 'intl-format-cache';
var memoizeFormatConstructor = require('intl-format-cache');
This default export is a factory function which can be passed an Intl
format constructor and it will return a memoizer that will create or reuse an Intl
format instance and return it.
var getNumberFormat = memoizeFormatConstructor(Intl.NumberFormat);
var nf1 = getNumberFormat('en');
var nf2 = getNumberFormat('en');
var nf3 = getNumberFormat('fr');
console.log(nf1 === nf2);
console.log(nf1 === nf3);
console.log(nf1.format(1000));
console.log(nf3.format(1000));
License
This software is free to use under the Yahoo! Inc. BSD license.
See the LICENSE file for license text and copyright information.