diff --git a/src/plugin/customParseFormat/index.js b/src/plugin/customParseFormat/index.js index 034b5d5..cc338b0 100644 --- a/src/plugin/customParseFormat/index.js +++ b/src/plugin/customParseFormat/index.js @@ -7,8 +7,6 @@ const match2 = /\d\d/ // 00 - 99 const match3 = /\d{3}/ // 000 - 999 const match4 = /\d{4}/ // 0000 - 9999 const match1to2 = /\d\d?/ // 0 - 99 -const matchUpperCaseAMPM = /[AP]M/ -const matchLowerCaseAMPM = /[ap]m/ const matchSigned = /[+-]?\d+/ // -inf - inf const matchOffset = /[+-]\d\d:?\d\d/ // +00:00 -00:00 +0000 or -0000 const matchWord = /\d*[^\s\d-:/()]+/ // Word @@ -38,13 +36,28 @@ const getLocalePart = (name) => { part.indexOf ? part : part.s.concat(part.f) ) } - +const meridiemMatch = (input, isLowerCase) => { + let isAfternoon + const { meridiem } = locale + if (!meridiem) { + isAfternoon = input === (isLowerCase ? 'pm' : 'PM') + } else { + for (let i = 1; i <= 24; i += 1) { + // todo: fix input === meridiem(i, 0, isLowerCase) + if (input.indexOf(meridiem(i, 0, isLowerCase)) > -1) { + isAfternoon = i > 12 + break + } + } + } + return isAfternoon +} const expressions = { - A: [matchUpperCaseAMPM, function (input) { - this.afternoon = input === 'PM' + A: [matchWord, function (input) { + this.afternoon = meridiemMatch(input, false) }], - a: [matchLowerCaseAMPM, function (input) { - this.afternoon = input === 'pm' + a: [matchWord, function (input) { + this.afternoon = meridiemMatch(input, true) }], S: [match1, function (input) { this.milliseconds = +input * 100 diff --git a/test/plugin/customParseFormat.test.js b/test/plugin/customParseFormat.test.js index e1cf9e5..03b446a 100644 --- a/test/plugin/customParseFormat.test.js +++ b/test/plugin/customParseFormat.test.js @@ -1,11 +1,11 @@ import MockDate from 'mockdate' import moment from 'moment' import dayjs from '../../src' -import customParseFormat from '../../src/plugin/customParseFormat' -import localizedFormats from '../../src/plugin/localizedFormat' +import '../../src/locale/ru' import uk from '../../src/locale/uk' import '../../src/locale/zh-cn' -import '../../src/locale/ru' +import customParseFormat from '../../src/plugin/customParseFormat' +import localizedFormats from '../../src/plugin/localizedFormat' dayjs.extend(customParseFormat) dayjs.extend(localizedFormats) @@ -305,3 +305,18 @@ describe('Array format support', () => { expect(dayjs(input, format, 'zh-cn', true).format('YYYY MMMM DD')).toBe(input) }) }) + +describe('meridiem locale', () => { + const format = 'YYYY年M月D日Ah点mm分ss秒' + const format2 = 'YYYY-MM-DD HH:mm:ss' + it('AM', () => { + const input = '2018-05-02 01:02:03' + const date = dayjs(input).locale('zh-cn').format(format) + expect(dayjs(date, format, 'zh-cn').format(format2)).toBe(input) + }) + it('PM', () => { + const input = '2018-05-02 20:02:03' + const date = dayjs(input).locale('zh-cn').format(format) + expect(dayjs(date, format, 'zh-cn').format(format2)).toBe(input) + }) +})