From 1d752144633df0998b19ea6248562f1ea0671498 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Thu, 23 Jul 2026 15:27:10 +0100 Subject: [PATCH 1/2] Match locale names longest-first so a name that is a prefix of another parses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit formatRe joined a field's names into an ordered regex alternation (?:a|b|…). JavaScript alternation returns the first alternative that matches at the position, not the longest. When an earlier name is a prefix of a later name in the same field, the parser matched the short name first and the leftover characters then broke the parse. This corrupts d3's own formatter output in shipped locales: Czech "červen" (June) is a prefix of "červenec" (July), so timeParse("%d %B %Y") of "15 červenec 2020" returned null; Turkish "Pazar" (Sun) is a prefix of "Pazartesi" (Mon), so every Monday/Saturday failed to parse. Sort the names by descending length before building the alternation, so the longest matching name wins. formatLookup is keyed by the matched text, so the reordering does not affect index resolution. --- src/locale.js | 6 +++++- test/locale-test.js | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/locale.js b/src/locale.js index cc0dac2..d43a032 100644 --- a/src/locale.js +++ b/src/locale.js @@ -402,7 +402,11 @@ function requote(s) { } function formatRe(names) { - return new RegExp("^(?:" + names.map(requote).join("|") + ")", "i"); + // Sort by descending length so that when one name is a prefix of another in + // the same field (e.g. Czech "červen" is a prefix of "červenec"), the + // longer name is matched first; ordered alternation is first-match, not + // longest-match. formatLookup is keyed by text, so order is irrelevant to it. + return new RegExp("^(?:" + names.slice().sort((a, b) => b.length - a.length).map(requote).join("|") + ")", "i"); } function formatLookup(names) { diff --git a/test/locale-test.js b/test/locale-test.js index f28ac01..9f327e1 100644 --- a/test/locale-test.js +++ b/test/locale-test.js @@ -11,3 +11,17 @@ it("locale data is valid", () => { timeFormatLocale(locale); } }); + +it("parse resolves a name that is a prefix of a longer name in the same field", () => { + // Czech "červen" (June) is a prefix of "červenec" (July); Turkish "Pazar" + // (Sun) is a prefix of "Pazartesi" (Mon). Ordered alternation matched the + // shorter name first, so these formatted dates failed to round-trip. + for (const [file, spec, date] of [ + ["cs-CZ.json", "%d %B %Y", new Date(2020, 6, 15)], // July + ["tr-TR.json", "%A %d %B %Y", new Date(2020, 6, 13)], // Monday + ]) { + const locale = timeFormatLocale(JSON.parse(readFileSync(join("locale", file), "utf8"))); + const string = locale.format(spec)(date); + assert.deepStrictEqual(locale.parse(spec)(string), date); + } +}); From de09da28bb59a3ae66ed3c220eaa87e5222b7bf8 Mon Sep 17 00:00:00 2001 From: Yarchik Date: Sat, 25 Jul 2026 11:39:44 +0100 Subject: [PATCH 2/2] Tighten comments per review feedback --- src/locale.js | 3 +-- test/locale-test.js | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/locale.js b/src/locale.js index d43a032..fe1b287 100644 --- a/src/locale.js +++ b/src/locale.js @@ -404,8 +404,7 @@ function requote(s) { function formatRe(names) { // Sort by descending length so that when one name is a prefix of another in // the same field (e.g. Czech "červen" is a prefix of "červenec"), the - // longer name is matched first; ordered alternation is first-match, not - // longest-match. formatLookup is keyed by text, so order is irrelevant to it. + // longer name is matched first. return new RegExp("^(?:" + names.slice().sort((a, b) => b.length - a.length).map(requote).join("|") + ")", "i"); } diff --git a/test/locale-test.js b/test/locale-test.js index 9f327e1..526ab1a 100644 --- a/test/locale-test.js +++ b/test/locale-test.js @@ -14,8 +14,7 @@ it("locale data is valid", () => { it("parse resolves a name that is a prefix of a longer name in the same field", () => { // Czech "červen" (June) is a prefix of "červenec" (July); Turkish "Pazar" - // (Sun) is a prefix of "Pazartesi" (Mon). Ordered alternation matched the - // shorter name first, so these formatted dates failed to round-trip. + // (Sun) is a prefix of "Pazartesi" (Mon). for (const [file, spec, date] of [ ["cs-CZ.json", "%d %B %Y", new Date(2020, 6, 15)], // July ["tr-TR.json", "%A %d %B %Y", new Date(2020, 6, 13)], // Monday