hsu-utils 0.0.49 → 0.0.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  /*!
2
2
  *
3
- * hsu-utils v0.0.48
3
+ * hsu-utils v0.0.49
4
4
  *
5
5
  * some front-end utils
6
6
  *
@@ -5,6 +5,9 @@ export interface GetDateRangeOptions {
5
5
  type: DateRangeType;
6
6
  baseDate?: string | Date | Dayjs;
7
7
  unit?: 'day' | 'week' | 'month' | 'year';
8
+ minDate?: string | Date | Dayjs;
9
+ maxDate?: string | Date | Dayjs;
10
+ hasTime?: boolean;
8
11
  }
9
12
  export type DateRangeResult = [string, string];
10
13
  export default function getDateRange(options: GetDateRangeOptions): DateRangeResult;
@@ -3,36 +3,49 @@ import quarterOfYear from 'dayjs/plugin/quarterOfYear';
3
3
  import weekOfYear from 'dayjs/plugin/weekOfYear';
4
4
  dayjs.extend(quarterOfYear);
5
5
  dayjs.extend(weekOfYear);
6
- function getFormat(type, unit = 'day') {
6
+ function getFormat(type, unit = 'day', hasTime = true) {
7
+ let baseFormat;
7
8
  switch (type) {
8
9
  case 'today':
9
- return 'YYYY-MM-DD';
10
+ baseFormat = 'YYYY-MM-DD';
11
+ break;
10
12
  case 'thisWeek':
11
- return 'YYYY-MM-DD';
13
+ baseFormat = 'YYYY-MM-DD';
14
+ break;
12
15
  case 'thisMonth':
13
- return 'YYYY-MM';
16
+ baseFormat = 'YYYY-MM';
17
+ break;
14
18
  case 'thisQuarter':
15
- return 'YYYY-[Q]Q';
19
+ baseFormat = 'YYYY-[Q]Q';
20
+ break;
16
21
  case 'thisYear':
17
- return 'YYYY';
22
+ baseFormat = 'YYYY';
23
+ break;
18
24
  case 'past':
19
25
  case 'future':
20
26
  default:
27
+ switch (unit) {
28
+ case 'year':
29
+ baseFormat = 'YYYY';
30
+ break;
31
+ case 'month':
32
+ baseFormat = 'YYYY-MM';
33
+ break;
34
+ case 'week':
35
+ case 'day':
36
+ default:
37
+ baseFormat = 'YYYY-MM-DD';
38
+ break;
39
+ }
21
40
  break;
22
41
  }
23
- switch (unit) {
24
- case 'year':
25
- return 'YYYY';
26
- case 'month':
27
- return 'YYYY-MM';
28
- case 'week':
29
- case 'day':
30
- default:
31
- return 'YYYY-MM-DD';
42
+ if (hasTime && (baseFormat.includes('DD') || baseFormat === 'YYYY-MM-DD')) {
43
+ return `${baseFormat} HH:mm:ss`;
32
44
  }
45
+ return baseFormat;
33
46
  }
34
47
  export default function getDateRange(options) {
35
- const { type, amount = 0, baseDate, unit = 'day' } = options;
48
+ const { type, amount = 0, baseDate, unit = 'day', minDate: minDateLimit, maxDate: maxDateLimit, hasTime = true } = options;
36
49
  const base = baseDate ? dayjs(baseDate) : dayjs();
37
50
  let minDate;
38
51
  let maxDate;
@@ -40,10 +53,34 @@ export default function getDateRange(options) {
40
53
  case 'past':
41
54
  maxDate = base.endOf(unit);
42
55
  minDate = base.subtract(amount, unit).startOf(unit);
56
+ if (minDateLimit) {
57
+ const minLimit = dayjs(minDateLimit).startOf(unit);
58
+ if (minLimit.isAfter(minDate)) {
59
+ minDate = minLimit;
60
+ }
61
+ }
62
+ if (maxDateLimit) {
63
+ const maxLimit = dayjs(maxDateLimit).endOf(unit);
64
+ if (maxLimit.isBefore(maxDate)) {
65
+ maxDate = maxLimit;
66
+ }
67
+ }
43
68
  break;
44
69
  case 'future':
45
70
  minDate = base.startOf(unit);
46
71
  maxDate = base.add(amount, unit).endOf(unit);
72
+ if (minDateLimit) {
73
+ const minLimit = dayjs(minDateLimit).startOf(unit);
74
+ if (minLimit.isAfter(minDate)) {
75
+ minDate = minLimit;
76
+ }
77
+ }
78
+ if (maxDateLimit) {
79
+ const maxLimit = dayjs(maxDateLimit).endOf(unit);
80
+ if (maxLimit.isBefore(maxDate)) {
81
+ maxDate = maxLimit;
82
+ }
83
+ }
47
84
  break;
48
85
  case 'today':
49
86
  minDate = base.startOf('day');
@@ -68,7 +105,7 @@ export default function getDateRange(options) {
68
105
  default:
69
106
  throw new Error(`不支持的类型: ${type}`);
70
107
  }
71
- const format = getFormat(type, unit);
108
+ const format = getFormat(type, unit, hasTime);
72
109
  const min = minDate.format(format);
73
110
  const max = maxDate.format(format);
74
111
  return [min, max];
@@ -5,6 +5,9 @@ export interface GetDateRangeOptions {
5
5
  type: DateRangeType;
6
6
  baseDate?: string | Date | Dayjs;
7
7
  unit?: 'day' | 'week' | 'month' | 'year';
8
+ minDate?: string | Date | Dayjs;
9
+ maxDate?: string | Date | Dayjs;
10
+ hasTime?: boolean;
8
11
  }
9
12
  export type DateRangeResult = [string, string];
10
13
  export default function getDateRange(options: GetDateRangeOptions): DateRangeResult;
@@ -8,37 +8,51 @@ var quarterOfYear_1 = __importDefault(require("dayjs/plugin/quarterOfYear"));
8
8
  var weekOfYear_1 = __importDefault(require("dayjs/plugin/weekOfYear"));
9
9
  dayjs_1.default.extend(quarterOfYear_1.default);
10
10
  dayjs_1.default.extend(weekOfYear_1.default);
11
- function getFormat(type, unit) {
11
+ function getFormat(type, unit, hasTime) {
12
12
  if (unit === void 0) { unit = 'day'; }
13
+ if (hasTime === void 0) { hasTime = true; }
14
+ var baseFormat;
13
15
  switch (type) {
14
16
  case 'today':
15
- return 'YYYY-MM-DD';
17
+ baseFormat = 'YYYY-MM-DD';
18
+ break;
16
19
  case 'thisWeek':
17
- return 'YYYY-MM-DD';
20
+ baseFormat = 'YYYY-MM-DD';
21
+ break;
18
22
  case 'thisMonth':
19
- return 'YYYY-MM';
23
+ baseFormat = 'YYYY-MM';
24
+ break;
20
25
  case 'thisQuarter':
21
- return 'YYYY-[Q]Q';
26
+ baseFormat = 'YYYY-[Q]Q';
27
+ break;
22
28
  case 'thisYear':
23
- return 'YYYY';
29
+ baseFormat = 'YYYY';
30
+ break;
24
31
  case 'past':
25
32
  case 'future':
26
33
  default:
34
+ switch (unit) {
35
+ case 'year':
36
+ baseFormat = 'YYYY';
37
+ break;
38
+ case 'month':
39
+ baseFormat = 'YYYY-MM';
40
+ break;
41
+ case 'week':
42
+ case 'day':
43
+ default:
44
+ baseFormat = 'YYYY-MM-DD';
45
+ break;
46
+ }
27
47
  break;
28
48
  }
29
- switch (unit) {
30
- case 'year':
31
- return 'YYYY';
32
- case 'month':
33
- return 'YYYY-MM';
34
- case 'week':
35
- case 'day':
36
- default:
37
- return 'YYYY-MM-DD';
49
+ if (hasTime && (baseFormat.includes('DD') || baseFormat === 'YYYY-MM-DD')) {
50
+ return "".concat(baseFormat, " HH:mm:ss");
38
51
  }
52
+ return baseFormat;
39
53
  }
40
54
  function getDateRange(options) {
41
- var type = options.type, _a = options.amount, amount = _a === void 0 ? 0 : _a, baseDate = options.baseDate, _b = options.unit, unit = _b === void 0 ? 'day' : _b;
55
+ var type = options.type, _a = options.amount, amount = _a === void 0 ? 0 : _a, baseDate = options.baseDate, _b = options.unit, unit = _b === void 0 ? 'day' : _b, minDateLimit = options.minDate, maxDateLimit = options.maxDate, _c = options.hasTime, hasTime = _c === void 0 ? true : _c;
42
56
  var base = baseDate ? (0, dayjs_1.default)(baseDate) : (0, dayjs_1.default)();
43
57
  var minDate;
44
58
  var maxDate;
@@ -46,10 +60,34 @@ function getDateRange(options) {
46
60
  case 'past':
47
61
  maxDate = base.endOf(unit);
48
62
  minDate = base.subtract(amount, unit).startOf(unit);
63
+ if (minDateLimit) {
64
+ var minLimit = (0, dayjs_1.default)(minDateLimit).startOf(unit);
65
+ if (minLimit.isAfter(minDate)) {
66
+ minDate = minLimit;
67
+ }
68
+ }
69
+ if (maxDateLimit) {
70
+ var maxLimit = (0, dayjs_1.default)(maxDateLimit).endOf(unit);
71
+ if (maxLimit.isBefore(maxDate)) {
72
+ maxDate = maxLimit;
73
+ }
74
+ }
49
75
  break;
50
76
  case 'future':
51
77
  minDate = base.startOf(unit);
52
78
  maxDate = base.add(amount, unit).endOf(unit);
79
+ if (minDateLimit) {
80
+ var minLimit = (0, dayjs_1.default)(minDateLimit).startOf(unit);
81
+ if (minLimit.isAfter(minDate)) {
82
+ minDate = minLimit;
83
+ }
84
+ }
85
+ if (maxDateLimit) {
86
+ var maxLimit = (0, dayjs_1.default)(maxDateLimit).endOf(unit);
87
+ if (maxLimit.isBefore(maxDate)) {
88
+ maxDate = maxLimit;
89
+ }
90
+ }
53
91
  break;
54
92
  case 'today':
55
93
  minDate = base.startOf('day');
@@ -74,7 +112,7 @@ function getDateRange(options) {
74
112
  default:
75
113
  throw new Error("\u4E0D\u652F\u6301\u7684\u7C7B\u578B: ".concat(type));
76
114
  }
77
- var format = getFormat(type, unit);
115
+ var format = getFormat(type, unit, hasTime);
78
116
  var min = minDate.format(format);
79
117
  var max = maxDate.format(format);
80
118
  return [min, max];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hsu-utils",
3
- "version": "0.0.49",
3
+ "version": "0.0.50",
4
4
  "description": "some front-end utils",
5
5
  "repository": "git@github.com:VitaTsui/hsu-utils.git",
6
6
  "author": "VitaHsu <vitahsu7@gmail.com>",