Skip to content

Commit c569644

Browse files
fix(mf2)!: Use spec values for :number useGrouping
1 parent 1f4b633 commit c569644

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

mf2/messageformat/src/data-model/function-ref.test.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('inputs with options', () => {
3030
test('local variable with :number expression', () => {
3131
const mf = new MessageFormat(
3232
'en',
33-
`.local $val = {12345678 :number useGrouping=false}
33+
`.local $val = {12345678 :number useGrouping=never}
3434
{{{$val :number minimumFractionDigits=2}}}`
3535
);
3636
//const val = new MessageNumber(null, BigInt(12345678), { options: { useGrouping: false } });
@@ -77,20 +77,28 @@ describe('inputs with options', () => {
7777
});
7878

7979
describe('Type casts based on runtime', () => {
80+
const date = '2000-01-01T15:00:00';
81+
8082
test('boolean function option with literal value', () => {
81-
const mfTrue = new MessageFormat('en', '{$var :number useGrouping=true}');
82-
expect(mfTrue.format({ var: 1234 })).toBe('1,234');
83-
const mfFalse = new MessageFormat('en', '{$var :number useGrouping=false}');
84-
expect(mfFalse.format({ var: 1234 })).toBe('1234');
83+
const mfTrue = new MessageFormat(
84+
'en',
85+
'{$date :datetime timeStyle=short hour12=true}'
86+
);
87+
expect(mfTrue.format({ date })).toMatch(/3:00/);
88+
const mfFalse = new MessageFormat(
89+
'en',
90+
'{$date :datetime timeStyle=short hour12=false}'
91+
);
92+
expect(mfFalse.format({ date })).toMatch(/15:00/);
8593
});
8694

8795
test('boolean function option with variable value', () => {
8896
const mf = new MessageFormat(
8997
'en',
90-
'{$var :number useGrouping=$useGrouping}'
98+
'{$date :datetime timeStyle=short hour12=$hour12}'
9199
);
92-
expect(mf.format({ var: 1234, useGrouping: 'false' })).toBe('1234');
93-
expect(mf.format({ var: 1234, useGrouping: false })).toBe('1234');
100+
expect(mf.format({ date, hour12: 'false' })).toMatch(/15:00/);
101+
expect(mf.format({ date, hour12: false })).toMatch(/15:00/);
94102
});
95103
});
96104

mf2/messageformat/src/functions/number.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import { MessageResolutionError } from '../errors.js';
22
import type { MessageExpressionPart } from '../formatted-parts.js';
33
import { getLocaleDir } from '../dir-utils.js';
44
import type { MessageFunctionContext, MessageValue } from './index.js';
5-
import {
6-
asBoolean,
7-
asPositiveInteger,
8-
asString,
9-
mergeLocales
10-
} from './utils.js';
5+
import { asPositiveInteger, asString, mergeLocales } from './utils.js';
116

127
/** @beta */
138
export interface MessageNumber extends MessageValue {
@@ -98,9 +93,12 @@ export function number(
9893
// @ts-expect-error TS types don't know about roundingIncrement
9994
opt[name] = asPositiveInteger(optval);
10095
break;
101-
case 'useGrouping':
102-
opt[name] = asBoolean(optval);
96+
case 'useGrouping': {
97+
const strval = asString(optval);
98+
// @ts-expect-error TS type is wrong
99+
opt[name] = strval === 'never' ? false : strval;
103100
break;
101+
}
104102
default:
105103
// @ts-expect-error Unknown options will be ignored
106104
opt[name] = asString(optval);

0 commit comments

Comments
 (0)