Skip to content

Commit 39df94d

Browse files
fix: use specified pattern length for standalone formats
1 parent 6f326ed commit 39df94d

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/dates/date-pattern.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ const DATE_OPTIONS_MAP = [ {
6060
specifier: "z"
6161
} ];
6262

63+
const STAND_ALONE_SPECIFIERS = {
64+
e: 'c',
65+
E: 'c',
66+
M: 'L',
67+
Q: 'q'
68+
};
69+
6370
const specifiersRegex = {};
6471
const resolvedFormats = {};
6572

@@ -131,14 +138,18 @@ function findBestMatch(specifiers, availableFormats) {
131138
}
132139

133140
result = result.replace("v", "z");
134-
//need to check for standalone specifiers if only one specifier
141+
135142
for (let idx = 0; idx < specifiersLength; idx++) {
136-
if (bestMatches[idx] && bestMatches[idx] !== specifiers[idx]) {
137-
result = result.replace(getSpecifierRegex(bestMatches[idx][0]), specifiers[idx]);
143+
const bestMatch = bestMatches[idx];
144+
if (bestMatch && bestMatch !== specifiers[idx]) {
145+
const matchSpecifier = bestMatches[idx][0];
146+
result = result.replace(getSpecifierRegex(matchSpecifier), specifiers[idx]);
147+
if (STAND_ALONE_SPECIFIERS[matchSpecifier]) {
148+
result = result.replace(getSpecifierRegex(STAND_ALONE_SPECIFIERS[matchSpecifier]), specifiers[idx]);
149+
}
138150
}
139151
}
140152

141-
142153
return result;
143154
}
144155

test/dates.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ describe('date formatting', () => {
312312
expect(formatDate(date(2000, 2, 10), { skeleton: "MMd" })).toEqual('02 10');
313313
});
314314

315+
it('replaces available format pattern with specified pattern', () => {
316+
expect(formatDate(date(2000, 2, 10), { skeleton: "MMMM" })).toEqual('February');
317+
});
318+
315319
it('supports skeleton format if there is both date and time', () => {
316320
expect(formatDate(date(2000, 2, 10, 10, 30), { skeleton: "yMMMdhm" })).toEqual('Feb 10, 2000, 10:30 AM');
317321
});

0 commit comments

Comments
 (0)