Skip to content

Commit

Permalink
fix: use specified pattern length for standalone formats
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkaradachki committed Aug 21, 2017
1 parent 6f326ed commit 39df94d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/dates/date-pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ const DATE_OPTIONS_MAP = [ {
specifier: "z"
} ];

const STAND_ALONE_SPECIFIERS = {
e: 'c',
E: 'c',
M: 'L',
Q: 'q'
};

const specifiersRegex = {};
const resolvedFormats = {};

Expand Down Expand Up @@ -131,14 +138,18 @@ function findBestMatch(specifiers, availableFormats) {
}

result = result.replace("v", "z");
//need to check for standalone specifiers if only one specifier

for (let idx = 0; idx < specifiersLength; idx++) {
if (bestMatches[idx] && bestMatches[idx] !== specifiers[idx]) {
result = result.replace(getSpecifierRegex(bestMatches[idx][0]), specifiers[idx]);
const bestMatch = bestMatches[idx];
if (bestMatch && bestMatch !== specifiers[idx]) {
const matchSpecifier = bestMatches[idx][0];
result = result.replace(getSpecifierRegex(matchSpecifier), specifiers[idx]);
if (STAND_ALONE_SPECIFIERS[matchSpecifier]) {
result = result.replace(getSpecifierRegex(STAND_ALONE_SPECIFIERS[matchSpecifier]), specifiers[idx]);
}
}
}


return result;
}

Expand Down
4 changes: 4 additions & 0 deletions test/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,10 @@ describe('date formatting', () => {
expect(formatDate(date(2000, 2, 10), { skeleton: "MMd" })).toEqual('02 10');
});

it('replaces available format pattern with specified pattern', () => {
expect(formatDate(date(2000, 2, 10), { skeleton: "MMMM" })).toEqual('February');
});

it('supports skeleton format if there is both date and time', () => {
expect(formatDate(date(2000, 2, 10, 10, 30), { skeleton: "yMMMdhm" })).toEqual('Feb 10, 2000, 10:30 AM');
});
Expand Down

0 comments on commit 39df94d

Please sign in to comment.