message-format
Advanced tools
Comparing version 1.2.1 to 2.0.0
# Changelog | ||
> **Tags:** | ||
> - [New Feature] | ||
> - [Bug Fix] | ||
> - [Spec Compliancy] | ||
> - [Breaking Change] | ||
> - [Documentation] | ||
> - [Internal] | ||
> - [Polish] | ||
## 2.0.0 | ||
* **Spec Compliancy** | ||
* `supportedLocalesOf` now matches the behavior of Intl.* apis | ||
* `format` is defined as a getter for a function, just like other Intl.* apis | ||
* Update to CLDR 28 | ||
* Removed `escape` option allowing for backslash escaping | ||
* **Breaking Change** | ||
* Can no longer get the full list of supported locales via supportedLocalesOf | ||
* Parameter order has been reversed, so `locales` is first, just like Intl.* apis | ||
* Removed `cache` option | ||
* Removed parser and printer apis, refer instead to `format-message-parse` and `format-message-print` | ||
## 1.2.1 | ||
@@ -60,2 +64,1 @@ | ||
* Use `eslint` and `jscs` for style checking. | ||
73
index.js
@@ -1,1 +0,72 @@ | ||
module.exports = require('./lib/message-format') | ||
/*! | ||
* Intl.MessageFormat prollyfill | ||
* Copyright(c) 2015 Andy VanWagoner | ||
* MIT licensed | ||
**/ | ||
'use strict' | ||
var parse = require('format-message-parse') | ||
var interpret = require('format-message-interpret') | ||
var closestSupportedLocale = interpret.closestSupportedLocale | ||
function MessageFormat (locales, pattern) { | ||
if (!(this instanceof MessageFormat)) { | ||
return new MessageFormat(locales, pattern) | ||
} | ||
var root = interpret(locales, parse(pattern)) | ||
this._internal = { | ||
locale: closestSupportedLocale(locales), | ||
format: typeof root === 'string' | ||
? function format () { return root } | ||
: root | ||
} | ||
} | ||
module.exports = MessageFormat | ||
Object.defineProperties(MessageFormat.prototype, { | ||
resolvedOptions: { | ||
configurable: true, | ||
writable: true, | ||
value: function resolvedOptions () { | ||
return { | ||
locale: this._internal.locale | ||
} | ||
} | ||
}, | ||
format: { | ||
configurable: true, | ||
get: function () { | ||
return this._internal.format | ||
} | ||
}, | ||
_internal: { | ||
configurable: true, | ||
writable: true, | ||
value: { | ||
locale: 'en', | ||
format: function format () { | ||
return '' | ||
} | ||
} | ||
} | ||
}) | ||
Object.defineProperties(MessageFormat, { | ||
supportedLocalesOf: { | ||
configurable: true, | ||
writable: true, | ||
value: function supportedLocalesOf (requestedLocales) { | ||
// if the closest match is a prefix of the requested, | ||
// and it isn't a duplicate, then it is supported | ||
return [].concat(requestedLocales || []) | ||
.filter(function (locale, i, array) { | ||
var closest = closestSupportedLocale(locale) | ||
return ( | ||
closest === locale.slice(0, closest.length) && | ||
array.indexOf(locale) === i | ||
) | ||
}) | ||
} | ||
} | ||
}) |
{ | ||
"name": "message-format", | ||
"version": "1.2.1", | ||
"version": "2.0.0", | ||
"description": "Intl.MessageFormat prollyfill supporting ICU message format", | ||
"author": "Andy VanWagoner <thetalecrafter@gmail.com> (https://thetalecrafter.com/)", | ||
"homepage": "https://github.com/format-message/format-message", | ||
"license": "MIT", | ||
"repository": "format-message/format-message", | ||
"bugs": "https://github.com/format-message/format-message/issues", | ||
"keywords": [ | ||
@@ -19,56 +24,6 @@ "i18n", | ||
], | ||
"main": "lib/message-format.js", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"babel": "^5.6.14", | ||
"babel-core": "^5.6.15", | ||
"babel-loader": "^5.3.1", | ||
"benchmark": "^1.0.0", | ||
"chai": "^3.0.0", | ||
"cldr-core": "^27.0.0", | ||
"intl": "^0.1.4", | ||
"intl-messageformat": "^1.1.0", | ||
"istanbul": "^0.3.17", | ||
"karma": "^0.12.37", | ||
"karma-chai": "^0.1.0", | ||
"karma-chrome-launcher": "^0.2.0", | ||
"karma-cli": "^0.1.0", | ||
"karma-coverage": "^0.4.2", | ||
"karma-firefox-launcher": "^0.1.4", | ||
"karma-mocha": "^0.2.0", | ||
"karma-phantomjs-launcher": "^0.2.0", | ||
"karma-safari-launcher": "^0.1.1", | ||
"karma-sourcemap-loader": "^0.3.5", | ||
"karma-webpack": "^1.5.1", | ||
"mocha": "^2.2.5", | ||
"standard": "^4.5.2", | ||
"webpack": "^1.10.1" | ||
}, | ||
"scripts": { | ||
"benchmark": "babel-node --loose=all scripts/benchmark.js", | ||
"cldr": "babel-node --loose=all scripts/cldr.js", | ||
"lint": "standard", | ||
"prepublish": "babel --loose=all src --out-dir lib && webpack -p", | ||
"test": "npm run lint && npm run test-node && npm run test-browsers", | ||
"test-browsers": "karma start --browsers Firefox,PhantomJS --single-run", | ||
"test-node": "istanbul cover _mocha -- -cG -R dot -r ./test/setup test/**/*.spec.js" | ||
}, | ||
"standard": { | ||
"ignore": [ | ||
"lib/**" | ||
] | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/format-message/message-format.git" | ||
}, | ||
"author": { | ||
"name": "Andy VanWagoner", | ||
"email": "thetalecrafter@gmail.com" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/format-message/message-format/issues" | ||
}, | ||
"homepage": "https://github.com/format-message/message-format" | ||
"dependencies": { | ||
"format-message-interpret": "^4.0.0", | ||
"format-message-parse": "^4.0.0" | ||
} | ||
} |
@@ -6,4 +6,2 @@ # message-format | ||
[![npm Version][npm-image]][npm] | ||
[![Dependency Status][deps-image]][deps] | ||
[![Build Status][build-image]][build] | ||
[![JS Standard Style][style-image]][style] | ||
@@ -21,3 +19,3 @@ | ||
var message = new MessageFormat('Hello { place }!', 'en-US'); | ||
var message = new MessageFormat('en-US', 'Hello { place }!'); | ||
var formatted = message.format({ place:'World' }); | ||
@@ -75,18 +73,3 @@ ``` | ||
### Comparison to intl-messageformat | ||
intl-messageformat is in many ways the inspiration for message-format. However, | ||
intl-messageformat deviates from the ICU standard in the way you escape special | ||
characters in the message pattern, and its pegjs generated parser is fairly | ||
large and not particularly speedy. message-format's primary goals are | ||
simplicity, compliance, and performance (both in size and speed). | ||
If you prefer backslash escaping over the standard quote, you can enable it in | ||
the options: | ||
```js | ||
new MessageFormat('message with \\{braces\\}', 'en-US', { escape:'\\' }) | ||
``` | ||
API | ||
@@ -102,3 +85,3 @@ --- | ||
new MessageFormat(pattern, [locales[, options]]) | ||
new MessageFormat(locales, pattern) | ||
``` | ||
@@ -110,7 +93,4 @@ | ||
- `locales` is a string with a BCP 47 language tag, or an array of such strings. | ||
- `pattern` is a properly formatted ICU Message Format pattern. A poorly formatted pattern will cause an `Error` to be thrown. | ||
- `locales` is an optional string with a BCP 47 language tag, or an array of such strings. | ||
- `options` is an optional object containing options that change the behavior of `MessageFormat`. | ||
- `cache` default `true`, if `true` cache the result of preparing the `format` function. The cache is shared across all instances of `MessageFormat`. | ||
- `escape` default `'`, if any other character, escape the single character following each escape character. | ||
@@ -129,23 +109,3 @@ ### `MessageFormat` instances | ||
### `Parser` | ||
```js | ||
var Parser = require('message-format/parser') | ||
var ast = Parser.parse(pattern, [options]) | ||
``` | ||
Parse a message pattern to a compact tokenization | ||
### `Printer` | ||
```js | ||
var Printer = require('message-format/printer') | ||
var pattern = Printer.print(ast, [options]) | ||
``` | ||
Pretty print the parser's output to a message pattern | ||
Examples | ||
@@ -157,3 +117,3 @@ -------- | ||
```js | ||
var message = new MessageFormat('Welcome back, {name}!'); | ||
var message = new MessageFormat('en', 'Welcome back, {name}!'); | ||
message.format({ name:'Bob' }); // "Welcome back, Bob!" | ||
@@ -176,7 +136,7 @@ message.format({ name:'Bill' }); // "Welcome back, Bill!" | ||
```js | ||
var message = new MessageFormat('This isn\'\'t a \'{simple}\' \'string\''); | ||
var message = new MessageFormat('en', 'This isn\'\'t a \'{simple}\' \'string\''); | ||
message.format(); // "This isn't a {simple} 'string'" | ||
// double quotes or backticks (ES6) make it a little easier to read | ||
message = new MessageFormat("This isn''t a '{simple}' 'string'"); | ||
message = new MessageFormat("en", "This isn''t a '{simple}' 'string'"); | ||
message.format(); // "This isn't a {simple} 'string'" | ||
@@ -188,9 +148,9 @@ ``` | ||
```js | ||
var message = new MessageFormat('You took {n,number} pictures since {d,date} {d,time}'); | ||
var message = new MessageFormat('en', 'You took {n,number} pictures since {d,date} {d,time}'); | ||
message.format({ n:4000, d:new Date() }); // "You took 4,000 pictures since Jan 1, 2015 9:33:04 AM" | ||
message = new MessageFormat('{ n, number, percent }'); | ||
message = new MessageFormat('en', '{ n, number, percent }'); | ||
message.format({ n:0.1 }); // "10%" | ||
message = new MessageFormat('{ shorty, date, short }'); | ||
message = new MessageFormat('en', '{ shorty, date, short }'); | ||
message.format({ shorty:new Date() }); // "1/1/15" | ||
@@ -202,3 +162,3 @@ ``` | ||
```js | ||
var message = new MessageFormat('{ n, selectordinal,\ | ||
var message = new MessageFormat('en', '{ n, selectordinal,\ | ||
one {#st}\ | ||
@@ -208,3 +168,3 @@ two {#nd}\ | ||
other {#th}\ | ||
} place', 'en') | ||
} place') | ||
message.format({ n:102 }) // "102nd place" | ||
@@ -219,3 +179,3 @@ ``` | ||
// using a template string for multiline, no interpolation | ||
let message = new MessageFormat(`On { date, date, short } {name} ate { | ||
let message = new MessageFormat('en', `On { date, date, short } {name} ate { | ||
numBananas, plural, | ||
@@ -249,6 +209,2 @@ =0 {no bananas} | ||
[npm-image]: https://img.shields.io/npm/v/message-format.svg | ||
[deps]: https://david-dm.org/format-message/message-format | ||
[deps-image]: https://img.shields.io/david/format-message/message-format.svg | ||
[build]: https://travis-ci.org/format-message/message-format | ||
[build-image]: https://img.shields.io/travis/format-message/message-format.svg | ||
[style]: https://github.com/feross/standard | ||
@@ -263,2 +219,2 @@ [style-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg | ||
[intl]: https://github.com/andyearnshaw/Intl.js | ||
[LICENSE]: https://github.com/format-message/message-format/blob/master/LICENSE-MIT | ||
[LICENSE]: https://github.com/format-message/format-message/blob/master/LICENSE-MIT |
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
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
0
0
10850
2
4
67
207
1
1
+ Addedformat-message-parse@^4.0.0
+ Addedformat-message-formats@4.0.0(transitive)
+ Addedformat-message-interpret@4.0.1(transitive)
+ Addedformat-message-parse@4.1.1(transitive)
+ Addedlookup-closest-locale@4.0.0(transitive)