Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Use column-first rather than first-match selection #395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions packages/mf2-icu-mf1/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,17 +104,17 @@ class MessageMF1Number extends MessageNumber {
super(locale, number, opt);
}

matchSelectKey(key: string) {
selectKey(keys: Set<string>) {
let num = this.value;
const offset = getMF1Offset(this.options);
if (offset) {
if (typeof num === 'bigint') num += BigInt(offset);
else num += offset;
}
return (
(/^[0-9]+$/.test(key) && key === String(num)) ||
key === this.getPluralCategory()
);
const str = String(num);
if (keys.has(str)) return str;
const cat = this.getPluralCategory();
return keys.has(cat) ? cat : null;
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/mf2-messageformat/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [4.0.0-3.cf](https://github.com/messageformat/messageformat/compare/[email protected]@4.0.0-3.cf) (2023-03-14)

* Use column-first rather than first-match selection ([f5d1bba](https://github.com/messageformat/messageformat/commit/f5d1bba7b33b697eeb73bd9de1c01320f3d43bab))

## [4.0.0-3](https://github.com/messageformat/messageformat/compare/[email protected]@4.0.0-3) (2023-03-14)

* Use `|` rather than `()` as literal quotes ([15e1fcd](https://github.com/messageformat/messageformat/commit/15e1fcd65341a5ab536a06d4401b7f488b8cdfcc))
Expand Down
2 changes: 1 addition & 1 deletion packages/mf2-messageformat/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "messageformat",
"version": "4.0.0-3",
"version": "4.0.0-3.cf",
"description": "Intl.MessageFormat / Unicode MessageFormat 2 parser, runtime and polyfill",
"keywords": [
"messageformat",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
{
"src": "match {$foo} when one {one} when 1 {=1} when * {other}",
"params": { "foo": 1 },
"exp": "one"
"exp": "=1"
},
{
"src": "match {$foo} {$bar} when one one {one one} when one * {one other} when * * {other}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export class MessageFallback extends MessageValue<undefined> {
super(FALLBACK, locale, undefined, fmt);
}

matchSelectKey() {
return false;
selectKey() {
return null;
}

toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class MessageMarkupStart extends MessageValue<string> {
this.options = { ...options };
}

matchSelectKey() {
return false;
selectKey() {
return null;
}

toString() {
Expand All @@ -52,8 +52,8 @@ export class MessageMarkupEnd extends MessageValue<string> {
super(MARKUP_END, locale, name, options);
}

matchSelectKey() {
return false;
selectKey() {
return null;
}

toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ export class MessageNumber extends MessageValue<number | bigint> {
* For example, cardinal English plurals only use `one` and `other`,
* so a key `zero` will never be matched for that locale.
*/
matchSelectKey(key: string) {
if (/^[0-9]+$/.test(key) && key === String(this.value)) return true;
if (key === this.getPluralCategory()) return { plural: key };
return false;
selectKey(keys: Set<string>) {
const str = String(this.value);
if (keys.has(str)) return str;
const cat = this.getPluralCategory();
return keys.has(cat) ? cat : null;
}

toParts(): Intl.NumberFormatPart[] {
Expand Down
8 changes: 6 additions & 2 deletions packages/mf2-messageformat/src/message-value/message-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ export class MessageValue<T = unknown> {
this.#localeContext = locale;
}

matchSelectKey(key: string): boolean | Meta {
return this.value != null && String(this.value) === key;
selectKey(keys: Set<string>): string | null {
if (this.value != null) {
const str = String(this.value);
if (keys.has(str)) return str;
}
return null;
}

toString(onError?: Context['onError']): string {
Expand Down
60 changes: 46 additions & 14 deletions packages/mf2-messageformat/src/message-value/resolved-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,52 @@ function getPattern(
return { pattern: message.pattern.body };

case 'select': {
const resSelectors = message.selectors.map(sel => context.resolve(sel));
const ctx = message.selectors.map(sel => ({
selector: context.resolve(sel),
best: null as string | null,
keys: null as Set<string> | null
}));

cases: for (const { keys, value } of message.variants) {
let meta: Meta | undefined = undefined;
for (let i = 0; i < keys.length; ++i) {
const key = keys[i];
const rs = resSelectors[i];
const match = key.type === '*' || rs?.matchSelectKey(key.value);
if (!match) continue cases;
if (typeof match === 'object')
meta = Object.assign(meta ?? {}, match);
let candidates = message.variants;
loop: for (let i = 0; i < ctx.length; ++i) {
const sc = ctx[i];
if (!sc.keys) {
sc.keys = new Set();
for (const { keys } of candidates) {
const key = keys[i];
if (!key) break loop; // key-mismatch error
if (key.type !== '*') sc.keys.add(key.value);
}
}
sc.best = sc.keys.size ? sc.selector.selectKey(sc.keys) : null;

// Leave out all candidate variants that aren't the best,
// or only the catchall ones, if nothing else matches.
candidates = candidates.filter(v => {
const k = v.keys[i];
if (k.type === '*') return sc.best == null;
return sc.best === k.value;
});

// If we've run out of candidates,
// drop the previous best key of the preceding selector,
// reset all subsequent key sets,
// and restart the loop.
if (candidates.length === 0) {
if (i === 0) break; // No match; should not happen
const prev = ctx[i - 1];
if (prev.best == null) prev.keys?.clear();
else prev.keys?.delete(prev.best);
for (let j = i; j < ctx.length; ++j) ctx[j].keys = null;
candidates = message.variants;
i = -1;
}
return { pattern: value.body, meta };
}

return { pattern: [], meta: { selectResult: 'no-match' } };
const res = candidates[0];
return res
? { pattern: res.value.body }
: { pattern: [], meta: { selectResult: 'no-match' } };
}

default:
Expand All @@ -52,8 +82,10 @@ export class ResolvedMessage extends MessageValue<MessageValue[]> {
super(MESSAGE, context, resMsg, { meta, source });
}

matchSelectKey(key: string) {
return this.toString() === key;
selectKey(keys: Set<string>) {
let hasError = false;
const str = this.toString(() => (hasError = true));
return !hasError && keys.has(str) ? str : null;
}

toString(onError?: Context['onError']) {
Expand Down