Skip to content

Commit d8a062d

Browse files
committed
(third) Refactor PluralValue to store the computed plural category
1 parent 51df1f3 commit d8a062d

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

experiments/stasm/third/example/example_list.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ REGISTRY["PLURAL_LEN"] = function (
5151
throw new TypeError();
5252
}
5353

54-
return new PluralValue(elements.value.length);
54+
// TODO(stasm): Cache PluralRules.
55+
let pr = new Intl.PluralRules(ctx.locale);
56+
let category = pr.select(elements.value.length);
57+
return new PluralValue(category, elements.value.length);
5558
};
5659

5760
REGISTRY["PEOPLE_LIST"] = function (

experiments/stasm/third/impl/context.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,41 +56,35 @@ export class FormattingContext {
5656
}
5757

5858
selectVariant(variants: Array<Variant>, selectors: Array<Selector>): Variant {
59-
interface ResolvedSelector<T> {
60-
value: T | null;
61-
string: string | null;
59+
interface ResolvedSelector {
60+
value: RuntimeValue<unknown> | null;
6261
default: string;
6362
}
6463

65-
let resolved_selectors: Array<ResolvedSelector<unknown>> = [];
64+
let resolved_selectors: Array<ResolvedSelector> = [];
6665
for (let selector of selectors) {
6766
if (selector.expr === null) {
6867
// A special selector which only selects its default value. Used in the
6968
// data model of single-variant messages.
7069
resolved_selectors.push({
7170
value: null,
72-
string: null,
7371
default: selector.default.value,
7472
});
7573
continue;
7674
}
7775

7876
switch (selector.expr.type) {
7977
case "VariableReference": {
80-
let value = this.vars[selector.expr.name];
8178
resolved_selectors.push({
82-
value: value.value,
83-
string: value.formatToString(this),
79+
value: this.vars[selector.expr.name],
8480
default: selector.default.value,
8581
});
8682
break;
8783
}
8884
case "FunctionCall": {
8985
let callable = REGISTRY[selector.expr.name];
90-
let value = callable(this, selector.expr.args, selector.expr.opts);
9186
resolved_selectors.push({
92-
value: value.value,
93-
string: value.formatToString(this),
87+
value: callable(this, selector.expr.args, selector.expr.opts),
9488
default: selector.default.value,
9589
});
9690
break;
@@ -104,7 +98,7 @@ export class FormattingContext {
10498
// TODO(stasm): Add NumberLiterals as keys (maybe).
10599
function matches_corresponding_selector(key: StringLiteral, idx: number) {
106100
return (
107-
key.value === resolved_selectors[idx].string ||
101+
key.value === resolved_selectors[idx].value?.value ||
108102
key.value === resolved_selectors[idx].default
109103
);
110104
}

experiments/stasm/third/impl/registry.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ function select_plural(
2525
throw new TypeError();
2626
}
2727

28-
return new PluralValue(count.value);
28+
// TODO(stasm): Cache PluralRules.
29+
let pr = new Intl.PluralRules(ctx.locale);
30+
let category = pr.select(count.value);
31+
return new PluralValue(category, count.value);
2932
}
3033

3134
function get_phrase(

experiments/stasm/third/impl/runtime.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,20 @@ export class NumberValue extends RuntimeValue<number> {
5656
}
5757
}
5858

59-
export class PluralValue extends RuntimeValue<number> {
60-
private opts: Intl.PluralRulesOptions;
59+
export class PluralValue extends RuntimeValue<Intl.LDMLPluralRule> {
60+
private count: number;
6161

62-
constructor(value: number, opts: Intl.PluralRulesOptions = {}) {
62+
constructor(value: Intl.LDMLPluralRule, count: number) {
6363
super(value);
64-
this.opts = opts;
64+
this.count = count;
6565
}
6666

6767
formatToString(ctx: FormattingContext): string {
68-
// TODO(stasm): Cache PluralRules.
69-
let pr = new Intl.PluralRules(ctx.locale, this.opts);
70-
return pr.select(this.value);
68+
throw new TypeError("PluralValue is not formattable.");
7169
}
7270

7371
*formatToParts(ctx: FormattingContext): IterableIterator<FormattedPart> {
74-
throw new TypeError("Pluralvalue is not formattable to parts.");
72+
throw new TypeError("PluralValue is not formattable.");
7573
}
7674
}
7775

0 commit comments

Comments
 (0)