@nocobase/utils 1.7.0-beta.31 → 1.7.0-beta.33

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.
package/lib/client.d.ts CHANGED
@@ -28,4 +28,5 @@ export * from './parseHTML';
28
28
  export * from './uid';
29
29
  export * from './url';
30
30
  export * from './transformMultiColumnToSingleColumn';
31
+ export * from './dateRangeUtils';
31
32
  export { dayjs, lodash };
package/lib/client.js CHANGED
@@ -63,6 +63,7 @@ __reExport(client_exports, require("./parseHTML"), module.exports);
63
63
  __reExport(client_exports, require("./uid"), module.exports);
64
64
  __reExport(client_exports, require("./url"), module.exports);
65
65
  __reExport(client_exports, require("./transformMultiColumnToSingleColumn"), module.exports);
66
+ __reExport(client_exports, require("./dateRangeUtils"), module.exports);
66
67
  // Annotate the CommonJS export names for ESM import in node:
67
68
  0 && (module.exports = {
68
69
  dayjs,
@@ -86,5 +87,6 @@ __reExport(client_exports, require("./transformMultiColumnToSingleColumn"), modu
86
87
  ...require("./parseHTML"),
87
88
  ...require("./uid"),
88
89
  ...require("./url"),
89
- ...require("./transformMultiColumnToSingleColumn")
90
+ ...require("./transformMultiColumnToSingleColumn"),
91
+ ...require("./dateRangeUtils")
90
92
  });
@@ -0,0 +1,22 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ type DateUnit = 'day' | 'week' | 'isoWeek' | 'month' | 'quarter' | 'year';
10
+ type RangeType = 'today' | 'yesterday' | 'tomorrow' | 'thisWeek' | 'lastWeek' | 'nextWeek' | 'thisMonth' | 'lastMonth' | 'nextMonth' | 'thisQuarter' | 'lastQuarter' | 'nextQuarter' | 'thisYear' | 'lastYear' | 'nextYear' | 'past' | 'next';
11
+ interface RangeParams {
12
+ type: RangeType;
13
+ unit?: DateUnit;
14
+ number?: number;
15
+ timezone?: string;
16
+ }
17
+ export declare const getOffsetRangeByParams: (params: RangeParams) => [string, string];
18
+ /**
19
+ * 获取某个时间范围的起止时间(字符串格式)
20
+ */
21
+ export declare const getDayRangeByParams: (params: RangeParams) => [string, string];
22
+ export {};
@@ -0,0 +1,102 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __defProp = Object.defineProperty;
11
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
+ var __getOwnPropNames = Object.getOwnPropertyNames;
13
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
14
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
+ var __export = (target, all) => {
16
+ for (var name in all)
17
+ __defProp(target, name, { get: all[name], enumerable: true });
18
+ };
19
+ var __copyProps = (to, from, except, desc) => {
20
+ if (from && typeof from === "object" || typeof from === "function") {
21
+ for (let key of __getOwnPropNames(from))
22
+ if (!__hasOwnProp.call(to, key) && key !== except)
23
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
24
+ }
25
+ return to;
26
+ };
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var dateRangeUtils_exports = {};
29
+ __export(dateRangeUtils_exports, {
30
+ getDayRangeByParams: () => getDayRangeByParams,
31
+ getOffsetRangeByParams: () => getOffsetRangeByParams
32
+ });
33
+ module.exports = __toCommonJS(dateRangeUtils_exports);
34
+ var import_dayjs2 = require("./dayjs");
35
+ const getNow = /* @__PURE__ */ __name((tz) => {
36
+ if (!tz) return (0, import_dayjs2.dayjs)();
37
+ if (/^[+-]\d{2}:\d{2}$/.test(tz)) {
38
+ const [sign, hour, minute] = tz.match(/([+-])(\d{2}):(\d{2})/).slice(1);
39
+ const offset = (parseInt(hour) * 60 + parseInt(minute)) * (sign === "+" ? 1 : -1);
40
+ return (0, import_dayjs2.dayjs)().utcOffset(offset);
41
+ }
42
+ return (0, import_dayjs2.dayjs)().tz(tz);
43
+ }, "getNow");
44
+ const getOffsetRangeByParams = /* @__PURE__ */ __name((params) => {
45
+ const { type, unit = "day", number = 1, timezone } = params;
46
+ const now = getNow(timezone);
47
+ const actualUnit = unit === "week" ? "isoWeek" : unit;
48
+ let start;
49
+ let end;
50
+ if (type === "past") {
51
+ const base = now.startOf(actualUnit);
52
+ start = base.subtract(number, unit).startOf(actualUnit);
53
+ end = base.subtract(1, unit).endOf(actualUnit);
54
+ } else if (type === "next") {
55
+ const base = now.startOf(actualUnit);
56
+ start = base.add(1, unit).startOf(actualUnit);
57
+ end = start.add(number - 1, unit).endOf(actualUnit);
58
+ } else {
59
+ throw new Error(`Unsupported type: ${type}`);
60
+ }
61
+ return [start.format("YYYY-MM-DD HH:mm:ss"), end.format("YYYY-MM-DD HH:mm:ss")];
62
+ }, "getOffsetRangeByParams");
63
+ const getStart = /* @__PURE__ */ __name((offset, unit, tz) => {
64
+ const actualUnit = unit === "isoWeek" ? "week" : unit;
65
+ return getNow(tz).add(offset, actualUnit).startOf(unit);
66
+ }, "getStart");
67
+ const getEnd = /* @__PURE__ */ __name((offset, unit, tz) => {
68
+ const actualUnit = unit === "isoWeek" ? "week" : unit;
69
+ return getNow(tz).add(offset, actualUnit).endOf(unit);
70
+ }, "getEnd");
71
+ const strategies = {
72
+ today: /* @__PURE__ */ __name((params) => [getStart(0, "day", params == null ? void 0 : params.timezone), getEnd(0, "day", params == null ? void 0 : params.timezone)], "today"),
73
+ yesterday: /* @__PURE__ */ __name((params) => [getStart(-1, "day", params == null ? void 0 : params.timezone), getEnd(-1, "day", params == null ? void 0 : params.timezone)], "yesterday"),
74
+ tomorrow: /* @__PURE__ */ __name((params) => [getStart(1, "day", params == null ? void 0 : params.timezone), getEnd(1, "day", params == null ? void 0 : params.timezone)], "tomorrow"),
75
+ thisWeek: /* @__PURE__ */ __name((params) => [getStart(0, "isoWeek", params == null ? void 0 : params.timezone), getEnd(0, "isoWeek", params == null ? void 0 : params.timezone)], "thisWeek"),
76
+ lastWeek: /* @__PURE__ */ __name((params) => [getStart(-1, "isoWeek", params == null ? void 0 : params.timezone), getEnd(-1, "isoWeek", params == null ? void 0 : params.timezone)], "lastWeek"),
77
+ nextWeek: /* @__PURE__ */ __name((params) => [getStart(1, "isoWeek", params == null ? void 0 : params.timezone), getEnd(1, "isoWeek", params == null ? void 0 : params.timezone)], "nextWeek"),
78
+ thisMonth: /* @__PURE__ */ __name((params) => [getStart(0, "month", params == null ? void 0 : params.timezone), getEnd(0, "month", params == null ? void 0 : params.timezone)], "thisMonth"),
79
+ lastMonth: /* @__PURE__ */ __name((params) => [getStart(-1, "month", params == null ? void 0 : params.timezone), getEnd(-1, "month", params == null ? void 0 : params.timezone)], "lastMonth"),
80
+ nextMonth: /* @__PURE__ */ __name((params) => [getStart(1, "month", params == null ? void 0 : params.timezone), getEnd(1, "month", params == null ? void 0 : params.timezone)], "nextMonth"),
81
+ thisQuarter: /* @__PURE__ */ __name((params) => [getStart(0, "quarter", params == null ? void 0 : params.timezone), getEnd(0, "quarter", params == null ? void 0 : params.timezone)], "thisQuarter"),
82
+ lastQuarter: /* @__PURE__ */ __name((params) => [getStart(-1, "quarter", params == null ? void 0 : params.timezone), getEnd(-1, "quarter", params == null ? void 0 : params.timezone)], "lastQuarter"),
83
+ nextQuarter: /* @__PURE__ */ __name((params) => [getStart(1, "quarter", params == null ? void 0 : params.timezone), getEnd(1, "quarter", params == null ? void 0 : params.timezone)], "nextQuarter"),
84
+ thisYear: /* @__PURE__ */ __name((params) => [getStart(0, "year", params == null ? void 0 : params.timezone), getEnd(0, "year", params == null ? void 0 : params.timezone)], "thisYear"),
85
+ lastYear: /* @__PURE__ */ __name((params) => [getStart(-1, "year", params == null ? void 0 : params.timezone), getEnd(-1, "year", params == null ? void 0 : params.timezone)], "lastYear"),
86
+ nextYear: /* @__PURE__ */ __name((params) => [getStart(1, "year", params == null ? void 0 : params.timezone), getEnd(1, "year", params == null ? void 0 : params.timezone)], "nextYear")
87
+ };
88
+ const getDayRangeByParams = /* @__PURE__ */ __name((params) => {
89
+ console.log(params);
90
+ if (params.type === "past" || params.type === "next") {
91
+ return getOffsetRangeByParams(params);
92
+ }
93
+ const fn = strategies[params.type];
94
+ if (!fn) throw new Error(`Unsupported type: ${params.type}`);
95
+ const [start, end] = fn(params);
96
+ return [start.format("YYYY-MM-DD HH:mm:ss"), end.format("YYYY-MM-DD HH:mm:ss")];
97
+ }, "getDayRangeByParams");
98
+ // Annotate the CommonJS export names for ESM import in node:
99
+ 0 && (module.exports = {
100
+ getDayRangeByParams,
101
+ getOffsetRangeByParams
102
+ });
package/lib/index.d.ts CHANGED
@@ -37,4 +37,5 @@ export * from './url';
37
37
  export * from './i18n';
38
38
  export * from './wrap-middleware';
39
39
  export * from './object-to-cli-args';
40
+ export * from './dateRangeUtils';
40
41
  export { Schema } from '@formily/json-schema';
package/lib/index.js CHANGED
@@ -72,6 +72,7 @@ __reExport(src_exports, require("./url"), module.exports);
72
72
  __reExport(src_exports, require("./i18n"), module.exports);
73
73
  __reExport(src_exports, require("./wrap-middleware"), module.exports);
74
74
  __reExport(src_exports, require("./object-to-cli-args"), module.exports);
75
+ __reExport(src_exports, require("./dateRangeUtils"), module.exports);
75
76
  var import_json_schema = require("@formily/json-schema");
76
77
  // Annotate the CommonJS export names for ESM import in node:
77
78
  0 && (module.exports = {
@@ -105,5 +106,6 @@ var import_json_schema = require("@formily/json-schema");
105
106
  ...require("./url"),
106
107
  ...require("./i18n"),
107
108
  ...require("./wrap-middleware"),
108
- ...require("./object-to-cli-args")
109
+ ...require("./object-to-cli-args"),
110
+ ...require("./dateRangeUtils")
109
111
  });
package/lib/parse-date.js CHANGED
@@ -33,14 +33,16 @@ __export(parse_date_exports, {
33
33
  module.exports = __toCommonJS(parse_date_exports);
34
34
  var import_date = require("./date");
35
35
  var import_dayjs = require("./dayjs");
36
+ var import_dateRangeUtils = require("./dateRangeUtils");
36
37
  function parseUTC(value) {
38
+ var _a;
37
39
  if (value instanceof Date || import_dayjs.dayjs.isDayjs(value)) {
38
40
  return {
39
41
  unit: "utc",
40
42
  start: value.toISOString()
41
43
  };
42
44
  }
43
- if (value.endsWith("Z")) {
45
+ if ((_a = value == null ? void 0 : value.endsWith) == null ? void 0 : _a.call(value, "Z")) {
44
46
  return {
45
47
  unit: "utc",
46
48
  start: value
@@ -186,6 +188,9 @@ function parseDate(value, options = {}) {
186
188
  if (!value) {
187
189
  return;
188
190
  }
191
+ if (value.type) {
192
+ value = (0, import_dateRangeUtils.getDayRangeByParams)({ ...value, ...options });
193
+ }
189
194
  if (Array.isArray(value)) {
190
195
  return parseDateBetween(value, options);
191
196
  }
@@ -53,6 +53,7 @@ var import_moment = __toESM(require("moment"));
53
53
  var import_date = require("./date");
54
54
  var import_dayjs = require("./dayjs");
55
55
  var import_getValuesByPath = require("./getValuesByPath");
56
+ var import_dateRangeUtils = require("./dateRangeUtils");
56
57
  const re = /^\s*\{\{([\s\S]*)\}\}\s*$/;
57
58
  function isBuffer(obj) {
58
59
  return obj && obj.constructor && typeof obj.constructor.isBuffer === "function" && obj.constructor.isBuffer(obj);
@@ -131,6 +132,9 @@ const dateValueWrapper = /* @__PURE__ */ __name((value, timezone) => {
131
132
  if (!value) {
132
133
  return null;
133
134
  }
135
+ if (value.type) {
136
+ value = (0, import_dateRangeUtils.getDayRangeByParams)({ ...value, timezone });
137
+ }
134
138
  if (Array.isArray(value)) {
135
139
  if (value.length === 2) {
136
140
  value.push("[]", timezone);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocobase/utils",
3
- "version": "1.7.0-beta.31",
3
+ "version": "1.7.0-beta.33",
4
4
  "main": "lib/index.js",
5
5
  "types": "./lib/index.d.ts",
6
6
  "license": "AGPL-3.0",
@@ -16,5 +16,5 @@
16
16
  "multer": "^1.4.5-lts.2",
17
17
  "object-path": "^0.11.8"
18
18
  },
19
- "gitHead": "84bf8bcfeeaf3a05086e24cef00507ca209ac3c5"
19
+ "gitHead": "6fede035f813a48250c3e94e755618ea54be61c3"
20
20
  }