Merge pull request #321 from r4zorgeek/fix/#296
fix: [localize] date formatting forced #296
This commit is contained in:
commit
f74023ee33
3 changed files with 81 additions and 12 deletions
|
|
@ -13,22 +13,16 @@ export function formatDate(date, options) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
const formatOptions = options || {};
|
const formatOptions = options || {};
|
||||||
// make sure months and days are always 2-digits
|
/**
|
||||||
if (!options) {
|
* Set smart defaults if:
|
||||||
|
* 1) no options object is passed
|
||||||
|
* 2) options object is passed, but none of the following props on it: day, month, year.
|
||||||
|
*/
|
||||||
|
if (!options || (options && !options.day && !options.month && !options.year)) {
|
||||||
formatOptions.year = 'numeric';
|
formatOptions.year = 'numeric';
|
||||||
formatOptions.month = '2-digit';
|
formatOptions.month = '2-digit';
|
||||||
formatOptions.day = '2-digit';
|
formatOptions.day = '2-digit';
|
||||||
}
|
}
|
||||||
if (options && !(options && options.year)) {
|
|
||||||
formatOptions.year = 'numeric';
|
|
||||||
}
|
|
||||||
if (options && !(options && options.month)) {
|
|
||||||
formatOptions.month = '2-digit';
|
|
||||||
}
|
|
||||||
if (options && !(options && options.day)) {
|
|
||||||
formatOptions.day = '2-digit';
|
|
||||||
}
|
|
||||||
|
|
||||||
const computedLocale = getLocale(formatOptions && formatOptions.locale);
|
const computedLocale = getLocale(formatOptions && formatOptions.locale);
|
||||||
let formattedDate = '';
|
let formattedDate = '';
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,51 @@ storiesOf('Localize System|Date', module).add(
|
||||||
})}
|
})}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Date without year</td>
|
||||||
|
<td>
|
||||||
|
<code>
|
||||||
|
formatDate(new Date(), {weekday:'long',month:'long',day:'2-digit'})
|
||||||
|
</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
${formatDate(new Date(), {
|
||||||
|
weekday: 'long',
|
||||||
|
month: 'long',
|
||||||
|
day: '2-digit',
|
||||||
|
})}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Date without month</td>
|
||||||
|
<td>
|
||||||
|
<code>
|
||||||
|
formatDate(new Date(), {weekday:'long',year:'numeric',day:'2-digit'})
|
||||||
|
</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
${formatDate(new Date(), {
|
||||||
|
weekday: 'long',
|
||||||
|
year: 'numeric',
|
||||||
|
day: '2-digit',
|
||||||
|
})}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Date without day</td>
|
||||||
|
<td>
|
||||||
|
<code>
|
||||||
|
formatDate(new Date(), {weekday:'long',year:'numeric',month:'long'})
|
||||||
|
</code>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
${formatDate(new Date(), {
|
||||||
|
weekday: 'long',
|
||||||
|
month: 'long',
|
||||||
|
year: 'numeric',
|
||||||
|
})}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td>Locale</td>
|
<td>Locale</td>
|
||||||
<td><code>formatDate(new Date(){locale:'nl-NL'})</code></td>
|
<td><code>formatDate(new Date(){locale:'nl-NL'})</code></td>
|
||||||
|
|
|
||||||
|
|
@ -108,6 +108,36 @@ describe('formatDate', () => {
|
||||||
expect(formatDate(parsedDate, options)).to.equal('maandag 01 januari 1940');
|
expect(formatDate(parsedDate, options)).to.equal('maandag 01 januari 1940');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('handles options without year', async () => {
|
||||||
|
const options = {
|
||||||
|
weekday: 'long',
|
||||||
|
month: 'long',
|
||||||
|
day: '2-digit',
|
||||||
|
};
|
||||||
|
const parsedDate = parseDate('12.10.2019');
|
||||||
|
expect(formatDate(parsedDate, options)).to.equal('Saturday, 12 October');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles options without month', async () => {
|
||||||
|
const options = {
|
||||||
|
weekday: 'long',
|
||||||
|
year: 'numeric',
|
||||||
|
day: '2-digit',
|
||||||
|
};
|
||||||
|
const parsedDate = parseDate('12.10.2019');
|
||||||
|
expect(formatDate(parsedDate, options)).to.equal('Saturday 12 2019');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('handles options without day', async () => {
|
||||||
|
const options = {
|
||||||
|
weekday: 'long',
|
||||||
|
year: 'numeric',
|
||||||
|
month: 'long',
|
||||||
|
};
|
||||||
|
const parsedDate = parseDate('12.10.2019');
|
||||||
|
expect(formatDate(parsedDate, options)).to.equal('October 2019 Saturday');
|
||||||
|
});
|
||||||
|
|
||||||
it('returns empty string when input is not a Date object', async () => {
|
it('returns empty string when input is not a Date object', async () => {
|
||||||
const date = '1-1-2016';
|
const date = '1-1-2016';
|
||||||
expect(formatDate(date)).to.equal('');
|
expect(formatDate(date)).to.equal('');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue