@oeos-components/utils 0.0.18 → 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 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
- if (typeof time === "object") {
232
+ const timeStr = String(time);
233
+ if (typeof time === "object" && time instanceof Date) {
233
234
  date = time;
234
235
  } else {
235
- const timeStr = "" + time;
236
- if (timeStr.includes(".") && !isNaN(parseFloat(timeStr))) {
237
- time = parseFloat(time) * 1e3;
238
- } else if (timeStr.length === 10) {
239
- time = parseInt(time) * 1e3;
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
- date = new Date(time);
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
- const time_str = cFormat.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
253
- let value = formatObj[key];
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
- value = "0" + value;
267
+ return "0" + value;
259
268
  }
260
- return value || 0;
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;
@@ -819,6 +827,9 @@ function getVariable(propertyName) {
819
827
  let res = getComputedStyle(document.documentElement).getPropertyValue(propertyName).trim();
820
828
  return res;
821
829
  }
830
+ function test() {
831
+ return "\u54C8\u54C8\u54C8" + /* @__PURE__ */ new Date();
832
+ }
822
833
 
823
834
  exports.$toast = $toast;
824
835
  exports.asyncWrapper = asyncWrapper;
@@ -848,6 +859,7 @@ exports.processWidth = processWidth;
848
859
  exports.random = random;
849
860
  exports.setStorage = setStorage;
850
861
  exports.sleep = sleep;
862
+ exports.test = test;
851
863
  exports.throttle = throttle;
852
864
  exports.toFixed = toFixed;
853
865
  exports.toLine = toLine;
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
- * 时间戳10位和13位都可以转换成格式化的日期
124
- * java8格式的日期和有效的日期都可以转换成定义的日期格式
125
- * @param {Date, string} 都有默认参数
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: any, cFormat?: string): any;
129
+ declare function formatTime(time?: TimeType, cFormat?: FormatType): string;
138
130
  /**
139
131
  *
140
132
  * @param timestamp 持续的时间戳
@@ -399,5 +391,6 @@ declare function formatNewLines(str: any): any;
399
391
  * getVariable('--blue');
400
392
  * */
401
393
  declare function getVariable(propertyName: any): string;
394
+ declare function test(): string;
402
395
 
403
- export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, throttle, toFixed, toLine, tryCatch, uuid, validForm, validate, validateTrigger };
396
+ export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, test, throttle, toFixed, toLine, tryCatch, uuid, validForm, validate, validateTrigger };
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
- * 时间戳10位和13位都可以转换成格式化的日期
124
- * java8格式的日期和有效的日期都可以转换成定义的日期格式
125
- * @param {Date, string} 都有默认参数
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: any, cFormat?: string): any;
129
+ declare function formatTime(time?: TimeType, cFormat?: FormatType): string;
138
130
  /**
139
131
  *
140
132
  * @param timestamp 持续的时间戳
@@ -399,5 +391,6 @@ declare function formatNewLines(str: any): any;
399
391
  * getVariable('--blue');
400
392
  * */
401
393
  declare function getVariable(propertyName: any): string;
394
+ declare function test(): string;
402
395
 
403
- export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, throttle, toFixed, toLine, tryCatch, uuid, validForm, validate, validateTrigger };
396
+ export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, test, throttle, toFixed, toLine, tryCatch, uuid, validForm, validate, validateTrigger };
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
- * 时间戳10位和13位都可以转换成格式化的日期
124
- * java8格式的日期和有效的日期都可以转换成定义的日期格式
125
- * @param {Date, string} 都有默认参数
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: any, cFormat?: string): any;
129
+ declare function formatTime(time?: TimeType, cFormat?: FormatType): string;
138
130
  /**
139
131
  *
140
132
  * @param timestamp 持续的时间戳
@@ -399,5 +391,6 @@ declare function formatNewLines(str: any): any;
399
391
  * getVariable('--blue');
400
392
  * */
401
393
  declare function getVariable(propertyName: any): string;
394
+ declare function test(): string;
402
395
 
403
- export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, throttle, toFixed, toLine, tryCatch, uuid, validForm, validate, validateTrigger };
396
+ export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, test, throttle, toFixed, toLine, tryCatch, uuid, validForm, validate, validateTrigger };
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
- if (typeof time === "object") {
229
+ const timeStr = String(time);
230
+ if (typeof time === "object" && time instanceof Date) {
230
231
  date = time;
231
232
  } else {
232
- const timeStr = "" + time;
233
- if (timeStr.includes(".") && !isNaN(parseFloat(timeStr))) {
234
- time = parseFloat(time) * 1e3;
235
- } else if (timeStr.length === 10) {
236
- time = parseInt(time) * 1e3;
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
- date = new Date(time);
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
- const time_str = cFormat.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
250
- let value = formatObj[key];
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
- value = "0" + value;
264
+ return "0" + value;
256
265
  }
257
- return value || 0;
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;
@@ -816,5 +824,8 @@ function getVariable(propertyName) {
816
824
  let res = getComputedStyle(document.documentElement).getPropertyValue(propertyName).trim();
817
825
  return res;
818
826
  }
827
+ function test() {
828
+ return "\u54C8\u54C8\u54C8" + /* @__PURE__ */ new Date();
829
+ }
819
830
 
820
- export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, throttle, toFixed, toLine, tryCatch, uuid, validForm, validate, validateTrigger };
831
+ export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatNewLines, formatThousands, formatTime, getStorage, getType, getVariable, isEmpty, isNumber, isString, isStringNumber, log, merge, notEmpty, processWidth, random, setStorage, sleep, test, throttle, toFixed, toLine, tryCatch, uuid, validForm, validate, validateTrigger };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oeos-components/utils",
3
- "version": "0.0.18",
3
+ "version": "0.0.21",
4
4
  "description": "utils of oeos-components",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",