@@ -2,7 +2,9 @@ import {Argument, Message, Parameter} from "../impl/model.js";
22import { REGISTRY } from "../impl/registry.js" ;
33import {
44 formatMessage ,
5+ FormattedPart ,
56 FormattingContext ,
7+ formatToParts ,
68 PluralValue ,
79 RuntimeValue ,
810 StringValue ,
@@ -18,9 +20,24 @@ class Person {
1820 }
1921}
2022
21- class PeopleValue extends RuntimeValue < Array < Person > > {
22- format ( ctx : FormattingContext ) : string {
23- throw new RangeError ( "Must be formatted via PEOPLE_LIST." ) ;
23+ // TODO(stasm): This is generic enough that it could be in impl/runtime.ts.
24+ class ListValue < T > extends RuntimeValue < Array < T > > {
25+ private opts : Intl . ListFormatOptions ;
26+
27+ constructor ( value : Array < T > , opts : Intl . ListFormatOptions = { } ) {
28+ super ( value ) ;
29+ this . opts = opts ;
30+ }
31+
32+ formatToString ( ctx : FormattingContext ) : string {
33+ // TODO(stasm): Cache ListFormat.
34+ let lf = new Intl . ListFormat ( ctx . locale , this . opts ) ;
35+ return lf . format ( this . value ) ;
36+ }
37+
38+ * formatToParts ( ctx : FormattingContext ) : IterableIterator < FormattedPart > {
39+ let lf = new Intl . ListFormat ( ctx . locale , this . opts ) ;
40+ yield * lf . formatToParts ( this . value ) ;
2441 }
2542}
2643
@@ -30,7 +47,7 @@ REGISTRY["PLURAL_LEN"] = function (
3047 opts : Record < string , Parameter >
3148) : PluralValue {
3249 let elements = ctx . toRuntimeValue ( args [ 0 ] ) ;
33- if ( ! ( elements instanceof PeopleValue ) ) {
50+ if ( ! ( elements instanceof ListValue ) ) {
3451 throw new TypeError ( ) ;
3552 }
3653
@@ -41,13 +58,13 @@ REGISTRY["PEOPLE_LIST"] = function (
4158 ctx : FormattingContext ,
4259 args : Array < Argument > ,
4360 opts : Record < string , Parameter >
44- ) : StringValue {
61+ ) : ListValue < string > {
4562 if ( ctx . locale !== "ro" ) {
4663 throw new Error ( "Only Romanian supported" ) ;
4764 }
4865
4966 let elements = ctx . toRuntimeValue ( args [ 0 ] ) ;
50- if ( ! ( elements instanceof PeopleValue ) ) {
67+ if ( ! ( elements instanceof ListValue ) ) {
5168 throw new TypeError ( ) ;
5269 }
5370
@@ -70,14 +87,21 @@ REGISTRY["PEOPLE_LIST"] = function (
7087 break ;
7188 }
7289
73- // @ts -ignore
74- let lf = new Intl . ListFormat ( ctx . locale , {
75- // TODO(stasm): Type-check these.
90+ let list_style = ctx . toRuntimeValue ( opts [ "STYLE" ] ) ;
91+ if ( ! ( list_style instanceof StringValue ) ) {
92+ throw new TypeError ( ) ;
93+ }
94+
95+ let list_type = ctx . toRuntimeValue ( opts [ "TYPE" ] ) ;
96+ if ( ! ( list_type instanceof StringValue ) ) {
97+ throw new TypeError ( ) ;
98+ }
99+
100+ return new ListValue ( names , {
76101 // TODO(stasm): Add default options.
77- style : ctx . toRuntimeValue ( opts [ "STYLE" ] ) . value ,
78- type : ctx . toRuntimeValue ( opts [ "TYPE" ] ) . value ,
102+ style : list_style . value ,
103+ type : list_type . value ,
79104 } ) ;
80- return new StringValue ( lf . format ( names ) ) ;
81105
82106 function decline ( name : string ) : string {
83107 let declension = ctx . toRuntimeValue ( opts [ "CASE" ] ) ;
@@ -166,13 +190,24 @@ console.log("==== Romanian ====");
166190 } ;
167191 console . log (
168192 formatMessage ( message , {
169- names : new PeopleValue ( [
193+ names : new ListValue ( [
170194 new Person ( "Maria" , "Stanescu" ) ,
171195 new Person ( "Ileana" , "Zamfir" ) ,
172196 new Person ( "Petre" , "Belu" ) ,
173197 ] ) ,
174198 } )
175199 ) ;
200+ console . log (
201+ Array . of (
202+ ...formatToParts ( message , {
203+ names : new ListValue ( [
204+ new Person ( "Maria" , "Stanescu" ) ,
205+ new Person ( "Ileana" , "Zamfir" ) ,
206+ new Person ( "Petre" , "Belu" ) ,
207+ ] ) ,
208+ } )
209+ )
210+ ) ;
176211}
177212
178213{
@@ -235,7 +270,7 @@ console.log("==== Romanian ====");
235270 } ;
236271 console . log (
237272 formatMessage ( message , {
238- names : new PeopleValue ( [
273+ names : new ListValue ( [
239274 new Person ( "Maria" , "Stanescu" ) ,
240275 new Person ( "Ileana" , "Zamfir" ) ,
241276 new Person ( "Petre" , "Belu" ) ,
0 commit comments