Skip to content

Commit

Permalink
fix(next/antd): chore formatMomentValue (#3432)
Browse files Browse the repository at this point in the history
* fix: chore formatMomentValue

* feat: add moment test

* feat: update moment test

* feat: chore formatMomentValue
  • Loading branch information
heihei12305 committed Sep 30, 2022
1 parent 5196f45 commit ed386f4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 42 deletions.
12 changes: 12 additions & 0 deletions packages/antd/__tests__/moment.spec.ts
Expand Up @@ -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'],
Expand Down
34 changes: 13 additions & 21 deletions packages/antd/src/__builtins__/moment.ts
Expand Up @@ -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) => {
Expand Down
12 changes: 12 additions & 0 deletions packages/next/__tests__/moment.spec.ts
Expand Up @@ -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'],
Expand Down
34 changes: 13 additions & 21 deletions packages/next/src/__builtins__/moment.ts
Expand Up @@ -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) => {
Expand Down

0 comments on commit ed386f4

Please sign in to comment.