@@ -6,9 +6,10 @@ import {
66
77test ( 'Custom function' , ( ) => {
88 const functions = {
9- custom : ( { source, locales : [ locale ] } , _opt , input ) => ( {
9+ custom : ( { dir , source, locales : [ locale ] } , _opt , input ) => ( {
1010 type : 'custom' ,
1111 source,
12+ dir : dir ?? 'auto' ,
1213 locale,
1314 toParts : ( ) => [
1415 { type : 'custom' , source, locale, value : `part:${ input } ` }
@@ -17,17 +18,19 @@ test('Custom function', () => {
1718 } )
1819 } satisfies MessageFunctions ;
1920 const mf = new MessageFormat ( 'en' , '{$var :custom}' , { functions } ) ;
20- expect ( mf . format ( { var : 42 } ) ) . toEqual ( 'str :42' ) ;
21+ expect ( mf . format ( { var : 42 } ) ) . toEqual ( '\u2068str :42\u2069 ' ) ;
2122 expect ( mf . formatToParts ( { var : 42 } ) ) . toEqual ( [
22- { type : 'custom' , source : '$var' , locale : 'en' , value : 'part:42' }
23+ { type : 'bidiIsolation' , value : '\u2068' } ,
24+ { type : 'custom' , source : '$var' , locale : 'en' , value : 'part:42' } ,
25+ { type : 'bidiIsolation' , value : '\u2069' }
2326 ] ) ;
2427} ) ;
2528
2629describe ( 'inputs with options' , ( ) => {
2730 test ( 'local variable with :number expression' , ( ) => {
2831 const mf = new MessageFormat (
2932 'en' ,
30- `.local $val = {12345678 :number useGrouping=false }
33+ `.local $val = {12345678 :number useGrouping=never }
3134 {{{$val :number minimumFractionDigits=2}}}`
3235 ) ;
3336 //const val = new MessageNumber(null, BigInt(12345678), { options: { useGrouping: false } });
@@ -74,20 +77,28 @@ describe('inputs with options', () => {
7477} ) ;
7578
7679describe ( 'Type casts based on runtime' , ( ) => {
80+ const date = '2000-01-01T15:00:00' ;
81+
7782 test ( 'boolean function option with literal value' , ( ) => {
78- const mfTrue = new MessageFormat ( 'en' , '{$var :number useGrouping=true}' ) ;
79- expect ( mfTrue . format ( { var : 1234 } ) ) . toBe ( '1,234' ) ;
80- const mfFalse = new MessageFormat ( 'en' , '{$var :number useGrouping=false}' ) ;
81- 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 : 0 0 / ) ;
88+ const mfFalse = new MessageFormat (
89+ 'en' ,
90+ '{$date :datetime timeStyle=short hour12=false}'
91+ ) ;
92+ expect ( mfFalse . format ( { date } ) ) . toMatch ( / 1 5 : 0 0 / ) ;
8293 } ) ;
8394
8495 test ( 'boolean function option with variable value' , ( ) => {
8596 const mf = new MessageFormat (
8697 'en' ,
87- '{$var :number useGrouping=$useGrouping }'
98+ '{$date :datetime timeStyle=short hour12=$hour12 }'
8899 ) ;
89- expect ( mf . format ( { var : 1234 , useGrouping : 'false' } ) ) . toBe ( '1234' ) ;
90- expect ( mf . format ( { var : 1234 , useGrouping : false } ) ) . toBe ( '1234' ) ;
100+ expect ( mf . format ( { date , hour12 : 'false' } ) ) . toMatch ( / 1 5 : 0 0 / ) ;
101+ expect ( mf . format ( { date , hour12 : false } ) ) . toMatch ( / 1 5 : 0 0 / ) ;
91102 } ) ;
92103} ) ;
93104
@@ -98,9 +109,11 @@ describe('Function return is not a MessageValue', () => {
98109 } satisfies MessageFunctions ;
99110 const mf = new MessageFormat ( 'en' , '{:fail}' , { functions } ) ;
100111 const onError = jest . fn ( ) ;
101- expect ( mf . format ( undefined , onError ) ) . toEqual ( '{:fail}' ) ;
112+ expect ( mf . format ( undefined , onError ) ) . toEqual ( '\u2068 {:fail}\u2069 ' ) ;
102113 expect ( mf . formatToParts ( undefined , onError ) ) . toEqual ( [
103- { type : 'fallback' , source : ':fail' }
114+ { type : 'bidiIsolation' , value : '\u2068' } ,
115+ { type : 'fallback' , source : ':fail' } ,
116+ { type : 'bidiIsolation' , value : '\u2069' }
104117 ] ) ;
105118 expect ( onError ) . toHaveBeenCalledTimes ( 2 ) ;
106119 } ) ;
@@ -111,9 +124,11 @@ describe('Function return is not a MessageValue', () => {
111124 } satisfies MessageFunctions ;
112125 const mf = new MessageFormat ( 'en' , '{42 :fail}' , { functions } ) ;
113126 const onError = jest . fn ( ) ;
114- expect ( mf . format ( undefined , onError ) ) . toEqual ( '{|42|}' ) ;
127+ expect ( mf . format ( undefined , onError ) ) . toEqual ( '\u2068 {|42|}\u2069 ' ) ;
115128 expect ( mf . formatToParts ( undefined , onError ) ) . toEqual ( [
116- { type : 'fallback' , source : '|42|' }
129+ { type : 'bidiIsolation' , value : '\u2068' } ,
130+ { type : 'fallback' , source : '|42|' } ,
131+ { type : 'bidiIsolation' , value : '\u2069' }
117132 ] ) ;
118133 expect ( onError ) . toHaveBeenCalledTimes ( 2 ) ;
119134 } ) ;
0 commit comments