Skip to content
Open
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
5 changes: 4 additions & 1 deletion src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions test/locale-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});