Skip to content

Commit

Permalink
feat(intl-messageformat): support more parse options in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurdenner authored and longlho committed Jan 30, 2023
1 parent fd7776a commit e6b43dc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/icu-messageformat-parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function parse(message: string, opts: ParserOptions = {}) {
}
return result.val
}
export {ParserOptions}
export * from './types'
// only for testing
export const _Parser = Parser
18 changes: 8 additions & 10 deletions packages/intl-messageformat/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ Copyrights licensed under the New BSD License.
See the accompanying LICENSE file for terms.
*/

import {parse, MessageFormatElement} from '@formatjs/icu-messageformat-parser'
import {
parse,
MessageFormatElement,
ParserOptions,
} from '@formatjs/icu-messageformat-parser'
import memoize, {Cache, strategies} from '@formatjs/fast-memoize'
import {
FormatterCache,
Expand Down Expand Up @@ -53,15 +57,8 @@ function mergeConfigs(
)
}

export interface Options {
export interface Options extends Omit<ParserOptions, 'locale'> {
formatters?: Formatters
/**
* Whether to treat HTML/XML tags as string literal
* instead of parsing them as tag token.
* When this is false we only allow simple tags without
* any attributes
*/
ignoreTag?: boolean
}

function createFastMemoizeCache<V>(
Expand Down Expand Up @@ -133,9 +130,10 @@ export class IntlMessageFormat {
'IntlMessageFormat.__parse must be set to process `message` of type `string`'
)
}
const {formatters, ...parseOpts} = opts || {}
// Parse string messages into an AST.
this.ast = IntlMessageFormat.__parse(message, {
ignoreTag: opts?.ignoreTag,
...parseOpts,
locale: this.resolvedLocale,
})
} else {
Expand Down
18 changes: 18 additions & 0 deletions packages/intl-messageformat/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,24 @@ describe('IntlMessageFormat', function () {
})
})

describe('select message without other clause', function () {
const msg = '{variable, select, a {A} b {B} c {C}}'

it('should throw by default', function () {
expect(() => {
new IntlMessageFormat(msg, 'en')
}).toThrow(/MISSING_OTHER_CLAUSE/)
})

it('should not throw when requiresOtherClause is false', function () {
expect(() => {
new IntlMessageFormat(msg, 'en', undefined, {
requiresOtherClause: false,
})
}).not.toThrow()
})
})

describe('selectordinal arguments', function () {
const msg =
'This is my {year, selectordinal, one{#st} two{#nd} few{#rd} other{#th}} birthday.'
Expand Down

0 comments on commit e6b43dc

Please sign in to comment.