From ed386f4d099d4907cb7503424f83a1020eb8c778 Mon Sep 17 00:00:00 2001 From: danyue <37496285+heihei12305@users.noreply.github.com> Date: Fri, 30 Sep 2022 13:22:48 +0800 Subject: [PATCH] fix(next/antd): chore formatMomentValue (#3432) * fix: chore formatMomentValue * feat: add moment test * feat: update moment test * feat: chore formatMomentValue --- packages/antd/__tests__/moment.spec.ts | 12 +++++++++ packages/antd/src/__builtins__/moment.ts | 34 +++++++++--------------- packages/next/__tests__/moment.spec.ts | 12 +++++++++ packages/next/src/__builtins__/moment.ts | 34 +++++++++--------------- 4 files changed, 50 insertions(+), 42 deletions(-) diff --git a/packages/antd/__tests__/moment.spec.ts b/packages/antd/__tests__/moment.spec.ts index 2e1ad350237..790264216d4 100644 --- a/packages/antd/__tests__/moment.spec.ts +++ b/packages/antd/__tests__/moment.spec.ts @@ -23,12 +23,24 @@ test('formatMomentValue is usable', () => { ) expect(formatMomentValue('12:11', 'HH:mm')).toBe('12:11') expect(formatMomentValue('12:11:11', 'HH:mm:ss')).toBe('12:11:11') + expect(formatMomentValue(['12:11'], ['HH:mm'])).toEqual(['12:11']) + expect(formatMomentValue(['12:11:11'], ['HH:mm:ss'])).toEqual(['12:11:11']) expect(formatMomentValue(1663155911097, 'YYYY-MM-DD HH:mm:ss')).toBe( moment(1663155911097).format('YYYY-MM-DD HH:mm:ss') ) expect(formatMomentValue([1663155911097], ['YYYY-MM-DD HH:mm:ss'])).toEqual([ moment(1663155911097).format('YYYY-MM-DD HH:mm:ss'), ]) + expect( + formatMomentValue('2022-09-15T09:56:26.000Z', 'YYYY-MM-DD HH:mm:ss') + ).toBe(moment('2022-09-15T09:56:26.000Z').format('YYYY-MM-DD HH:mm:ss')) + expect( + formatMomentValue(['2022-09-15T09:56:26.000Z'], ['YYYY-MM-DD HH:mm:ss']) + ).toEqual([moment('2022-09-15T09:56:26.000Z').format('YYYY-MM-DD HH:mm:ss')]) + expect(formatMomentValue('2022-09-15 09:56:26', 'HH:mm:ss')).toBe('09:56:26') + expect(formatMomentValue(['2022-09-15 09:56:26'], ['HH:mm:ss'])).toEqual([ + '09:56:26', + ]) expect( formatMomentValue( ['2021-12-21 15:47:00', '2021-12-29 15:47:00'], diff --git a/packages/antd/src/__builtins__/moment.ts b/packages/antd/src/__builtins__/moment.ts index 6c76b959d93..6cd32cf7193 100644 --- a/packages/antd/src/__builtins__/moment.ts +++ b/packages/antd/src/__builtins__/moment.ts @@ -16,30 +16,22 @@ export const formatMomentValue = ( ): string | string[] => { const formatDate = (date: any, format: any, i = 0) => { if (!date) return placeholder + const TIME_REG = /^(?:[01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$/ + let _format = format if (isArr(format)) { - const _format = format[i] - if (isFn(_format)) { - return _format(date) - } - if (isEmpty(_format)) { - return date - } - if (typeof date === 'number') { - return moment(date).format(_format) - } + _format = format[i] + } + if (isFn(_format)) { + return _format(date) + } + if (isEmpty(_format)) { + return date + } + // moment '19:55:22' 下需要传入第二个参数 + if (TIME_REG.test(date)) { return moment(date, _format).format(_format) - } else { - if (isFn(format)) { - return format(date) - } - if (isEmpty(format)) { - return date - } - if (typeof date === 'number') { - return moment(date).format(format) - } - return moment(date, format).format(format) } + return moment(date).format(_format) } if (isArr(value)) { return value.map((val, index) => { diff --git a/packages/next/__tests__/moment.spec.ts b/packages/next/__tests__/moment.spec.ts index d4c857a0cb1..f5ba8ba64f5 100644 --- a/packages/next/__tests__/moment.spec.ts +++ b/packages/next/__tests__/moment.spec.ts @@ -23,12 +23,24 @@ test('formatMomentValue is usable', () => { ) expect(formatMomentValue('12:11', 'HH:mm')).toBe('12:11') expect(formatMomentValue('12:11:11', 'HH:mm:ss')).toBe('12:11:11') + expect(formatMomentValue(['12:11'], ['HH:mm'])).toEqual(['12:11']) + expect(formatMomentValue(['12:11:11'], ['HH:mm:ss'])).toEqual(['12:11:11']) expect(formatMomentValue(1663155911097, 'YYYY-MM-DD HH:mm:ss')).toBe( moment(1663155911097).format('YYYY-MM-DD HH:mm:ss') ) expect(formatMomentValue([1663155911097], ['YYYY-MM-DD HH:mm:ss'])).toEqual([ moment(1663155911097).format('YYYY-MM-DD HH:mm:ss'), ]) + expect( + formatMomentValue('2022-09-15T09:56:26.000Z', 'YYYY-MM-DD HH:mm:ss') + ).toBe(moment('2022-09-15T09:56:26.000Z').format('YYYY-MM-DD HH:mm:ss')) + expect( + formatMomentValue(['2022-09-15T09:56:26.000Z'], ['YYYY-MM-DD HH:mm:ss']) + ).toEqual([moment('2022-09-15T09:56:26.000Z').format('YYYY-MM-DD HH:mm:ss')]) + expect(formatMomentValue('2022-09-15 09:56:26', 'HH:mm:ss')).toBe('09:56:26') + expect(formatMomentValue(['2022-09-15 09:56:26'], ['HH:mm:ss'])).toEqual([ + '09:56:26', + ]) expect( formatMomentValue( ['2021-12-21 15:47:00', '2021-12-29 15:47:00'], diff --git a/packages/next/src/__builtins__/moment.ts b/packages/next/src/__builtins__/moment.ts index 23a9953c238..534e02e4624 100644 --- a/packages/next/src/__builtins__/moment.ts +++ b/packages/next/src/__builtins__/moment.ts @@ -20,30 +20,22 @@ export const formatMomentValue = ( ): string | string[] => { const formatDate = (date: any, format: any, i = 0) => { if (!date) return placeholder + const TIME_REG = /^(?:[01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$/ + let _format = format if (isArr(format)) { - const _format = format[i] - if (isFn(_format)) { - return _format(date) - } - if (isEmpty(_format)) { - return date - } - if (typeof date === 'number') { - return moment(date).format(_format) - } + _format = format[i] + } + if (isFn(_format)) { + return _format(date) + } + if (isEmpty(_format)) { + return date + } + // moment '19:55:22' 下需要传入第二个参数 + if (TIME_REG.test(date)) { return moment(date, _format).format(_format) - } else { - if (isFn(format)) { - return format(date) - } - if (isEmpty(format)) { - return date - } - if (typeof date === 'number') { - return moment(date).format(format) - } - return moment(date, format).format(format) } + return moment(date).format(_format) } if (isArr(value)) { return value.map((val, index) => {