Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yee94 authored Jul 26, 2023
1 parent ff51124 commit e44cf8b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"resolutions": {
"rollup-plugin-typescript2": "^0.34.1"
},
"dependencies": {},
"devDependencies": {
"@commitlint/cli": "^17.3.0",
"@commitlint/config-conventional": "^17.3.0",
Expand All @@ -54,6 +53,7 @@
"@testing-library/jest-dom": "^5.0.0",
"@types/fs-extra": "^9.0.13",
"@types/jest": "^24.0.18",
"@types/moment": "^2.13.0",
"@types/node": "^18.11.9",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
Expand Down Expand Up @@ -83,6 +83,7 @@
"lerna": "^6.0.0",
"lint-staged": "^13.0.4",
"mfetch": "^0.2.27",
"moment": "^2.29.4",
"prettier": "^2.2.1",
"pretty-format": "^29.3.1",
"pretty-quick": "^3.1.0",
Expand Down
69 changes: 69 additions & 0 deletions packages/components/src/__builtins__/moment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// FOR: https://github.com/ant-design/antd-moment-webpack-plugin
import { isArr, isFn, isEmpty } from '@formily/shared'
import moment, { MomentInput, Moment } from 'moment'

export function dayjsable(value: MomentInput, format?: string): Moment
export function dayjsable(value: MomentInput[], format?: string): Moment[]

export function dayjsable(
value: MomentInput | MomentInput[],
format?: string
): any {
if (!value) return value
if (Array.isArray(value)) {
return value.map((val) => {
const date = moment(val, format)
if (date.isValid()) return date
const _date = moment(val)
return _date.isValid() ? _date : val
})
} else {
const date = moment(value, format)
if (date.isValid()) return date
const _date = moment(value)
return _date.isValid() ? _date : value
}
}

export const formatDayjsValue = (
value: any,
format: any,
placeholder?: string
): string | string[] => {
const validFormatDate = (date: any, format: any) => {
if (typeof date === 'number') {
return moment(date).format(format)
}
const _date = moment(date, format)
return _date.isValid() ? _date.format(format) : date
}

const formatDate = (date: any, format: any, i = 0) => {
if (!date) return placeholder
if (isArr(format)) {
const _format = format[i]
if (isFn(_format)) {
return _format(date)
}
if (isEmpty(_format)) {
return date
}
return validFormatDate(date, _format)
} else {
if (isFn(format)) {
return format(date)
}
if (isEmpty(format)) {
return date
}
return validFormatDate(date, format)
}
}
if (isArr(value)) {
return value.map((val, index) => {
return formatDate(val, format, index)
})
} else {
return value ? formatDate(value, format) : value || placeholder
}
}

0 comments on commit e44cf8b

Please sign in to comment.