Skip to content

Commit 144ebce

Browse files
tlouissedaKmoR
authored andcommitted
feat(localize): allow long/short/narrow param getMonthNames
1 parent 6cfa301 commit 144ebce

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

packages/localize/src/date/getMonthNames.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,31 @@ import { normalizeDate } from './normalizeDate.js';
22

33
const monthsLocaleCache = {};
44

5-
export function getMonthNames({ locale }) {
6-
let months = monthsLocaleCache[locale];
5+
/**
6+
* @desc Returns month names for locale
7+
* @param {string} options.locale locale
8+
* @param {string} [options.style=long] long, short or narrow
9+
* @returns {Array} like: ['Januray', 'February', ...etc].
10+
*/
11+
export function getMonthNames({ locale, style = 'long' } = {}) {
12+
let months = monthsLocaleCache[locale] && monthsLocaleCache[locale][style];
713

814
if (months) {
915
return months;
1016
}
1117

1218
months = [];
1319

14-
const formatter = new Intl.DateTimeFormat(locale, { month: 'long' });
20+
const formatter = new Intl.DateTimeFormat(locale, { month: style });
1521
for (let i = 0; i < 12; i += 1) {
1622
const date = new Date(2019, i, 1);
1723
const formattedDate = formatter.format(date);
1824
const normalizedDate = normalizeDate(formattedDate);
1925
months.push(normalizedDate);
2026
}
2127

22-
monthsLocaleCache[locale] = months;
28+
monthsLocaleCache[locale] = monthsLocaleCache[locale] || {};
29+
monthsLocaleCache[locale][style] = months;
2330

2431
return months;
2532
}

packages/localize/test/date/getMonthNames.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,15 @@ describe('getMonthNames', () => {
1818
s`一月 二月 三月 四月 五月 六月 七月 八月 九月 十月 十一月 十二月`,
1919
);
2020
});
21+
it('supports "short" style', () => {
22+
expect(getMonthNames({ locale: 'en-GB', style: 'short' })).to.deep.equal(
23+
s`Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec`,
24+
);
25+
expect(getMonthNames({ locale: 'nl-NL', style: 'short' })).to.deep.equal(
26+
s`jan. feb. mrt. apr. mei jun. jul. aug. sep. okt. nov. dec.`,
27+
);
28+
expect(getMonthNames({ locale: 'zh-CH', style: 'short' })).to.deep.equal(
29+
s`1月 2月 3月 4月 5月 6月 7月 8月 9月 10月 11月 12月`,
30+
);
31+
});
2132
});

0 commit comments

Comments
 (0)