hsu-utils 0.0.51 → 0.0.53
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/README.md +3 -2
- package/dist/hsu-utils.js +3 -3
- package/dist/hsu-utils.min.js +1 -1
- package/dist/hsu-utils.min.js.LICENSE.txt +1 -1
- package/es/DownloadFile/index.d.ts +1 -0
- package/es/DownloadFile/index.js +20 -1
- package/es/GetDateRange/index.d.ts +1 -1
- package/es/GetDateRange/index.js +5 -1
- package/lib/DownloadFile/index.d.ts +1 -0
- package/lib/DownloadFile/index.js +23 -2
- package/lib/GetDateRange/index.d.ts +1 -1
- package/lib/GetDateRange/index.js +5 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -106,7 +106,7 @@ yarn add hsu-utils
|
|
|
106
106
|
| type | 日期范围类型 | [DateRangeType](#daterangetype) | - | 必填 |
|
|
107
107
|
| amount | 数量(用于过去/未来) | number | 0 | - |
|
|
108
108
|
| baseDate | 基准日期 | string \| Date \| Dayjs | - | 默认为当前日期 |
|
|
109
|
-
| unit | 单位(用于过去/未来) | 'day' \| 'week' \| 'month' \| 'year' | 'day' | -
|
|
109
|
+
| unit | 单位(用于过去/未来) | 'second' \| 'minute' \| 'hour' \| 'day' \| 'week' \| 'month' \| 'year' | 'day' | 时、分、秒单位格式为 YYYY-MM-DD HH:mm:ss |
|
|
110
110
|
| minDate | 最小时间限制 | string \| Date \| Dayjs | - | 用于过去/未来,限制范围最小值 |
|
|
111
111
|
| maxDate | 最大时间限制 | string \| Date \| Dayjs | - | 用于过去/未来,限制范围最大值 |
|
|
112
112
|
| hasTime | 是否包含时间部分 | boolean | false | 控制返回格式是否包含时间 |
|
|
@@ -130,11 +130,12 @@ yarn add hsu-utils
|
|
|
130
130
|
返回时间段数组,格式根据类型和单位自动匹配:
|
|
131
131
|
|
|
132
132
|
- `today` / `thisWeek` / `past`(day/week) / `future`(day/week) → `YYYY-MM-DD` (hasTime 为 true 时: `YYYY-MM-DD HH:mm:ss`)
|
|
133
|
+
- `past`(second/minute/hour) / `future`(second/minute/hour) → `YYYY-MM-DD HH:mm:ss` (固定格式,不受 hasTime 影响)
|
|
133
134
|
- `thisMonth` / `past`(month) / `future`(month) → `YYYY-MM`
|
|
134
135
|
- `thisQuarter` → `YYYY-[Q]Q` (例如: `2024-Q1`)
|
|
135
136
|
- `thisYear` / `past`(year) / `future`(year) → `YYYY`
|
|
136
137
|
|
|
137
|
-
注意:当 `hasTime` 为 `true` 时,包含日期部分的格式会自动添加时间部分 `HH:mm:ss`。默认 `hasTime` 为 `false
|
|
138
|
+
注意:当 `hasTime` 为 `true` 时,包含日期部分的格式会自动添加时间部分 `HH:mm:ss`。默认 `hasTime` 为 `false`。时、分、秒单位固定返回 `YYYY-MM-DD HH:mm:ss` 格式。
|
|
138
139
|
|
|
139
140
|
## License
|
|
140
141
|
|
package/dist/hsu-utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
*
|
|
3
|
-
* hsu-utils v0.0.
|
|
3
|
+
* hsu-utils v0.0.52
|
|
4
4
|
*
|
|
5
5
|
* some front-end utils
|
|
6
6
|
*
|
|
@@ -139,7 +139,7 @@ eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nvar
|
|
|
139
139
|
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
|
|
140
140
|
|
|
141
141
|
"use strict";
|
|
142
|
-
eval("\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nvar __generator = (this && this.__generator) || function (thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [op[0] & 2, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nvar __1 = __webpack_require__(/*! .. */ \"./tools/index.ts\");\nfunction downloadFileByUrl(url, fileName, signal) {\n return __awaiter(this, void 0, void 0, function () {\n var response, arrayBuffer, error_1, downloadElement;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n _a.trys.push([0, 3, , 4]);\n return [4, fetch(url, { signal: signal })];\n case 1:\n response = _a.sent();\n return [4, response.arrayBuffer()];\n case 2:\n arrayBuffer = _a.sent();\n downloadFile(arrayBuffer,
|
|
142
|
+
eval("\nvar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {\n function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }\n return new (P || (P = Promise))(function (resolve, reject) {\n function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }\n function rejected(value) { try { step(generator[\"throw\"](value)); } catch (e) { reject(e); } }\n function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }\n step((generator = generator.apply(thisArg, _arguments || [])).next());\n });\n};\nvar __generator = (this && this.__generator) || function (thisArg, body) {\n var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;\n return g = { next: verb(0), \"throw\": verb(1), \"return\": verb(2) }, typeof Symbol === \"function\" && (g[Symbol.iterator] = function() { return this; }), g;\n function verb(n) { return function (v) { return step([n, v]); }; }\n function step(op) {\n if (f) throw new TypeError(\"Generator is already executing.\");\n while (g && (g = 0, op[0] && (_ = 0)), _) try {\n if (f = 1, y && (t = op[0] & 2 ? y[\"return\"] : op[0] ? y[\"throw\"] || ((t = y[\"return\"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;\n if (y = 0, t) op = [op[0] & 2, t.value];\n switch (op[0]) {\n case 0: case 1: t = op; break;\n case 4: _.label++; return { value: op[1], done: false };\n case 5: _.label++; y = op[1]; op = [0]; continue;\n case 7: op = _.ops.pop(); _.trys.pop(); continue;\n default:\n if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }\n if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }\n if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }\n if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }\n if (t[2]) _.ops.pop();\n _.trys.pop(); continue;\n }\n op = body.call(thisArg, _);\n } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }\n if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };\n }\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nexports.getFileNameFromHeader = void 0;\nvar __1 = __webpack_require__(/*! .. */ \"./tools/index.ts\");\nfunction getFileNameFromHeader(response) {\n var contentDisposition = response.headers.get('Content-Disposition');\n if (!contentDisposition) {\n return null;\n }\n var filenameStarMatch = contentDisposition.match(/filename\\*=UTF-8''([^;]+)/i);\n if (filenameStarMatch) {\n return decodeURIComponent(filenameStarMatch[1]);\n }\n var filenameMatch = contentDisposition.match(/filename=[\"']?([^;\"']+)[\"']?/i);\n if (filenameMatch) {\n return decodeURIComponent(filenameMatch[1]);\n }\n return null;\n}\nexports.getFileNameFromHeader = getFileNameFromHeader;\nfunction downloadFileByUrl(url, fileName, signal) {\n return __awaiter(this, void 0, void 0, function () {\n var response, finalFileName, arrayBuffer, error_1, downloadElement;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n _a.trys.push([0, 3, , 4]);\n return [4, fetch(url, { signal: signal })];\n case 1:\n response = _a.sent();\n finalFileName = fileName;\n if (!finalFileName) {\n finalFileName = getFileNameFromHeader(response) || undefined;\n }\n return [4, response.arrayBuffer()];\n case 2:\n arrayBuffer = _a.sent();\n downloadFile(arrayBuffer, finalFileName);\n return [3, 4];\n case 3:\n error_1 = _a.sent();\n if ((error_1 === null || error_1 === void 0 ? void 0 : error_1.name) !== 'AbortError') {\n downloadElement = document.createElement('a');\n downloadElement.href = url;\n downloadElement.download = decodeURIComponent(fileName || '');\n downloadElement.target = '_blank';\n document.body.appendChild(downloadElement);\n downloadElement.click();\n document.body.removeChild(downloadElement);\n }\n return [3, 4];\n case 4: return [2];\n }\n });\n });\n}\nfunction downloadFile(file, fileName, signal) {\n return __awaiter(this, void 0, void 0, function () {\n var blob, downloadElement, href;\n return __generator(this, function (_a) {\n switch (_a.label) {\n case 0:\n if (!(typeof file === 'string')) return [3, 2];\n return [4, downloadFileByUrl(file, fileName, signal)];\n case 1:\n _a.sent();\n return [3, 3];\n case 2:\n blob = (0, __1.Typeof)(file) === 'blob' ? file : new Blob([file]);\n downloadElement = document.createElement('a');\n href = window.URL.createObjectURL(blob);\n downloadElement.href = href;\n downloadElement.download = decodeURIComponent(fileName || '');\n downloadElement.target = '_blank';\n document.body.appendChild(downloadElement);\n downloadElement.click();\n document.body.removeChild(downloadElement);\n window.URL.revokeObjectURL(href);\n _a.label = 3;\n case 3: return [2];\n }\n });\n });\n}\nexports[\"default\"] = downloadFile;\n\n\n//# sourceURL=webpack://hsu-utils/./tools/DownloadFile/index.ts?");
|
|
143
143
|
|
|
144
144
|
/***/ }),
|
|
145
145
|
|
|
@@ -172,7 +172,7 @@ eval("\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nfunc
|
|
|
172
172
|
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
|
|
173
173
|
|
|
174
174
|
"use strict";
|
|
175
|
-
eval("\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nvar dayjs_1 = __importDefault(__webpack_require__(/*! dayjs */ \"./node_modules/dayjs/dayjs.min.js\"));\nvar quarterOfYear_1 = __importDefault(__webpack_require__(/*! dayjs/plugin/quarterOfYear */ \"./node_modules/dayjs/plugin/quarterOfYear.js\"));\nvar weekOfYear_1 = __importDefault(__webpack_require__(/*! dayjs/plugin/weekOfYear */ \"./node_modules/dayjs/plugin/weekOfYear.js\"));\ndayjs_1.default.extend(quarterOfYear_1.default);\ndayjs_1.default.extend(weekOfYear_1.default);\nfunction getFormat(type, unit, hasTime) {\n if (unit === void 0) { unit = 'day'; }\n if (hasTime === void 0) { hasTime = false; }\n var baseFormat;\n switch (type) {\n case 'today':\n baseFormat = 'YYYY-MM-DD';\n break;\n case 'thisWeek':\n baseFormat = 'YYYY-MM-DD';\n break;\n case 'thisMonth':\n baseFormat = 'YYYY-MM';\n break;\n case 'thisQuarter':\n baseFormat = 'YYYY-[Q]Q';\n break;\n case 'thisYear':\n baseFormat = 'YYYY';\n break;\n case 'past':\n case 'future':\n default:\n switch (unit) {\n case 'year':\n baseFormat = 'YYYY';\n break;\n case 'month':\n baseFormat = 'YYYY-MM';\n break;\n case 'week':\n case 'day':\n default:\n baseFormat = 'YYYY-MM-DD';\n break;\n }\n break;\n }\n if (hasTime && baseFormat.
|
|
175
|
+
eval("\nvar __importDefault = (this && this.__importDefault) || function (mod) {\n return (mod && mod.__esModule) ? mod : { \"default\": mod };\n};\nObject.defineProperty(exports, \"__esModule\", ({ value: true }));\nvar dayjs_1 = __importDefault(__webpack_require__(/*! dayjs */ \"./node_modules/dayjs/dayjs.min.js\"));\nvar quarterOfYear_1 = __importDefault(__webpack_require__(/*! dayjs/plugin/quarterOfYear */ \"./node_modules/dayjs/plugin/quarterOfYear.js\"));\nvar weekOfYear_1 = __importDefault(__webpack_require__(/*! dayjs/plugin/weekOfYear */ \"./node_modules/dayjs/plugin/weekOfYear.js\"));\ndayjs_1.default.extend(quarterOfYear_1.default);\ndayjs_1.default.extend(weekOfYear_1.default);\nfunction getFormat(type, unit, hasTime) {\n if (unit === void 0) { unit = 'day'; }\n if (hasTime === void 0) { hasTime = false; }\n var baseFormat;\n switch (type) {\n case 'today':\n baseFormat = 'YYYY-MM-DD';\n break;\n case 'thisWeek':\n baseFormat = 'YYYY-MM-DD';\n break;\n case 'thisMonth':\n baseFormat = 'YYYY-MM';\n break;\n case 'thisQuarter':\n baseFormat = 'YYYY-[Q]Q';\n break;\n case 'thisYear':\n baseFormat = 'YYYY';\n break;\n case 'past':\n case 'future':\n default:\n switch (unit) {\n case 'year':\n baseFormat = 'YYYY';\n break;\n case 'month':\n baseFormat = 'YYYY-MM';\n break;\n case 'second':\n case 'minute':\n case 'hour':\n return 'YYYY-MM-DD HH:mm:ss';\n case 'week':\n case 'day':\n default:\n baseFormat = 'YYYY-MM-DD';\n break;\n }\n break;\n }\n if (hasTime && baseFormat.endsWith('DD')) {\n return \"\".concat(baseFormat, \" HH:mm:ss\");\n }\n return baseFormat;\n}\nfunction getDateRange(options) {\n 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 ? false : _c;\n var base = baseDate ? (0, dayjs_1.default)(baseDate) : (0, dayjs_1.default)();\n var minDate;\n var maxDate;\n switch (type) {\n case 'past':\n maxDate = base.endOf(unit);\n minDate = base.subtract(amount, unit).startOf(unit);\n if (minDateLimit) {\n var minLimit = (0, dayjs_1.default)(minDateLimit).startOf(unit);\n if (minLimit.isAfter(minDate)) {\n minDate = minLimit;\n }\n }\n if (maxDateLimit) {\n var maxLimit = (0, dayjs_1.default)(maxDateLimit).endOf(unit);\n if (maxLimit.isBefore(maxDate)) {\n maxDate = maxLimit;\n }\n }\n break;\n case 'future':\n minDate = base.startOf(unit);\n maxDate = base.add(amount, unit).endOf(unit);\n if (minDateLimit) {\n var minLimit = (0, dayjs_1.default)(minDateLimit).startOf(unit);\n if (minLimit.isAfter(minDate)) {\n minDate = minLimit;\n }\n }\n if (maxDateLimit) {\n var maxLimit = (0, dayjs_1.default)(maxDateLimit).endOf(unit);\n if (maxLimit.isBefore(maxDate)) {\n maxDate = maxLimit;\n }\n }\n break;\n case 'today':\n minDate = base.startOf('day');\n maxDate = base.endOf('day');\n break;\n case 'thisWeek':\n minDate = base.startOf('week');\n maxDate = base.endOf('week');\n break;\n case 'thisMonth':\n minDate = base.startOf('month');\n maxDate = base.endOf('month');\n break;\n case 'thisQuarter':\n minDate = base.startOf('quarter');\n maxDate = base.endOf('quarter');\n break;\n case 'thisYear':\n minDate = base.startOf('year');\n maxDate = base.endOf('year');\n break;\n default:\n throw new Error(\"\\u4E0D\\u652F\\u6301\\u7684\\u7C7B\\u578B: \".concat(type));\n }\n var format = getFormat(type, unit, hasTime);\n var min = minDate.format(format);\n var max = maxDate.format(format);\n return [min, max];\n}\nexports[\"default\"] = getDateRange;\n\n\n//# sourceURL=webpack://hsu-utils/./tools/GetDateRange/index.ts?");
|
|
176
176
|
|
|
177
177
|
/***/ }),
|
|
178
178
|
|