# format-message-parse

> Parse ICU MessageFormat pattern strings to a compact ast

Latest version **6.2.4** (published 2022-02-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install format-message-parse
pnpm add format-message-parse
yarn add format-message-parse
bun add format-message-parse
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 6.2.4 |
| Published | 2022-02-17 |
| First published | 2015-11-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 20.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 204 |
| Author | Andy VanWagoner |
| Maintainers | thetalecrafter, bjohn465, aaronshaf, brentburgoyne |
| Keywords | format-message, message-format, internationalization, i18n, icu, messageformat, pluralformat, selectformat |

## Links

- npm: https://www.npmjs.com/package/format-message-parse
- Repository: https://github.com/format-message/format-message
- Homepage: https://github.com/format-message/format-message/tree/master/packages/format-message-parse
- Issues: https://github.com/format-message/format-message/issues
- npm.io page: https://npm.io/package/format-message-parse

## Alternatives

- [messageformat](https://npm.io/package/messageformat.md) — 329.7K weekly downloads
- [@mintlify/scraping](https://npm.io/package/@mintlify/scraping.md) — 294.8K weekly downloads
- [@mintlify/previewing](https://npm.io/package/@mintlify/previewing.md) — 209.5K weekly downloads
- [@mintlify/prebuild](https://npm.io/package/@mintlify/prebuild.md) — 209.5K weekly downloads
- [@mintlify/link-rot](https://npm.io/package/@mintlify/link-rot.md) — 206.3K weekly downloads

## Recent versions

- 6.2.4 (latest) — 2022-02-17
- 6.0.0-alpha.2 (next) — 2018-04-18
- 6.2.3 — 2019-07-04
- 6.2.2 — 2019-07-04
- 6.2.0 — 2018-09-12
- 6.1.0 — 2018-08-30
- 6.0.4 — 2018-06-05
- 6.0.3 — 2018-04-27
- 6.0.0 — 2018-04-19
- 6.0.0-alpha.1 — 2018-04-17
- 6.0.0-alpha.0 — 2018-04-16
- 5.1.2 — 2017-03-23
- 5.1.0 — 2016-12-28
- 5.0.0 — 2016-09-27
- 5.0.0-beta.10 — 2016-09-26
- … 5 more at https://npm.io/package/format-message-parse/versions

## README

# ![format-message-parse][logo]

> Parse ICU MessageFormat pattern strings to a compact ast

[![npm Version][npm-image]][npm]
[![JS Standard Style][style-image]][style]
[![MIT License][license-image]][LICENSE]

Turns a ICU Message Format string:
```js
`You have {
  numBananas, plural,
     =0 {no bananas}
    one {a banana}
  other {# bananas}
} for sale`
```

into a compact format-message ast:
```js
[ "You have ", [ "numBananas", "plural", 0, {
     "=0": [ "no bananas" ],
    "one": [ "a banana" ],
  "other": [ [ '#' ], " bananas" ]
} ], " for sale." ]
```

Quick Examples
--------------

`npm install format-message-parse --save`

```js
import parse from 'format-message-parse'
import interpret from 'format-message-interpret'

interpret('en', parse('Hello, {name}!'))({ name: 'Jane' })
```

API
---

### `parse(pattern: string, tokens?: ?Token[]): AST`

Generate a compact array-based AST from an ICU MessageFormat string pattern. If an empty `tokens` array is passed in, it will be filled with found tokens.

This can throw a `SyntaxError` if the pattern is not valid. The `offset` property of the error lets you know how far into the pattern tokenization was able to go before the error. The `tokens` array will have all the found tokens up until the bad syntax.

Note that the only semantic validation done in parsing is ensuring that `select`, `selectordinal`, and `plural` include an `other` sub-message. It does *not* validate that a plural keyword applies to the locale, or that a style is supported by the type, or even that the type will be supported by the interpreter. Successful parsing is not a guarantee the final message will format as expected.

### `SyntaxError`

```ts
class SyntaxError extends Error {
  name: 'SyntaxError';
  message: string;
  expected: ?string;
  found: ?string;
  offset: number;
  line: number;
  column: number;
}
```


License
-------

This software is free to use under the MIT license. See the [LICENSE-MIT file][LICENSE] for license text and copyright information.


[logo]: https://format-message.github.io/format-message/logo.svg
[npm]: https://www.npmjs.org/package/format-message-parse
[npm-image]: https://img.shields.io/npm/v/format-message-parse.svg
[style]: https://github.com/feross/standard
[style-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg
[license-image]: https://img.shields.io/npm/l/format-message.svg
[LICENSE]: https://github.com/format-message/format-message/blob/master/LICENSE-MIT

---
_Source: https://npm.io/package/format-message-parse · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
