diff --git a/src/plugin/customParseFormat/index.js b/src/plugin/customParseFormat/index.js index c7082833c..c6399a4e7 100644 --- a/src/plugin/customParseFormat/index.js +++ b/src/plugin/customParseFormat/index.js @@ -105,19 +105,19 @@ const expressions = { MMM: [matchWord, function (input) { const months = getLocalePart('months') const monthsShort = getLocalePart('monthsShort') - const matchIndex = (monthsShort || months.map(_ => _.slice(0, 3))).indexOf(input) + 1 - if (matchIndex < 1) { + const matchIndex = (monthsShort || months.map(_ => _.slice(0, 3))).indexOf(input) + if (matchIndex < 0) { throw new Error() } - this.month = (matchIndex % 12) || matchIndex + this.month = (matchIndex % 12) + 1 }], MMMM: [matchWord, function (input) { const months = getLocalePart('months') - const matchIndex = months.indexOf(input) + 1 - if (matchIndex < 1) { + const matchIndex = months.indexOf(input) + if (matchIndex < 0) { throw new Error() } - this.month = (matchIndex % 12) || matchIndex + this.month = (matchIndex % 12) + 1 }], Y: [matchSigned, addInput('year')], YY: [match2, function (input) { diff --git a/test/plugin/customParseFormat.test.js b/test/plugin/customParseFormat.test.js index fb4030176..ede745e31 100644 --- a/test/plugin/customParseFormat.test.js +++ b/test/plugin/customParseFormat.test.js @@ -1,6 +1,7 @@ import MockDate from 'mockdate' import moment from 'moment' import dayjs from '../../src' +import '../../src/locale/be' import '../../src/locale/ru' import uk from '../../src/locale/uk' import '../../src/locale/zh-cn' @@ -211,6 +212,12 @@ it('parse month from short string with locale in argument', () => { expect(dayjs(input, format, 'uk').valueOf()).toBe(moment(input, format, 'uk').valueOf()) }) +it('parse last month of year from short format string with locale', () => { + const input = '2018 снеж. 03' + const format = 'YYYY MMM DD' + expect(dayjs(input, format, 'be').valueOf()).toBe(moment(input, format, 'be').valueOf()) +}) + it('parse month from string with locale in argument', () => { const input = '2018 лютий 03' const format = 'YYYY MMMM DD' @@ -218,6 +225,13 @@ it('parse month from string with locale in argument', () => { expect(dayjs(input, format, 'uk').valueOf()).toBe(moment(input, format, 'uk').valueOf()) }) +it('parse last month of year from format string with locale', () => { + const input = '2018 грудня 03' + const format = 'YYYY MMMM DD' + + expect(dayjs(input, format, 'uk').valueOf()).toBe(moment(input, format, 'uk').valueOf()) +}) + it('return Invalid Date when parse corrupt string', () => { const input = '2018 Turnip 03' const format = 'YYYY MMMM DD'