intl-format-cache
Advanced tools
Comparing version 2.0.0 to 2.0.1
{ | ||
"name": "intl-format-cache", | ||
"version": "2.0.0", | ||
"description": "Produces instances of JavaScript `Intl` formats, and caches them for reuse.", | ||
"version": "2.0.1", | ||
"description": "A memoizer factory for Intl format constructors.", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "jsnext:main": "src/memoizer.js", |
Intl Format Cache | ||
================= | ||
Produces instances of JavaScript `Intl` formats, and caches them for reuse. | ||
A memoizer factory for Intl format constructors. | ||
@@ -9,8 +9,45 @@ [![npm Version][npm-badge]][npm] | ||
Overview | ||
-------- | ||
This is an helper package used within [Yahoo's IntlJS suite][IntlJS]. It provides a cache/memoize layer for creating [`Intl`][Intl] format instances: [`IntlNumberFormat`][Intl-NF], [`Intl.DateTimeFormat`][Intl-DTF], and [`IntlMessageFormat`][Intl-MF]. | ||
This is an helper package used within [Yahoo's FormatJS suite][FormatJS]. It provides a factory for creating memoizers of [`Intl`][Intl] format constructors: [`IntlNumberFormat`][Intl-NF], [`Intl.DateTimeFormat`][Intl-DTF], [`IntlMessageFormat`][Intl-MF], and [`IntlRelativeFormat`][Intl-RF]. | ||
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.: | ||
```js | ||
// In an ES6 module. | ||
import createFormatCache from 'intl-format-cache'; | ||
``` | ||
```js | ||
// In Node. | ||
var createFormatCache = 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. | ||
```js | ||
var getNumberFormat = createFormatCache(Intl.NumberFormat); | ||
var nf1 = getNumberFormat('en'); | ||
var nf2 = getNumberFormat('en'); | ||
var nf3 = getNumberFormat('fr'); | ||
console.log(nf1 === nf2); // => true | ||
console.log(nf1 === nf3); // => fase | ||
console.log(nf1.format(1000)); // => "1,000" | ||
console.log(nf3.format(1000)); // => "1 000" | ||
``` | ||
License | ||
@@ -24,5 +61,5 @@ ------- | ||
[npm]: https://www.npmjs.org/package/intl-format-cache | ||
[npm-badge]: https://img.shields.io/npm/v/intl-format-cache.svg?style=flat | ||
[npm-badge]: https://img.shields.io/npm/v/intl-format-cache.svg?style=flat-square | ||
[david]: https://david-dm.org/yahoo/intl-format-cache | ||
[david-badge]: https://img.shields.io/david/yahoo/intl-format-cache.svg?style=flat | ||
[david-badge]: https://img.shields.io/david/yahoo/intl-format-cache.svg?style=flat-square | ||
[Intl]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl | ||
@@ -32,3 +69,4 @@ [Intl-NF]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat | ||
[Intl-MF]: https://github.com/yahoo/intl-messageformat | ||
[IntlJS]: http://intljs.io/ | ||
[Intl-RF]: https://github.com/yahoo/intl-relativeformat | ||
[FormatJS]: http://formatjs.io/ | ||
[LICENSE]: https://github.com/yahoo/intl-format-cache/blob/master/LICENSE |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11662
70
0