Produces instances of JavaScript `Intl` formats, and caches them for reuse.
Switch branches/tags
Nothing to show
Clone or download
Latest commit 5247b12 Nov 7, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Fix change from #11 Nov 23, 2015
test Remove failing-to-conenct Firefox Dev Sauce Labs config Nov 24, 2015
.gitignore Initial drop Aug 14, 2014
.npmignore Add unit tests Nov 23, 2015
.travis.yml Integrate with Sauce Labs Nov 23, 2015
Gruntfile.js Add missing sourcemap files (#13) Nov 7, 2017
LICENSE Initial commit Aug 14, 2014
README.md Tweak README Nov 24, 2015
index.js Make Browserify bundle work in ES3 environments Jan 13, 2015
package.json Bump version to v2.1.0 Nov 7, 2017

README.md

Intl Format Cache

A memoizer factory for Intl format constructors.

npm Version Build Status Dependency Status

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.:

// In an ES6 module.
import memoizeFormatConstructor from 'intl-format-cache';
// In Node.
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); // => true
console.log(nf1 === nf3); // => false

console.log(nf1.format(1000)); // => "1,000"
console.log(nf3.format(1000)); // => "1 000"

License

This software is free to use under the Yahoo! Inc. BSD license. See the LICENSE file for license text and copyright information.