diff --git a/src/locale.js b/src/locale.js index cc0dac2..fe1b287 100644 --- a/src/locale.js +++ b/src/locale.js @@ -402,7 +402,10 @@ 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. + 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..526ab1a 100644 --- a/test/locale-test.js +++ b/test/locale-test.js @@ -11,3 +11,16 @@ 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). + 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); + } +});