intl-messageformat-parser
Advanced tools
Comparing version 1.8.1 to 2.0.0
55
build.js
#!/usr/bin/env node | ||
const peg = require('pegjs') | ||
const tspegjs = require("ts-pegjs"); | ||
const fs = require('fs') | ||
@@ -7,9 +8,51 @@ const {outputFileSync} = require('fs-extra') | ||
// ES6 | ||
outputFileSync('src/parser.js', `export default ${peg.generate(grammar, {output: 'source'})}`) | ||
// TS | ||
const srcString = peg.generate(grammar, { | ||
plugins: [tspegjs], | ||
output: 'source', | ||
tspegjs: { | ||
customHeader: ` | ||
import { | ||
MessageFormatElement, | ||
LiteralElement, | ||
ArgumentElement, | ||
NumberElement, | ||
DateElement, | ||
TimeElement, | ||
SelectElement, | ||
PluralElement, | ||
PluralOrSelectOption, | ||
TYPE | ||
} from './types'` | ||
}, | ||
returnTypes: { | ||
argument: 'string', | ||
ws: 'string', | ||
digit: 'string', | ||
hexDigit: 'string', | ||
quoteEscapedChar: 'string', | ||
apostrophe: 'string', | ||
escape: 'string', | ||
char: 'string', | ||
chars: 'string', | ||
varName: 'string', | ||
number: 'number', | ||
start: 'MessageFormatElement[]', | ||
message: 'MessageFormatElement[]', | ||
literalElement: 'LiteralElement', | ||
argumentElement: 'ArgumentElement', | ||
selectElement: 'SelectElement', | ||
pluralElement: 'PluralElement', | ||
selectOption: 'PluralOrSelectOption', | ||
pluralOption: 'PluralOrSelectOption', | ||
simpleFormatElement: ` | ||
| NumberElement | ||
| DateElement | ||
| TimeElement | ||
` | ||
} | ||
}) | ||
// Globals | ||
outputFileSync('dist/parser.js', peg.generate(grammar, {output: 'source', format: 'globals', exportVar: 'IntlMessageFormatParser'})) | ||
const REGEX = /ParseFunction = \((.*?)\) => (any);/g | ||
// CJS | ||
outputFileSync('lib/parser.js', peg.generate(grammar, {output: 'source', format: 'commonjs'})) | ||
outputFileSync('src/parser.ts', srcString.replace(REGEX, 'ParseFunction = ($1) => MessageFormatElement[];')) |
@@ -6,2 +6,44 @@ # Change Log | ||
# [2.0.0](https://github.com/formatjs/formatjs/compare/intl-messageformat-parser@1.8.1...intl-messageformat-parser@2.0.0) (2019-07-08) | ||
### Features | ||
- **intl-messageformat-parser:** Rewrite grammar ([#112](https://github.com/formatjs/formatjs/issues/112)) ([093de35](https://github.com/formatjs/formatjs/commit/093de35)) | ||
### BREAKING CHANGES | ||
- **intl-messageformat-parser:** This completely changes the AST produced by the parser | ||
Before: | ||
``` | ||
complex_msg AST length 12567 | ||
normal_msg AST length 2638 | ||
simple_msg AST length 567 | ||
string_msg AST length 288 | ||
complex_msg x 3,405 ops/sec ±5.44% (81 runs sampled) | ||
normal_msg x 27,513 ops/sec ±2.14% (87 runs sampled) | ||
simple_msg x 113,043 ops/sec ±1.20% (89 runs sampled) | ||
string_msg x 147,838 ops/sec ±0.78% (90 runs sampled) | ||
``` | ||
After: | ||
``` | ||
complex_msg AST length 2053 | ||
normal_msg AST length 410 | ||
simple_msg AST length 79 | ||
string_msg AST length 36 | ||
complex_msg x 3,926 ops/sec ±2.37% (90 runs sampled) | ||
normal_msg x 27,641 ops/sec ±3.93% (86 runs sampled) | ||
simple_msg x 100,764 ops/sec ±5.35% (79 runs sampled) | ||
string_msg x 120,362 ops/sec ±7.11% (74 runs sampled) | ||
``` | ||
- feat: normalize hashtag token in plural | ||
- feat(intl-messageformat): adapt to new AST | ||
- feat(babel-plugin-react-intl): adapt to new AST | ||
## [1.8.1](https://github.com/formatjs/formatjs/compare/intl-messageformat-parser@1.8.0...intl-messageformat-parser@1.8.1) (2019-06-28) | ||
@@ -8,0 +50,0 @@ |
'use strict'; | ||
var parser = require('./lib/parser') | ||
var parser = require('./dist/parser') | ||
module.exports = parser | ||
module.exports['default'] = parser |
{ | ||
"name": "intl-messageformat-parser", | ||
"version": "1.8.1", | ||
"version": "2.0.0", | ||
"description": "Parses ICU Message strings into an AST via JavaScript.", | ||
"main": "index.js", | ||
"module": "src/parser.js", | ||
"main": "dist/index.js", | ||
"module": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"scripts": { | ||
"clean": "rimraf dist lib", | ||
"build": "./build.js", | ||
"benchmark": "./test/benchmark.js", | ||
"build": "./build.js && tsc && tsc -p tsconfig.cjs.json", | ||
"benchmark": "node ./test/benchmark.js", | ||
"prepublish": "npm run build", | ||
"test": "mocha --opts ../../mocha.opts tests/index.ts" | ||
"test": "TS_NODE_PROJECT='./tsconfig.cjs.json' cross-env NODE_ENV=test jest" | ||
}, | ||
@@ -43,3 +44,3 @@ "contributors": [ | ||
"homepage": "https://github.com/formatjs/formatjs", | ||
"gitHead": "60ddf374ed659ffe2f3807c3c7df411c80492761" | ||
"gitHead": "700edc75a439d52f497d3927fb0384966dd5c2b2" | ||
} |
151
README.md
@@ -5,3 +5,4 @@ # Intl MessageFormat Parser | ||
[![npm Version][npm-badge]][npm] | ||
[![npm Version](https://badgen.net/npm/v/intl-messageformat-parser)(https://www.npmjs.com/package/intl-messageformat-parser) | ||
[![size](https://badgen.net/bundlephobia/minzip/intl-messageformat-parser)](https://bundlephobia.com/result?p=intl-messageformat-parser) | ||
@@ -12,9 +13,4 @@ ## Overview | ||
This parser is written in [PEG.js][], a parser generator for JavaScript. This parser's implementation was inspired by and derived from Alex Sexton's [messageformat.js][] project. The differences from Alex's implementation are: | ||
This parser is written in [PEG.js][], a parser generator for JavaScript. | ||
- This project is standalone. | ||
- It's authored as ES6 modules compiled to CommonJS and the Bundle format for the browser. | ||
- The produced AST is more descriptive and uses recursive structures. | ||
- The keywords used in the AST match the ICU Message "spec". | ||
## Usage | ||
@@ -62,83 +58,75 @@ | ||
```json | ||
{ | ||
"type": "messageFormatPattern", | ||
"elements": [ | ||
{ | ||
"type": "messageTextElement", | ||
"value": "On " | ||
}, | ||
{ | ||
"type": "argumentElement", | ||
"id": "takenDate", | ||
"format": { | ||
"type": "dateFormat", | ||
"style": "short" | ||
} | ||
}, | ||
{ | ||
"type": "messageTextElement", | ||
"value": " " | ||
}, | ||
{ | ||
"type": "argumentElement", | ||
"id": "name", | ||
"format": null | ||
}, | ||
{ | ||
"type": "messageTextElement", | ||
"value": " took " | ||
}, | ||
{ | ||
"type": "argumentElement", | ||
"id": "numPhotos", | ||
"format": { | ||
"type": "pluralFormat", | ||
"offset": 0, | ||
"options": [ | ||
[ | ||
{ | ||
"type": 0, | ||
"value": "On " | ||
}, | ||
{ | ||
"type": 3, | ||
"style": "short", | ||
"value": "takenDate" | ||
}, | ||
{ | ||
"type": 0, | ||
"value": " " | ||
}, | ||
{ | ||
"type": 1, | ||
"value": "name" | ||
}, | ||
{ | ||
"type": 0, | ||
"value": " took " | ||
}, | ||
{ | ||
"type": 6, | ||
"pluralType": "cardinal", | ||
"value": "numPhotos", | ||
"offset": 0, | ||
"options": [ | ||
{ | ||
"id": "=0", | ||
"value": [ | ||
{ | ||
"type": "optionalFormatPattern", | ||
"selector": "=0", | ||
"value": { | ||
"type": "messageFormatPattern", | ||
"elements": [ | ||
{ | ||
"type": "messageTextElement", | ||
"value": "no photos." | ||
} | ||
] | ||
} | ||
}, | ||
"type": 0, | ||
"value": "no photos." | ||
} | ||
] | ||
}, | ||
{ | ||
"id": "=1", | ||
"value": [ | ||
{ | ||
"type": "optionalFormatPattern", | ||
"selector": "=1", | ||
"value": { | ||
"type": "messageFormatPattern", | ||
"elements": [ | ||
{ | ||
"type": "messageTextElement", | ||
"value": "one photo." | ||
} | ||
] | ||
} | ||
}, | ||
"type": 0, | ||
"value": "one photo." | ||
} | ||
] | ||
}, | ||
{ | ||
"id": "other", | ||
"value": [ | ||
{ | ||
"type": "optionalFormatPattern", | ||
"selector": "other", | ||
"value": { | ||
"type": "messageFormatPattern", | ||
"elements": [ | ||
{ | ||
"type": "messageTextElement", | ||
"value": "# photos." | ||
} | ||
] | ||
} | ||
"type": 0, | ||
"value": "# photos." | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
``` | ||
## Benchmarks | ||
``` | ||
complex_msg AST length 2053 | ||
normal_msg AST length 410 | ||
simple_msg AST length 79 | ||
string_msg AST length 36 | ||
complex_msg x 3,926 ops/sec ±2.37% (90 runs sampled) | ||
normal_msg x 27,641 ops/sec ±3.93% (86 runs sampled) | ||
simple_msg x 100,764 ops/sec ±5.35% (79 runs sampled) | ||
string_msg x 120,362 ops/sec ±7.11% (74 runs sampled) | ||
``` | ||
## License | ||
@@ -149,8 +137,5 @@ | ||
[npm]: https://www.npmjs.org/package/intl-messageformat-parser | ||
[npm-badge]: https://img.shields.io/npm/v/intl-messageformat-parser.svg?style=flat-square | ||
[icu]: http://userguide.icu-project.org/formatparse/messages | ||
[intl-mf]: https://github.com/formatjs/formatjs | ||
[peg.js]: https://pegjs.org/ | ||
[messageformat.js]: https://github.com/SlexAxton/messageformat.js | ||
[license file]: https://github.com/formatjs/formatjs/blob/master/LICENSE |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
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
330423
35
6087
138