package
A PEG.js parser for ICU MessageFormat strings
npm install messageformat-parserGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
Estimated unpacked size across 1 packages.
68.4 kB
Includes dependencies.
Latest published Apr 12, 2020
latest
slexaxton
alexsexton@gmail.com
A PEG.js parser for ICU MessageFormat strings – part of messageformat. Outputs an AST defined by parser.pegjs.
The generated parse(src, [options]) function takes two parameters, first the
string to be parsed, and a second optional parameter options, an object with
the following possible keys:
cardinal and ordinal – Arrays of valid plural categories for the current
locale, used to validate plural and selectordinal keys. If these are
missing or set to false, the full set of valid Unicode CLDR keys is used:
'zero', 'one', 'two', 'few', 'many', 'other'. To disable this check, pass in
an empty array.
strict – By default, the parsing applies a few relaxations to the ICU
MessageFormat spec. Setting strict: true will disable these relaxations:
argType of simpleArg formatting functions will be restricted to the
set of number, date, time, spellout, ordinal, and duration,
rather than accepting any lower-case identifier that does not start with a
number.argStyle of simpleArg formatting functions will not be
parsed as any other text, but instead as the spec requires: "In
argStyleText, every single ASCII apostrophe begins and ends quoted literal
text, and unquoted {curly braces} must occur in matched pairs."plural or selectordinal statement, a pound symbol (#) is
replaced with the input number. By default, # is also parsed as a special
character in nested statements too, and can be escaped using apostrophes
('#'). In strict mode # will be parsed as a special character only
directly inside a plural or selectordinal statement. Outside those, #
and '#' will be parsed as literal text.The parser only supports the default DOUBLE_OPTIONAL apostrophe mode, in
which a single apostrophe only starts quoted literal text if it immediately
precedes a curly brace {}, or a pound symbol # if inside a plural format. A
literal apostrophe ' is represented by either a single ' or a doubled ''
apostrophe character.
npm install messageformat-parser> var parse = require('messageformat-parser').parse;
> parse('So {wow}.')
[ 'So ', { type: 'argument', arg: 'wow' }, '.' ]
> parse('Such { thing }. { count, selectordinal, one {First} two {Second}' +
' few {Third} other {#th} } word.')
[ 'Such ',
{ type: 'argument', arg: 'thing' },
'. ',
{ type: 'selectordinal',
arg: 'count',
offset: 0,
cases:
[ { key: 'one', tokens: [ 'First' ] },
For more example usage, please take a look at our test suite.
The output of parse() is a Token array:
type Token = string | Argument | Plural | Select | Function
type Argument = {
type: 'argument',
arg: Identifier
}
type Plural = {
type: 'plural' | 'selectordinal',
arg: Identifier,
offset: number,
cases: PluralCase[]
}
type Select = {
type: 'select',
Messageformat is an OpenJS Foundation project, and we follow its Code of Conduct.