Skip to content

Commit b6599d3

Browse files
authored
fix: Update locale month to support both array and function (#581)
1 parent d409304 commit b6599d3

File tree

4 files changed

+76
-21
lines changed

4 files changed

+76
-21
lines changed

src/index.js

+18-17
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,15 @@ class Dayjs {
278278
const str = formatStr || C.FORMAT_DEFAULT
279279
const zoneStr = Utils.z(this)
280280
const locale = this.$locale()
281+
const { $H, $m, $M } = this
281282
const {
282283
weekdays, months, meridiem
283284
} = locale
284285
const getShort = (arr, index, full, length) => (
285-
(arr && arr[index]) || full[index].substr(0, length)
286+
(arr && (arr[index] || arr(this, str))) || full[index].substr(0, length)
286287
)
287288
const get$H = num => (
288-
Utils.s(this.$H % 12 || 12, num, '0')
289+
Utils.s($H % 12 || 12, num, '0')
289290
)
290291

291292
const meridiemFunc = meridiem || ((hour, minute, isLowercase) => {
@@ -295,32 +296,32 @@ class Dayjs {
295296

296297
const matches = {
297298
YY: String(this.$y).slice(-2),
298-
YYYY: String(this.$y),
299-
M: String(this.$M + 1),
300-
MM: Utils.s(this.$M + 1, 2, '0'),
301-
MMM: getShort(locale.monthsShort, this.$M, months, 3),
302-
MMMM: months[this.$M],
303-
D: String(this.$D),
299+
YYYY: this.$y,
300+
M: $M + 1,
301+
MM: Utils.s($M + 1, 2, '0'),
302+
MMM: getShort(locale.monthsShort, $M, months, 3),
303+
MMMM: months[$M] || months(this, str),
304+
D: this.$D,
304305
DD: Utils.s(this.$D, 2, '0'),
305-
d: String(this.$W),
306+
d: this.$W,
306307
dd: getShort(locale.weekdaysMin, this.$W, weekdays, 2),
307308
ddd: getShort(locale.weekdaysShort, this.$W, weekdays, 3),
308309
dddd: weekdays[this.$W],
309-
H: String(this.$H),
310-
HH: Utils.s(this.$H, 2, '0'),
310+
H: $H,
311+
HH: Utils.s($H, 2, '0'),
311312
h: get$H(1),
312313
hh: get$H(2),
313-
a: meridiemFunc(this.$H, this.$m, true),
314-
A: meridiemFunc(this.$H, this.$m, false),
315-
m: String(this.$m),
316-
mm: Utils.s(this.$m, 2, '0'),
317-
s: String(this.$s),
314+
a: meridiemFunc($H, $m, true),
315+
A: meridiemFunc($H, $m, false),
316+
m: $m,
317+
mm: Utils.s($m, 2, '0'),
318+
s: this.$s,
318319
ss: Utils.s(this.$s, 2, '0'),
319320
SSS: Utils.s(this.$ms, 3, '0'),
320321
Z: zoneStr // 'ZZ' logic below
321322
}
322323

323-
return str.replace(C.REGEX_FORMAT, (match, $1) => $1 || matches[match] || zoneStr.replace(':', '')) // 'ZZ'
324+
return String(str.replace(C.REGEX_FORMAT, (match, $1) => $1 || matches[match] || zoneStr.replace(':', ''))) // 'ZZ'
324325
}
325326

326327
utcOffset() {

src/locale/ru.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
import dayjs from 'dayjs'
22

3+
const monthFormat = 'января_февраля_марта_апреля_мая_июня_июля_августа_сентября_октября_ноября_декабря'.split('_')
4+
const monthStandalone = 'январь_февраль_март_апрель_май_июнь_июль_август_сентябрь_октябрь_ноябрь_декабрь'.split('_')
5+
6+
const monthShortFormat = 'янв._февр._мар._апр._мая_июня_июля_авг._сент._окт._нояб._дек.'.split('_')
7+
const monthShortStandalone = 'янв._февр._март_апр._май_июнь_июль_авг._сент._окт._нояб._дек.'.split('_')
8+
9+
const MONTHS_IN_FORMAT = /D[oD]?(\[[^[\]]*\]|\s)+MMMM?/
310
const locale = {
411
name: 'ru',
512
weekdays: 'воскресенье_понедельник_вторник_среда_четверг_пятница_суббота'.split('_'),
613
weekdaysShort: 'вск_пнд_втр_срд_чтв_птн_сбт'.split('_'),
714
weekdaysMin: 'вс_пн_вт_ср_чт_пт_сб'.split('_'),
8-
months: 'январь_февраль_март_апрель_май_июнь_июль_август_сентябрь_октябрь_ноябрь_декабрь'.split('_'),
9-
monthsShort: 'янв_фев_мар_апр_май_июн_июл_авг_сен_окт_ноя_дек'.split('_'),
15+
months: (dayjsInstance, format) => {
16+
if (MONTHS_IN_FORMAT.test(format)) {
17+
return monthFormat[dayjsInstance.month()]
18+
}
19+
return monthStandalone[dayjsInstance.month()]
20+
},
21+
monthsShort: (dayjsInstance, format) => {
22+
if (MONTHS_IN_FORMAT.test(format)) {
23+
return monthShortFormat[dayjsInstance.month()]
24+
}
25+
return monthShortStandalone[dayjsInstance.month()]
26+
},
1027
weekStart: 1,
1128
formats: {
1229
LT: 'H:mm',

test/locale/keys.test.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,23 @@ Locale.forEach((locale) => {
3535
expect(weekdays).toEqual(expect.any(Array))
3636

3737
if (weekdaysShort) expect(weekdaysShort).toEqual(expect.any(Array))
38-
if (monthsShort) expect(monthsShort).toEqual(expect.any(Array))
3938
if (weekdaysMin) expect(weekdaysMin).toEqual(expect.any(Array))
4039
if (weekStart) expect(weekStart).toEqual(expect.any(Number))
4140

42-
expect(months).toEqual(expect.any(Array))
41+
// months could be a function or array
42+
if (Array.isArray(months)) {
43+
expect(months).toEqual(expect.any(Array))
44+
} else {
45+
expect(months(dayjs(), 'str')).toEqual(expect.any(String))
46+
}
47+
// monthsShort could be a function or array
48+
if (monthsShort) {
49+
if (Array.isArray(monthsShort)) {
50+
expect(monthsShort).toEqual(expect.any(Array))
51+
} else {
52+
expect(monthsShort(dayjs(), 'str')).toEqual(expect.any(String))
53+
}
54+
}
4355
// function pass date return string or number or null
4456
if (name !== 'en') { // en ordinal set in advancedFormat
4557
for (let i = 1; i <= 31; i += 1) {

test/locale/ru.test.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import moment from 'moment'
2+
import MockDate from 'mockdate'
3+
import dayjs from '../../src'
4+
import '../../src/locale/ru'
5+
6+
beforeEach(() => {
7+
MockDate.set(new Date())
8+
})
9+
10+
afterEach(() => {
11+
MockDate.reset()
12+
})
13+
14+
it('Format Month with locale function', () => {
15+
for (let i = 0; i <= 7; i += 1) {
16+
const dayjsRU = dayjs().locale('ru').add(i, 'day')
17+
const momentRU = moment().locale('ru').add(i, 'day')
18+
const testFormat1 = 'DD MMMM YYYY MMM'
19+
const testFormat2 = 'MMMM'
20+
const testFormat3 = 'MMM'
21+
expect(dayjsRU.format(testFormat1)).toEqual(momentRU.format(testFormat1))
22+
expect(dayjsRU.format(testFormat2)).toEqual(momentRU.format(testFormat2))
23+
expect(dayjsRU.format(testFormat3)).toEqual(momentRU.format(testFormat3))
24+
}
25+
})

0 commit comments

Comments
 (0)