@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.
- package/dist/common/index.cjs +7 -0
- package/dist/common/index.d.cts +1 -0
- package/dist/common/index.d.mts +1 -0
- package/dist/common/index.d.ts +1 -0
- package/dist/common/index.mjs +1 -0
- package/dist/common/padLeftZero/index.cjs +12 -0
- package/dist/common/padLeftZero/index.d.cts +8 -0
- package/dist/common/padLeftZero/index.d.mts +8 -0
- package/dist/common/padLeftZero/index.d.ts +8 -0
- package/dist/common/padLeftZero/index.mjs +10 -0
- package/dist/date/format/index.cjs +25 -0
- package/dist/date/format/index.d.cts +3 -0
- package/dist/date/format/index.d.mts +3 -0
- package/dist/date/format/index.d.ts +3 -0
- package/dist/date/format/index.mjs +23 -0
- package/dist/date/friendly/index.cjs +49 -0
- package/dist/date/friendly/index.d.cts +8 -0
- package/dist/date/friendly/index.d.mts +8 -0
- package/dist/date/friendly/index.d.ts +8 -0
- package/dist/date/friendly/index.mjs +47 -0
- package/dist/date/index.cjs +11 -0
- package/dist/date/index.d.cts +3 -0
- package/dist/date/index.d.mts +3 -0
- package/dist/date/index.d.ts +3 -0
- package/dist/date/index.mjs +3 -0
- package/dist/date/parse/index.cjs +6 -0
- package/dist/date/parse/index.d.cts +3 -0
- package/dist/date/parse/index.d.mts +3 -0
- package/dist/date/parse/index.d.ts +3 -0
- package/dist/date/parse/index.mjs +4 -0
- package/dist/debounce/index.cjs +16 -0
- package/dist/debounce/index.d.cts +3 -0
- package/dist/debounce/index.d.mts +3 -0
- package/dist/debounce/index.d.ts +3 -0
- package/dist/debounce/index.mjs +14 -0
- package/dist/index.cjs +15 -0
- package/dist/index.d.cts +5 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.mjs +5 -0
- package/package.json +14 -0
@@ -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,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,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,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,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;
|
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;
|
package/dist/index.d.cts
ADDED
@@ -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';
|
package/dist/index.d.mts
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/dist/index.d.ts
ADDED
@@ -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