@xyx-utils/core 0.0.1

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.
Files changed (41) hide show
  1. package/dist/common/index.cjs +7 -0
  2. package/dist/common/index.d.cts +1 -0
  3. package/dist/common/index.d.mts +1 -0
  4. package/dist/common/index.d.ts +1 -0
  5. package/dist/common/index.mjs +1 -0
  6. package/dist/common/padLeftZero/index.cjs +12 -0
  7. package/dist/common/padLeftZero/index.d.cts +8 -0
  8. package/dist/common/padLeftZero/index.d.mts +8 -0
  9. package/dist/common/padLeftZero/index.d.ts +8 -0
  10. package/dist/common/padLeftZero/index.mjs +10 -0
  11. package/dist/date/format/index.cjs +25 -0
  12. package/dist/date/format/index.d.cts +3 -0
  13. package/dist/date/format/index.d.mts +3 -0
  14. package/dist/date/format/index.d.ts +3 -0
  15. package/dist/date/format/index.mjs +23 -0
  16. package/dist/date/friendly/index.cjs +49 -0
  17. package/dist/date/friendly/index.d.cts +8 -0
  18. package/dist/date/friendly/index.d.mts +8 -0
  19. package/dist/date/friendly/index.d.ts +8 -0
  20. package/dist/date/friendly/index.mjs +47 -0
  21. package/dist/date/index.cjs +11 -0
  22. package/dist/date/index.d.cts +3 -0
  23. package/dist/date/index.d.mts +3 -0
  24. package/dist/date/index.d.ts +3 -0
  25. package/dist/date/index.mjs +3 -0
  26. package/dist/date/parse/index.cjs +6 -0
  27. package/dist/date/parse/index.d.cts +3 -0
  28. package/dist/date/parse/index.d.mts +3 -0
  29. package/dist/date/parse/index.d.ts +3 -0
  30. package/dist/date/parse/index.mjs +4 -0
  31. package/dist/debounce/index.cjs +16 -0
  32. package/dist/debounce/index.d.cts +3 -0
  33. package/dist/debounce/index.d.mts +3 -0
  34. package/dist/debounce/index.d.ts +3 -0
  35. package/dist/debounce/index.mjs +14 -0
  36. package/dist/index.cjs +15 -0
  37. package/dist/index.d.cts +5 -0
  38. package/dist/index.d.mts +5 -0
  39. package/dist/index.d.ts +5 -0
  40. package/dist/index.mjs +5 -0
  41. package/package.json +14 -0
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ const common_padLeftZero_index = require('./padLeftZero/index.cjs');
4
+
5
+
6
+
7
+ exports.padLeftZero = common_padLeftZero_index.padLeftZero;
@@ -0,0 +1 @@
1
+ export { padLeftZero } from './padLeftZero/index.cjs';
@@ -0,0 +1 @@
1
+ export { padLeftZero } from './padLeftZero/index.mjs';
@@ -0,0 +1 @@
1
+ export { padLeftZero } from './padLeftZero/index.js';
@@ -0,0 +1 @@
1
+ export { padLeftZero } from './padLeftZero/index.mjs';
@@ -0,0 +1,12 @@
1
+ 'use strict';
2
+
3
+ function padLeftZero(str, len) {
4
+ if (typeof str !== "string") return str;
5
+ if (str.padStart) {
6
+ return str.padStart(len, "0");
7
+ }
8
+ if (str.length >= len) return str;
9
+ return (Array(len).fill("0").join("") + str).substr(str.length);
10
+ }
11
+
12
+ exports.padLeftZero = padLeftZero;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 用来在字符串前面补0
3
+ * @param str 字符串
4
+ * @returns 补0后的字符串
5
+ */
6
+ declare function padLeftZero(str: string, len: number): string;
7
+
8
+ export { padLeftZero };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 用来在字符串前面补0
3
+ * @param str 字符串
4
+ * @returns 补0后的字符串
5
+ */
6
+ declare function padLeftZero(str: string, len: number): string;
7
+
8
+ export { padLeftZero };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 用来在字符串前面补0
3
+ * @param str 字符串
4
+ * @returns 补0后的字符串
5
+ */
6
+ declare function padLeftZero(str: string, len: number): string;
7
+
8
+ export { padLeftZero };
@@ -0,0 +1,10 @@
1
+ function padLeftZero(str, len) {
2
+ if (typeof str !== "string") return str;
3
+ if (str.padStart) {
4
+ return str.padStart(len, "0");
5
+ }
6
+ if (str.length >= len) return str;
7
+ return (Array(len).fill("0").join("") + str).substr(str.length);
8
+ }
9
+
10
+ export { padLeftZero };
@@ -0,0 +1,25 @@
1
+ 'use strict';
2
+
3
+ const common_padLeftZero_index = require('../../common/padLeftZero/index.cjs');
4
+
5
+ function formatDateByFmt(date, fmt) {
6
+ if (/(y+)/.test(fmt)) {
7
+ fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
8
+ }
9
+ const o = {
10
+ "M+": date.getMonth() + 1,
11
+ "d+": date.getDate(),
12
+ "h+": date.getHours(),
13
+ "m+": date.getMinutes(),
14
+ "s+": date.getSeconds()
15
+ };
16
+ for (const k in o) {
17
+ if (new RegExp(`(${k})`).test(fmt)) {
18
+ const str = o[k] + "";
19
+ fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : common_padLeftZero_index.padLeftZero(str, 2));
20
+ }
21
+ }
22
+ return fmt;
23
+ }
24
+
25
+ exports.formatDateByFmt = formatDateByFmt;
@@ -0,0 +1,3 @@
1
+ declare function formatDateByFmt(date: any, fmt: any): any;
2
+
3
+ export { formatDateByFmt };
@@ -0,0 +1,3 @@
1
+ declare function formatDateByFmt(date: any, fmt: any): any;
2
+
3
+ export { formatDateByFmt };
@@ -0,0 +1,3 @@
1
+ declare function formatDateByFmt(date: any, fmt: any): any;
2
+
3
+ export { formatDateByFmt };
@@ -0,0 +1,23 @@
1
+ import { padLeftZero } from '../../common/padLeftZero/index.mjs';
2
+
3
+ function formatDateByFmt(date, fmt) {
4
+ if (/(y+)/.test(fmt)) {
5
+ fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
6
+ }
7
+ const o = {
8
+ "M+": date.getMonth() + 1,
9
+ "d+": date.getDate(),
10
+ "h+": date.getHours(),
11
+ "m+": date.getMinutes(),
12
+ "s+": date.getSeconds()
13
+ };
14
+ for (const k in o) {
15
+ if (new RegExp(`(${k})`).test(fmt)) {
16
+ const str = o[k] + "";
17
+ fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str, 2));
18
+ }
19
+ }
20
+ return fmt;
21
+ }
22
+
23
+ export { formatDateByFmt };
@@ -0,0 +1,49 @@
1
+ 'use strict';
2
+
3
+ function formatTimeFriendly(str, now) {
4
+ var currentTime = new Date(now);
5
+ var arr = str.split(/\s+/gi);
6
+ var arr1, arr2, oldTime, delta;
7
+ var getIntValue = function(ss, defaultValue) {
8
+ try {
9
+ return parseInt(ss, 10);
10
+ } catch (e) {
11
+ return defaultValue;
12
+ }
13
+ };
14
+ var getWidthString = function(num) {
15
+ return num < 10 ? "0" + num : num;
16
+ };
17
+ if (arr.length >= 2) {
18
+ arr1 = arr[0].split(/[\/\-]/gi);
19
+ arr2 = arr[1].split(":");
20
+ oldTime = /* @__PURE__ */ new Date();
21
+ oldTime.setYear(getIntValue(arr1[0], currentTime.getFullYear()));
22
+ oldTime.setMonth(getIntValue(arr1[1], currentTime.getMonth() + 1) - 1);
23
+ oldTime.setDate(getIntValue(arr1[2], currentTime.getDate()));
24
+ oldTime.setHours(getIntValue(arr2[0], currentTime.getHours()));
25
+ oldTime.setMinutes(getIntValue(arr2[1], currentTime.getMinutes()));
26
+ oldTime.setSeconds(getIntValue(arr2[2], currentTime.getSeconds()));
27
+ delta = currentTime.getTime() - oldTime.getTime();
28
+ if (delta <= 6e3) {
29
+ return "1\u5206\u949F\u5185";
30
+ } else if (delta < 60 * 60 * 1e3) {
31
+ return Math.floor(delta / (60 * 1e3)) + "\u5206\u949F\u524D";
32
+ } else if (delta < 24 * 60 * 60 * 1e3) {
33
+ return Math.floor(delta / (60 * 60 * 1e3)) + "\u5C0F\u65F6\u524D";
34
+ } else if (delta < 3 * 24 * 60 * 60 * 1e3) {
35
+ return Math.floor(delta / (24 * 60 * 60 * 1e3)) + "\u5929\u524D";
36
+ } else if (currentTime.getFullYear() != oldTime.getFullYear()) {
37
+ return [
38
+ getWidthString(oldTime.getFullYear()),
39
+ getWidthString(oldTime.getMonth() + 1),
40
+ getWidthString(oldTime.getDate())
41
+ ].join("-");
42
+ } else {
43
+ return [getWidthString(oldTime.getMonth() + 1), getWidthString(oldTime.getDate())].join("-");
44
+ }
45
+ }
46
+ return "";
47
+ }
48
+
49
+ exports.formatTimeFriendly = formatTimeFriendly;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 获取指定时间的友好时间字符串。
3
+ * @param str 指定的时间字符串,如yyyy-MM-dd HH:mm:ss
4
+ * @param now 当前时间,允许时间戳,GMT时间,如果该参数为undefined,则使用浏览器时间。
5
+ */
6
+ declare function formatTimeFriendly(str: any, now: any): string;
7
+
8
+ export { formatTimeFriendly };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 获取指定时间的友好时间字符串。
3
+ * @param str 指定的时间字符串,如yyyy-MM-dd HH:mm:ss
4
+ * @param now 当前时间,允许时间戳,GMT时间,如果该参数为undefined,则使用浏览器时间。
5
+ */
6
+ declare function formatTimeFriendly(str: any, now: any): string;
7
+
8
+ export { formatTimeFriendly };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * 获取指定时间的友好时间字符串。
3
+ * @param str 指定的时间字符串,如yyyy-MM-dd HH:mm:ss
4
+ * @param now 当前时间,允许时间戳,GMT时间,如果该参数为undefined,则使用浏览器时间。
5
+ */
6
+ declare function formatTimeFriendly(str: any, now: any): string;
7
+
8
+ export { formatTimeFriendly };
@@ -0,0 +1,47 @@
1
+ function formatTimeFriendly(str, now) {
2
+ var currentTime = new Date(now);
3
+ var arr = str.split(/\s+/gi);
4
+ var arr1, arr2, oldTime, delta;
5
+ var getIntValue = function(ss, defaultValue) {
6
+ try {
7
+ return parseInt(ss, 10);
8
+ } catch (e) {
9
+ return defaultValue;
10
+ }
11
+ };
12
+ var getWidthString = function(num) {
13
+ return num < 10 ? "0" + num : num;
14
+ };
15
+ if (arr.length >= 2) {
16
+ arr1 = arr[0].split(/[\/\-]/gi);
17
+ arr2 = arr[1].split(":");
18
+ oldTime = /* @__PURE__ */ new Date();
19
+ oldTime.setYear(getIntValue(arr1[0], currentTime.getFullYear()));
20
+ oldTime.setMonth(getIntValue(arr1[1], currentTime.getMonth() + 1) - 1);
21
+ oldTime.setDate(getIntValue(arr1[2], currentTime.getDate()));
22
+ oldTime.setHours(getIntValue(arr2[0], currentTime.getHours()));
23
+ oldTime.setMinutes(getIntValue(arr2[1], currentTime.getMinutes()));
24
+ oldTime.setSeconds(getIntValue(arr2[2], currentTime.getSeconds()));
25
+ delta = currentTime.getTime() - oldTime.getTime();
26
+ if (delta <= 6e3) {
27
+ return "1\u5206\u949F\u5185";
28
+ } else if (delta < 60 * 60 * 1e3) {
29
+ return Math.floor(delta / (60 * 1e3)) + "\u5206\u949F\u524D";
30
+ } else if (delta < 24 * 60 * 60 * 1e3) {
31
+ return Math.floor(delta / (60 * 60 * 1e3)) + "\u5C0F\u65F6\u524D";
32
+ } else if (delta < 3 * 24 * 60 * 60 * 1e3) {
33
+ return Math.floor(delta / (24 * 60 * 60 * 1e3)) + "\u5929\u524D";
34
+ } else if (currentTime.getFullYear() != oldTime.getFullYear()) {
35
+ return [
36
+ getWidthString(oldTime.getFullYear()),
37
+ getWidthString(oldTime.getMonth() + 1),
38
+ getWidthString(oldTime.getDate())
39
+ ].join("-");
40
+ } else {
41
+ return [getWidthString(oldTime.getMonth() + 1), getWidthString(oldTime.getDate())].join("-");
42
+ }
43
+ }
44
+ return "";
45
+ }
46
+
47
+ export { formatTimeFriendly };
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ const date_format_index = require('./format/index.cjs');
4
+ const date_parse_index = require('./parse/index.cjs');
5
+ const date_friendly_index = require('./friendly/index.cjs');
6
+
7
+
8
+
9
+ exports.formatDateByFmt = date_format_index.formatDateByFmt;
10
+ exports.parse = date_parse_index.parse;
11
+ exports.formatTimeFriendly = date_friendly_index.formatTimeFriendly;
@@ -0,0 +1,3 @@
1
+ export { formatDateByFmt } from './format/index.cjs';
2
+ export { parse } from './parse/index.cjs';
3
+ export { formatTimeFriendly } from './friendly/index.cjs';
@@ -0,0 +1,3 @@
1
+ export { formatDateByFmt } from './format/index.mjs';
2
+ export { parse } from './parse/index.mjs';
3
+ export { formatTimeFriendly } from './friendly/index.mjs';
@@ -0,0 +1,3 @@
1
+ export { formatDateByFmt } from './format/index.js';
2
+ export { parse } from './parse/index.js';
3
+ export { formatTimeFriendly } from './friendly/index.js';
@@ -0,0 +1,3 @@
1
+ export { formatDateByFmt } from './format/index.mjs';
2
+ export { parse } from './parse/index.mjs';
3
+ export { formatTimeFriendly } from './friendly/index.mjs';
@@ -0,0 +1,6 @@
1
+ 'use strict';
2
+
3
+ function parse(date, fmt) {
4
+ }
5
+
6
+ exports.parse = parse;
@@ -0,0 +1,3 @@
1
+ declare function parse(date: any, fmt: any): void;
2
+
3
+ export { parse };
@@ -0,0 +1,3 @@
1
+ declare function parse(date: any, fmt: any): void;
2
+
3
+ export { parse };
@@ -0,0 +1,3 @@
1
+ declare function parse(date: any, fmt: any): void;
2
+
3
+ export { parse };
@@ -0,0 +1,4 @@
1
+ function parse(date, fmt) {
2
+ }
3
+
4
+ export { parse };
@@ -0,0 +1,16 @@
1
+ 'use strict';
2
+
3
+ function debounce(fn, duration = 1500) {
4
+ let timer;
5
+ return function f(...argu) {
6
+ if (timer) {
7
+ clearTimeout(timer);
8
+ }
9
+ timer = setTimeout(() => {
10
+ timer = void 0;
11
+ fn.apply(this, argu);
12
+ }, duration);
13
+ };
14
+ }
15
+
16
+ exports.debounce = debounce;
@@ -0,0 +1,3 @@
1
+ declare function debounce<T extends any[], R = void>(fn: (...argu: T) => R, duration?: number): (this: void, ...argu: T) => void;
2
+
3
+ export { debounce };
@@ -0,0 +1,3 @@
1
+ declare function debounce<T extends any[], R = void>(fn: (...argu: T) => R, duration?: number): (this: void, ...argu: T) => void;
2
+
3
+ export { debounce };
@@ -0,0 +1,3 @@
1
+ declare function debounce<T extends any[], R = void>(fn: (...argu: T) => R, duration?: number): (this: void, ...argu: T) => void;
2
+
3
+ export { debounce };
@@ -0,0 +1,14 @@
1
+ function debounce(fn, duration = 1500) {
2
+ let timer;
3
+ return function f(...argu) {
4
+ if (timer) {
5
+ clearTimeout(timer);
6
+ }
7
+ timer = setTimeout(() => {
8
+ timer = void 0;
9
+ fn.apply(this, argu);
10
+ }, duration);
11
+ };
12
+ }
13
+
14
+ export { debounce };
package/dist/index.cjs ADDED
@@ -0,0 +1,15 @@
1
+ 'use strict';
2
+
3
+ const date_format_index = require('./date/format/index.cjs');
4
+ const date_parse_index = require('./date/parse/index.cjs');
5
+ const date_friendly_index = require('./date/friendly/index.cjs');
6
+ const common_padLeftZero_index = require('./common/padLeftZero/index.cjs');
7
+ const debounce_index = require('./debounce/index.cjs');
8
+
9
+
10
+
11
+ exports.formatDateByFmt = date_format_index.formatDateByFmt;
12
+ exports.parse = date_parse_index.parse;
13
+ exports.formatTimeFriendly = date_friendly_index.formatTimeFriendly;
14
+ exports.padLeftZero = common_padLeftZero_index.padLeftZero;
15
+ exports.debounce = debounce_index.debounce;
@@ -0,0 +1,5 @@
1
+ export { formatDateByFmt } from './date/format/index.cjs';
2
+ export { parse } from './date/parse/index.cjs';
3
+ export { formatTimeFriendly } from './date/friendly/index.cjs';
4
+ export { padLeftZero } from './common/padLeftZero/index.cjs';
5
+ export { debounce } from './debounce/index.cjs';
@@ -0,0 +1,5 @@
1
+ export { formatDateByFmt } from './date/format/index.mjs';
2
+ export { parse } from './date/parse/index.mjs';
3
+ export { formatTimeFriendly } from './date/friendly/index.mjs';
4
+ export { padLeftZero } from './common/padLeftZero/index.mjs';
5
+ export { debounce } from './debounce/index.mjs';
@@ -0,0 +1,5 @@
1
+ export { formatDateByFmt } from './date/format/index.js';
2
+ export { parse } from './date/parse/index.js';
3
+ export { formatTimeFriendly } from './date/friendly/index.js';
4
+ export { padLeftZero } from './common/padLeftZero/index.js';
5
+ export { debounce } from './debounce/index.js';
package/dist/index.mjs ADDED
@@ -0,0 +1,5 @@
1
+ export { formatDateByFmt } from './date/format/index.mjs';
2
+ export { parse } from './date/parse/index.mjs';
3
+ export { formatTimeFriendly } from './date/friendly/index.mjs';
4
+ export { padLeftZero } from './common/padLeftZero/index.mjs';
5
+ export { debounce } from './debounce/index.mjs';
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@xyx-utils/core",
3
+ "type": "module",
4
+ "version": "0.0.1",
5
+ "description": "",
6
+ "main": "dist/index.mjs",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "keywords": [],
11
+ "author": "",
12
+ "license": "ISC",
13
+ "scripts": {}
14
+ }