@oeos-components/utils 0.0.19 → 0.0.21
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/dist/index.cjs +22 -14
- package/dist/index.d.cts +7 -15
- package/dist/index.d.mts +7 -15
- package/dist/index.d.ts +7 -15
- package/dist/index.mjs +22 -14
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -224,21 +224,30 @@ function clone(data, times = 1) {
|
|
|
224
224
|
}
|
|
225
225
|
return result;
|
|
226
226
|
}
|
|
227
|
-
function formatTime(time, cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
|
|
227
|
+
function formatTime(time = /* @__PURE__ */ new Date(), cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
|
|
228
228
|
if (!time) {
|
|
229
|
-
return time;
|
|
229
|
+
return String(time);
|
|
230
230
|
}
|
|
231
231
|
let date;
|
|
232
|
-
|
|
232
|
+
const timeStr = String(time);
|
|
233
|
+
if (typeof time === "object" && time instanceof Date) {
|
|
233
234
|
date = time;
|
|
234
235
|
} else {
|
|
235
|
-
const
|
|
236
|
-
if (
|
|
237
|
-
|
|
238
|
-
} else
|
|
239
|
-
|
|
236
|
+
const isoRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?$/;
|
|
237
|
+
if (isoRegex.test(timeStr)) {
|
|
238
|
+
date = new Date(time);
|
|
239
|
+
} else {
|
|
240
|
+
if (timeStr.includes(".") && !isNaN(parseFloat(timeStr))) {
|
|
241
|
+
date = new Date(parseFloat(timeStr) * 1e3);
|
|
242
|
+
} else if (/^\d{10}$/.test(timeStr)) {
|
|
243
|
+
date = new Date(parseInt(timeStr) * 1e3);
|
|
244
|
+
} else {
|
|
245
|
+
date = new Date(time);
|
|
246
|
+
}
|
|
240
247
|
}
|
|
241
|
-
|
|
248
|
+
}
|
|
249
|
+
if (isNaN(date.getTime())) {
|
|
250
|
+
throw new Error("Invalid Date");
|
|
242
251
|
}
|
|
243
252
|
const formatObj = {
|
|
244
253
|
y: date.getFullYear(),
|
|
@@ -249,17 +258,16 @@ function formatTime(time, cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
|
|
|
249
258
|
s: date.getSeconds(),
|
|
250
259
|
a: date.getDay()
|
|
251
260
|
};
|
|
252
|
-
|
|
253
|
-
|
|
261
|
+
return cFormat.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
|
262
|
+
const value = formatObj[key];
|
|
254
263
|
if (key === "a") {
|
|
255
264
|
return ["\u65E5", "\u4E00", "\u4E8C", "\u4E09", "\u56DB", "\u4E94", "\u516D"][value];
|
|
256
265
|
}
|
|
257
266
|
if (result.length > 0 && value < 10) {
|
|
258
|
-
|
|
267
|
+
return "0" + value;
|
|
259
268
|
}
|
|
260
|
-
return value
|
|
269
|
+
return value;
|
|
261
270
|
});
|
|
262
|
-
return time_str;
|
|
263
271
|
}
|
|
264
272
|
function formatDurationTime(timestamp, cFormat = "{d}\u5929{h}\u65F6{i}\u5206{s}\u79D2") {
|
|
265
273
|
const secondsPerMinute = 60;
|
package/dist/index.d.cts
CHANGED
|
@@ -118,23 +118,15 @@ declare function merge(obj1: object, obj2: object): object;
|
|
|
118
118
|
* clone([1,2, {name: 'andy'}], 2) => [1, 2, {name: 'andy'}, 1, 2, {name: 'andy'}]
|
|
119
119
|
*/
|
|
120
120
|
declare function clone(data: any, times?: number): any;
|
|
121
|
+
type TimeType = Date | string | number | null | undefined;
|
|
122
|
+
type FormatType = string;
|
|
121
123
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
* @
|
|
126
|
-
* @example
|
|
127
|
-
* formatTime() // 2020-07-17 09:53:07
|
|
128
|
-
* formatTime('2018-02-13T06:17') // 2018-02-13 06:17:00
|
|
129
|
-
* formatTime('2020/03/02 06:02') // 2020-03-02 06:02:00
|
|
130
|
-
* formatTime(1541927611000); //2018-11-11 17:13:21
|
|
131
|
-
* formatTime(1541927611000, "{y}年{m}月{d}日 {h}时{m}分{s}秒"); // 2018年11月11日 17时11分31秒
|
|
132
|
-
* formatTime(1541927611, "{y}/{m}/{d} {h}:{m}:{s}"); // 2018/11/11 17:11:31
|
|
133
|
-
* formatTime(new Date()); // 2018-11-11 17:13:21
|
|
134
|
-
* formatTime(new Date().getTime()); // 2018-11-11 17:13:21
|
|
135
|
-
* formatTime('1764128798.456'); // 2025/11/26 11:11:38
|
|
124
|
+
* 时间格式化函数
|
|
125
|
+
* @param {TimeType} time - 可选时间参数,可以是 Date 对象、时间戳字符串或数字
|
|
126
|
+
* @param {FormatType} [cFormat='{y}-{m}-{d} {h}:{i}:{s}'] - 格式化字符串
|
|
127
|
+
* @returns {string} 格式化后的时间字符串
|
|
136
128
|
*/
|
|
137
|
-
declare function formatTime(time
|
|
129
|
+
declare function formatTime(time?: TimeType, cFormat?: FormatType): string;
|
|
138
130
|
/**
|
|
139
131
|
*
|
|
140
132
|
* @param timestamp 持续的时间戳
|
package/dist/index.d.mts
CHANGED
|
@@ -118,23 +118,15 @@ declare function merge(obj1: object, obj2: object): object;
|
|
|
118
118
|
* clone([1,2, {name: 'andy'}], 2) => [1, 2, {name: 'andy'}, 1, 2, {name: 'andy'}]
|
|
119
119
|
*/
|
|
120
120
|
declare function clone(data: any, times?: number): any;
|
|
121
|
+
type TimeType = Date | string | number | null | undefined;
|
|
122
|
+
type FormatType = string;
|
|
121
123
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
* @
|
|
126
|
-
* @example
|
|
127
|
-
* formatTime() // 2020-07-17 09:53:07
|
|
128
|
-
* formatTime('2018-02-13T06:17') // 2018-02-13 06:17:00
|
|
129
|
-
* formatTime('2020/03/02 06:02') // 2020-03-02 06:02:00
|
|
130
|
-
* formatTime(1541927611000); //2018-11-11 17:13:21
|
|
131
|
-
* formatTime(1541927611000, "{y}年{m}月{d}日 {h}时{m}分{s}秒"); // 2018年11月11日 17时11分31秒
|
|
132
|
-
* formatTime(1541927611, "{y}/{m}/{d} {h}:{m}:{s}"); // 2018/11/11 17:11:31
|
|
133
|
-
* formatTime(new Date()); // 2018-11-11 17:13:21
|
|
134
|
-
* formatTime(new Date().getTime()); // 2018-11-11 17:13:21
|
|
135
|
-
* formatTime('1764128798.456'); // 2025/11/26 11:11:38
|
|
124
|
+
* 时间格式化函数
|
|
125
|
+
* @param {TimeType} time - 可选时间参数,可以是 Date 对象、时间戳字符串或数字
|
|
126
|
+
* @param {FormatType} [cFormat='{y}-{m}-{d} {h}:{i}:{s}'] - 格式化字符串
|
|
127
|
+
* @returns {string} 格式化后的时间字符串
|
|
136
128
|
*/
|
|
137
|
-
declare function formatTime(time
|
|
129
|
+
declare function formatTime(time?: TimeType, cFormat?: FormatType): string;
|
|
138
130
|
/**
|
|
139
131
|
*
|
|
140
132
|
* @param timestamp 持续的时间戳
|
package/dist/index.d.ts
CHANGED
|
@@ -118,23 +118,15 @@ declare function merge(obj1: object, obj2: object): object;
|
|
|
118
118
|
* clone([1,2, {name: 'andy'}], 2) => [1, 2, {name: 'andy'}, 1, 2, {name: 'andy'}]
|
|
119
119
|
*/
|
|
120
120
|
declare function clone(data: any, times?: number): any;
|
|
121
|
+
type TimeType = Date | string | number | null | undefined;
|
|
122
|
+
type FormatType = string;
|
|
121
123
|
/**
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
* @
|
|
126
|
-
* @example
|
|
127
|
-
* formatTime() // 2020-07-17 09:53:07
|
|
128
|
-
* formatTime('2018-02-13T06:17') // 2018-02-13 06:17:00
|
|
129
|
-
* formatTime('2020/03/02 06:02') // 2020-03-02 06:02:00
|
|
130
|
-
* formatTime(1541927611000); //2018-11-11 17:13:21
|
|
131
|
-
* formatTime(1541927611000, "{y}年{m}月{d}日 {h}时{m}分{s}秒"); // 2018年11月11日 17时11分31秒
|
|
132
|
-
* formatTime(1541927611, "{y}/{m}/{d} {h}:{m}:{s}"); // 2018/11/11 17:11:31
|
|
133
|
-
* formatTime(new Date()); // 2018-11-11 17:13:21
|
|
134
|
-
* formatTime(new Date().getTime()); // 2018-11-11 17:13:21
|
|
135
|
-
* formatTime('1764128798.456'); // 2025/11/26 11:11:38
|
|
124
|
+
* 时间格式化函数
|
|
125
|
+
* @param {TimeType} time - 可选时间参数,可以是 Date 对象、时间戳字符串或数字
|
|
126
|
+
* @param {FormatType} [cFormat='{y}-{m}-{d} {h}:{i}:{s}'] - 格式化字符串
|
|
127
|
+
* @returns {string} 格式化后的时间字符串
|
|
136
128
|
*/
|
|
137
|
-
declare function formatTime(time
|
|
129
|
+
declare function formatTime(time?: TimeType, cFormat?: FormatType): string;
|
|
138
130
|
/**
|
|
139
131
|
*
|
|
140
132
|
* @param timestamp 持续的时间戳
|
package/dist/index.mjs
CHANGED
|
@@ -221,21 +221,30 @@ function clone(data, times = 1) {
|
|
|
221
221
|
}
|
|
222
222
|
return result;
|
|
223
223
|
}
|
|
224
|
-
function formatTime(time, cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
|
|
224
|
+
function formatTime(time = /* @__PURE__ */ new Date(), cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
|
|
225
225
|
if (!time) {
|
|
226
|
-
return time;
|
|
226
|
+
return String(time);
|
|
227
227
|
}
|
|
228
228
|
let date;
|
|
229
|
-
|
|
229
|
+
const timeStr = String(time);
|
|
230
|
+
if (typeof time === "object" && time instanceof Date) {
|
|
230
231
|
date = time;
|
|
231
232
|
} else {
|
|
232
|
-
const
|
|
233
|
-
if (
|
|
234
|
-
|
|
235
|
-
} else
|
|
236
|
-
|
|
233
|
+
const isoRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?$/;
|
|
234
|
+
if (isoRegex.test(timeStr)) {
|
|
235
|
+
date = new Date(time);
|
|
236
|
+
} else {
|
|
237
|
+
if (timeStr.includes(".") && !isNaN(parseFloat(timeStr))) {
|
|
238
|
+
date = new Date(parseFloat(timeStr) * 1e3);
|
|
239
|
+
} else if (/^\d{10}$/.test(timeStr)) {
|
|
240
|
+
date = new Date(parseInt(timeStr) * 1e3);
|
|
241
|
+
} else {
|
|
242
|
+
date = new Date(time);
|
|
243
|
+
}
|
|
237
244
|
}
|
|
238
|
-
|
|
245
|
+
}
|
|
246
|
+
if (isNaN(date.getTime())) {
|
|
247
|
+
throw new Error("Invalid Date");
|
|
239
248
|
}
|
|
240
249
|
const formatObj = {
|
|
241
250
|
y: date.getFullYear(),
|
|
@@ -246,17 +255,16 @@ function formatTime(time, cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
|
|
|
246
255
|
s: date.getSeconds(),
|
|
247
256
|
a: date.getDay()
|
|
248
257
|
};
|
|
249
|
-
|
|
250
|
-
|
|
258
|
+
return cFormat.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
|
259
|
+
const value = formatObj[key];
|
|
251
260
|
if (key === "a") {
|
|
252
261
|
return ["\u65E5", "\u4E00", "\u4E8C", "\u4E09", "\u56DB", "\u4E94", "\u516D"][value];
|
|
253
262
|
}
|
|
254
263
|
if (result.length > 0 && value < 10) {
|
|
255
|
-
|
|
264
|
+
return "0" + value;
|
|
256
265
|
}
|
|
257
|
-
return value
|
|
266
|
+
return value;
|
|
258
267
|
});
|
|
259
|
-
return time_str;
|
|
260
268
|
}
|
|
261
269
|
function formatDurationTime(timestamp, cFormat = "{d}\u5929{h}\u65F6{i}\u5206{s}\u79D2") {
|
|
262
270
|
const secondsPerMinute = 60;
|