Skip to content

Commit 08a9129

Browse files
r4zorgeekJoren Broekema
authored andcommitted
fix(localize): unforce defaults when options are given
should not force defaults when at least one of the three date options is given (day, month, year). also added test cases and storybook demo.
1 parent dfb4259 commit 08a9129

File tree

3 files changed

+81
-12
lines changed

3 files changed

+81
-12
lines changed

packages/localize/src/date/formatDate.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,16 @@ export function formatDate(date, options) {
1313
return '';
1414
}
1515
const formatOptions = options || {};
16-
// make sure months and days are always 2-digits
17-
if (!options) {
16+
/**
17+
* Set smart defaults if:
18+
* 1) no options object is passed
19+
* 2) options object is passed, but none of the following props on it: day, month, year.
20+
*/
21+
if (!options || (options && !options.day && !options.month && !options.year)) {
1822
formatOptions.year = 'numeric';
1923
formatOptions.month = '2-digit';
2024
formatOptions.day = '2-digit';
2125
}
22-
if (options && !(options && options.year)) {
23-
formatOptions.year = 'numeric';
24-
}
25-
if (options && !(options && options.month)) {
26-
formatOptions.month = '2-digit';
27-
}
28-
if (options && !(options && options.day)) {
29-
formatOptions.day = '2-digit';
30-
}
31-
3226
const computedLocale = getLocale(formatOptions && formatOptions.locale);
3327
let formattedDate = '';
3428
try {

packages/localize/stories/date.stories.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,51 @@ storiesOf('Localize System|Date', module).add(
6161
})}
6262
</td>
6363
</tr>
64+
<tr>
65+
<td>Date without year</td>
66+
<td>
67+
<code>
68+
formatDate(new Date(), {weekday:'long',month:'long',day:'2-digit'})
69+
</code>
70+
</td>
71+
<td>
72+
${formatDate(new Date(), {
73+
weekday: 'long',
74+
month: 'long',
75+
day: '2-digit',
76+
})}
77+
</td>
78+
</tr>
79+
<tr>
80+
<td>Date without month</td>
81+
<td>
82+
<code>
83+
formatDate(new Date(), {weekday:'long',year:'numeric',day:'2-digit'})
84+
</code>
85+
</td>
86+
<td>
87+
${formatDate(new Date(), {
88+
weekday: 'long',
89+
year: 'numeric',
90+
day: '2-digit',
91+
})}
92+
</td>
93+
</tr>
94+
<tr>
95+
<td>Date without day</td>
96+
<td>
97+
<code>
98+
formatDate(new Date(), {weekday:'long',year:'numeric',month:'long'})
99+
</code>
100+
</td>
101+
<td>
102+
${formatDate(new Date(), {
103+
weekday: 'long',
104+
month: 'long',
105+
year: 'numeric',
106+
})}
107+
</td>
108+
</tr>
64109
<tr>
65110
<td>Locale</td>
66111
<td><code>formatDate(new Date(){locale:'nl-NL'})</code></td>

packages/localize/test/date/formatDate.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,36 @@ describe('formatDate', () => {
108108
expect(formatDate(parsedDate, options)).to.equal('maandag 01 januari 1940');
109109
});
110110

111+
it('handles options without year', async () => {
112+
const options = {
113+
weekday: 'long',
114+
month: 'long',
115+
day: '2-digit',
116+
};
117+
const parsedDate = parseDate('12.10.2019');
118+
expect(formatDate(parsedDate, options)).to.equal('Saturday, 12 October');
119+
});
120+
121+
it('handles options without month', async () => {
122+
const options = {
123+
weekday: 'long',
124+
year: 'numeric',
125+
day: '2-digit',
126+
};
127+
const parsedDate = parseDate('12.10.2019');
128+
expect(formatDate(parsedDate, options)).to.equal('Saturday 12 2019');
129+
});
130+
131+
it('handles options without day', async () => {
132+
const options = {
133+
weekday: 'long',
134+
year: 'numeric',
135+
month: 'long',
136+
};
137+
const parsedDate = parseDate('12.10.2019');
138+
expect(formatDate(parsedDate, options)).to.equal('October 2019 Saturday');
139+
});
140+
111141
it('returns empty string when input is not a Date object', async () => {
112142
const date = '1-1-2016';
113143
expect(formatDate(date)).to.equal('');

0 commit comments

Comments
 (0)