• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SkylerHu / antd-restful / #83

22 Sep 2025 12:25AM UTC coverage: 83.121% (+0.5%) from 82.661%
#83

push

web-flow
Merge 9736826fa into 186ad040d

1325 of 1679 branches covered (78.92%)

Branch coverage included in aggregate %.

84 of 87 new or added lines in 9 files covered. (96.55%)

1 existing line in 1 file now uncovered.

1743 of 2012 relevant lines covered (86.63%)

71.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.97
/src/common/dateUtils.js
1
/**
2
 * 根据antd版本自动选择时间库并创建日期对象
3
 * antd4使用moment,antd5+使用dayjs
4
 */
5
import { version as antdVersion } from "antd";
6
import { isString } from "src/common/typeTools";
7

8
let moment = null;
5✔
9
let dayjs = null;
5✔
10

11
// 检测antd版本
12
export function detectAntdVersion() {
13
  const majorVersion = parseInt(antdVersion.split(".")[0]);
216✔
14
  return majorVersion;
216✔
15
}
16

17
// 懒加载moment
18
function getMoment() {
19
  if (moment === null) {
×
20
    try {
×
21
      moment = require("moment");
×
22
    } catch (e) {
23
      // moment is not installed, please install it for antd4 compatibility
24
      return null;
×
25
    }
26
  }
27
  return moment;
×
28
}
29

30
// 懒加载dayjs
31
function getDayjs() {
32
  if (dayjs === null) {
202✔
33
    try {
3✔
34
      dayjs = require("dayjs");
3✔
35
    } catch (e) {
36
      // dayjs is not installed, please install it for antd5 compatibility
37
      return null;
×
38
    }
39
  }
40
  return dayjs;
202✔
41
}
42

43
/**
44
 * 创建日期对象
45
 * @param {string} dateInput - 日期字符串输入
46
 * @param {string} format - 日期格式(仅在使用moment时有效)
47
 * @returns {object|null} 返回对应的日期对象或null
48
 */
49
export function createDate(dateInput, format = null) {
114✔
50
  if (!isString(dateInput)) {
209✔
51
    return null;
7✔
52
  }
53

54
  const version = detectAntdVersion();
202✔
55

56
  if (version >= 5) {
202!
57
    // antd5+ 使用dayjs
58
    const dayjsInstance = getDayjs();
202✔
59
    if (!dayjsInstance) {
202!
60
      // dayjs is required for antd5+
61
      return null;
×
62
    }
63
    try {
202✔
64
      if (format) {
202✔
65
        return dayjsInstance(dateInput, format);
57✔
66
      } else {
67
        return dayjsInstance(dateInput);
145✔
68
      }
69
    } catch (e) {
70
      // Failed to parse date with dayjs
71
      return null;
×
72
    }
73
  } else {
74
    // antd4 使用moment
75
    const momentInstance = getMoment();
×
76
    if (!momentInstance) {
×
77
      // moment is required for antd4
78
      return null;
×
79
    }
UNCOV
80
    try {
×
81
      if (format) {
×
82
        return momentInstance(dateInput, format);
×
83
      } else {
84
        return momentInstance(dateInput);
×
85
      }
86
    } catch (e) {
87
      // Failed to parse date with moment
88
      return null;
×
89
    }
90
  }
91
}
92

93
/**
94
 * 检查是否为有效日期
95
 * @param {string} dateInput - 日期字符串输入
96
 * @param {string} format - 日期格式(仅在使用moment时有效)
97
 * @returns {boolean} 是否为有效日期
98
 */
99
export function isValidDate(dateInput, format = null) {
8✔
100
  const date = createDate(dateInput, format);
16✔
101
  if (!date) return false;
16✔
102

103
  const version = detectAntdVersion();
14✔
104

105
  if (version >= 5) {
14!
106
    return date.isValid();
14✔
107
  } else {
108
    return date.isValid();
×
109
  }
110
}
111

112
/**
113
 * 格式化日期
114
 * @param {string} dateInput - 日期字符串输入
115
 * @param {string} format - 输出格式
116
 * @param {string} inputFormat - 输入格式(仅在使用moment时有效)
117
 * @returns {string} 格式化后的日期字符串
118
 */
119
export function formatDate(dateInput, format = "YYYY-MM-DD", inputFormat = null) {
29✔
120
  const date = createDate(dateInput, inputFormat);
43✔
121
  if (!date) return "";
43✔
122

123
  try {
41✔
124
    return date.format(format);
41✔
125
  } catch (e) {
126
    // Failed to format date
127
    return "";
×
128
  }
129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc