@oeos-components/utils 0.0.25 → 0.0.26
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/base.cjs +33 -708
- package/dist/base.mjs +6 -685
- package/dist/format.cjs +11 -232
- package/dist/format.d.cts +3 -1
- package/dist/format.d.mts +3 -1
- package/dist/format.d.ts +3 -1
- package/dist/format.mjs +2 -226
- package/dist/index.cjs +35 -36
- package/dist/index.mjs +1 -2
- package/dist/shared/utils.CPo2h8Qi.cjs +944 -0
- package/dist/shared/utils.DRGlWWeY.mjs +908 -0
- package/package.json +1 -1
package/dist/format.cjs
CHANGED
|
@@ -1,240 +1,19 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
const
|
|
3
|
+
require('./is.cjs');
|
|
4
|
+
const format = require('./shared/utils.CPo2h8Qi.cjs');
|
|
5
5
|
require('@vue/reactivity');
|
|
6
6
|
require('consola');
|
|
7
7
|
require('es-toolkit');
|
|
8
8
|
require('element-plus');
|
|
9
9
|
|
|
10
|
-
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
|
|
11
|
-
function formatBytes(bytes, options) {
|
|
12
|
-
let { digit = 2, thousands = false, prefix = "", suffix = "", roundType = "floor" } = options ?? {};
|
|
13
|
-
if (is.isStringNumber(bytes) || is.isNumber(bytes)) {
|
|
14
|
-
bytes = Number(bytes);
|
|
15
|
-
} else {
|
|
16
|
-
return bytes;
|
|
17
|
-
}
|
|
18
|
-
if (bytes <= 1) {
|
|
19
|
-
return Math[roundType](bytes * Math.pow(10, digit)) / Math.pow(10, digit) + " B";
|
|
20
|
-
}
|
|
21
|
-
const k = 1024;
|
|
22
|
-
const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
23
|
-
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
24
|
-
const power = Math.pow(k, i);
|
|
25
|
-
let num = bytes / power;
|
|
26
|
-
num = Math[roundType](num * Math.pow(10, digit)) / Math.pow(10, digit);
|
|
27
|
-
let res = num.toFixed(digit) + " " + sizes[i];
|
|
28
|
-
if (thousands) {
|
|
29
|
-
res = formatThousands(res);
|
|
30
|
-
}
|
|
31
|
-
return `${prefix}${res}${suffix}`;
|
|
32
|
-
}
|
|
33
|
-
function formatBytesConvert(oBytes, options) {
|
|
34
|
-
let { thounsands = false, digit = 0 } = options ?? {};
|
|
35
|
-
let bytes = oBytes;
|
|
36
|
-
if (is.isStringNumber(oBytes) || is.isNumber(oBytes) || base.getType(oBytes) !== "string") {
|
|
37
|
-
return parseDigitThounsands(oBytes);
|
|
38
|
-
}
|
|
39
|
-
if (!oBytes) {
|
|
40
|
-
return parseDigitThounsands(oBytes);
|
|
41
|
-
}
|
|
42
|
-
const regex = /^\d{1,3}(,\d{3})*(\.\d+)?[a-zA-Z ]*$/;
|
|
43
|
-
if (regex.test(oBytes)) {
|
|
44
|
-
bytes = oBytes.replace(/,/g, "");
|
|
45
|
-
if (is.isStringNumber(bytes) || is.isNumber(bytes) || base.getType(bytes) !== "string") {
|
|
46
|
-
return parseDigitThounsands(bytes);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
const bytesRegex = /^(\d+(?:\.\d+)?)\s*([BKMGTPEZY]?B|Byte)$/i;
|
|
50
|
-
const units = {
|
|
51
|
-
B: 1,
|
|
52
|
-
BYTE: 1,
|
|
53
|
-
KB: 1024,
|
|
54
|
-
MB: 1024 ** 2,
|
|
55
|
-
GB: 1024 ** 3,
|
|
56
|
-
TB: 1024 ** 4,
|
|
57
|
-
PB: 1024 ** 5,
|
|
58
|
-
EB: 1024 ** 6,
|
|
59
|
-
ZB: 1024 ** 7,
|
|
60
|
-
YB: 1024 ** 8
|
|
61
|
-
};
|
|
62
|
-
const match = bytes.match(bytesRegex);
|
|
63
|
-
if (!match) {
|
|
64
|
-
console.warn("Invalid bytes format. Please provide a valid bytes string, like '100GB'.");
|
|
65
|
-
return;
|
|
66
|
-
}
|
|
67
|
-
const size = parseFloat(match[1]);
|
|
68
|
-
const unit = match[2].toUpperCase();
|
|
69
|
-
if (!units.hasOwnProperty(unit)) {
|
|
70
|
-
console.warn(
|
|
71
|
-
"Invalid bytes unit. Please provide a valid unit, like 'B', 'BYTE', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', or 'YB'."
|
|
72
|
-
);
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
function parseDigitThounsands(val) {
|
|
76
|
-
let finalRes = val;
|
|
77
|
-
if (digit) {
|
|
78
|
-
finalRes = Number(finalRes).toFixed(digit);
|
|
79
|
-
}
|
|
80
|
-
if (thounsands) {
|
|
81
|
-
finalRes = formatThousands(finalRes);
|
|
82
|
-
}
|
|
83
|
-
return finalRes;
|
|
84
|
-
}
|
|
85
|
-
return parseDigitThounsands(size * units[unit]);
|
|
86
|
-
}
|
|
87
|
-
function formatThousands(number) {
|
|
88
|
-
let matches = ("" + number).match(/^([\d,]+)(\.?)(\d+)?(\D+)?$/);
|
|
89
|
-
if (!matches) {
|
|
90
|
-
return number;
|
|
91
|
-
}
|
|
92
|
-
let numericString = matches[1].replace(/\D/g, "");
|
|
93
|
-
let decimalString = matches[3] ? `.${matches[3]}` : "";
|
|
94
|
-
let unit = matches[4] || "";
|
|
95
|
-
let numberWithSeparator = numericString.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
96
|
-
return `${numberWithSeparator}${decimalString}${unit}`;
|
|
97
|
-
}
|
|
98
|
-
function formatTime(time = /* @__PURE__ */ new Date(), cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
|
|
99
|
-
let date;
|
|
100
|
-
const timeStr = String(time);
|
|
101
|
-
if (typeof time === "object" && time instanceof Date) {
|
|
102
|
-
date = time;
|
|
103
|
-
} else {
|
|
104
|
-
const isoRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?$/;
|
|
105
|
-
if (isoRegex.test(timeStr)) {
|
|
106
|
-
date = new Date(time);
|
|
107
|
-
} else {
|
|
108
|
-
if (timeStr.includes(".") && !isNaN(parseFloat(timeStr))) {
|
|
109
|
-
date = new Date(parseFloat(timeStr) * 1e3);
|
|
110
|
-
} else if (/^\d{10}$/.test(timeStr)) {
|
|
111
|
-
date = new Date(parseInt(timeStr) * 1e3);
|
|
112
|
-
} else {
|
|
113
|
-
date = new Date(time);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
if (isNaN(date.getTime())) {
|
|
118
|
-
throw new Error("Invalid Date");
|
|
119
|
-
}
|
|
120
|
-
const formatObj = {
|
|
121
|
-
y: date.getFullYear(),
|
|
122
|
-
m: date.getMonth() + 1,
|
|
123
|
-
d: date.getDate(),
|
|
124
|
-
h: date.getHours(),
|
|
125
|
-
i: date.getMinutes(),
|
|
126
|
-
s: date.getSeconds(),
|
|
127
|
-
a: date.getDay()
|
|
128
|
-
};
|
|
129
|
-
return cFormat.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
|
130
|
-
const value = formatObj[key];
|
|
131
|
-
if (key === "a") {
|
|
132
|
-
return ["\u65E5", "\u4E00", "\u4E8C", "\u4E09", "\u56DB", "\u4E94", "\u516D"][value];
|
|
133
|
-
}
|
|
134
|
-
if (result.length > 0 && value < 10) {
|
|
135
|
-
return "0" + value;
|
|
136
|
-
}
|
|
137
|
-
return value;
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
function formatDurationTime(timestamp, cFormat = "{d}\u5929{h}\u65F6{i}\u5206{s}\u79D2") {
|
|
141
|
-
const secondsPerMinute = 60;
|
|
142
|
-
const minutesPerHour = 60;
|
|
143
|
-
const hoursPerDay = 24;
|
|
144
|
-
let totalSeconds = Math.floor(timestamp / 1e3);
|
|
145
|
-
let days = 0;
|
|
146
|
-
if (cFormat.indexOf("d") !== -1) {
|
|
147
|
-
days = Math.floor(totalSeconds / (secondsPerMinute * minutesPerHour * hoursPerDay));
|
|
148
|
-
totalSeconds %= secondsPerMinute * minutesPerHour * hoursPerDay;
|
|
149
|
-
}
|
|
150
|
-
let hours = Math.floor(totalSeconds / (secondsPerMinute * minutesPerHour));
|
|
151
|
-
totalSeconds %= secondsPerMinute * minutesPerHour;
|
|
152
|
-
let minutes = Math.floor(totalSeconds / secondsPerMinute);
|
|
153
|
-
let seconds = totalSeconds % secondsPerMinute;
|
|
154
|
-
const formatObj = {
|
|
155
|
-
d: days,
|
|
156
|
-
h: hours,
|
|
157
|
-
i: minutes,
|
|
158
|
-
s: seconds
|
|
159
|
-
};
|
|
160
|
-
let parseFormat = cFormat;
|
|
161
|
-
if (days === 0) {
|
|
162
|
-
parseFormat = cFormat.match(/{h}.*/g)?.[0] ?? "";
|
|
163
|
-
if (hours === 0) {
|
|
164
|
-
parseFormat = cFormat.match(/{i}.*/g)?.[0] ?? "";
|
|
165
|
-
if (minutes === 0) {
|
|
166
|
-
parseFormat = cFormat.match(/{s}.*/g)?.[0] ?? "";
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
const time_str = parseFormat.replace(/{(y|m|d|h|i|s)+}/g, (result, key) => {
|
|
171
|
-
let value = formatObj[key];
|
|
172
|
-
if (result.length > 0 && value < 10 && value != 0) {
|
|
173
|
-
value = "0" + value;
|
|
174
|
-
}
|
|
175
|
-
return value || "00";
|
|
176
|
-
});
|
|
177
|
-
return time_str;
|
|
178
|
-
}
|
|
179
|
-
function formatImg(photoName, addPath = "", { basePath = "assets/images" } = {}) {
|
|
180
|
-
if (photoName.startsWith("http") || photoName.startsWith("https")) {
|
|
181
|
-
return photoName;
|
|
182
|
-
}
|
|
183
|
-
if (photoName.indexOf(".") === -1) {
|
|
184
|
-
photoName = photoName + ".png";
|
|
185
|
-
}
|
|
186
|
-
const addLastSlash = addPath.endsWith("/") || !addPath ? addPath : `${addPath}/`;
|
|
187
|
-
const addLastBasePathSlash = basePath.endsWith("/") || !basePath ? basePath : `${basePath}/`;
|
|
188
|
-
let mergeSrc = `${addLastSlash}${photoName}`;
|
|
189
|
-
let res = new URL(`../${addLastBasePathSlash}${mergeSrc}`, (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('format.cjs', document.baseURI).href))).href;
|
|
190
|
-
return res;
|
|
191
|
-
}
|
|
192
|
-
function formatToFixed(value, options) {
|
|
193
|
-
if (typeof options === "number") {
|
|
194
|
-
options = { digit: options };
|
|
195
|
-
}
|
|
196
|
-
const finalOptions = {
|
|
197
|
-
digit: 2,
|
|
198
|
-
prefix: "",
|
|
199
|
-
suffix: "",
|
|
200
|
-
unit: true,
|
|
201
|
-
thousands: false,
|
|
202
|
-
...options
|
|
203
|
-
};
|
|
204
|
-
const { digit, prefix, suffix, unit, thousands } = finalOptions;
|
|
205
|
-
let matches = ("" + value).match(/^([\d,]+)(\.?)(\d+)?(\D+)?$/);
|
|
206
|
-
if (!matches) {
|
|
207
|
-
return value;
|
|
208
|
-
}
|
|
209
|
-
let numericString = matches[1].replace(/\D/g, "");
|
|
210
|
-
let decimalString = matches[3] ? `.${matches[3]}` : "";
|
|
211
|
-
let finalUnit = matches[4] || "";
|
|
212
|
-
let res = numericString;
|
|
213
|
-
if (is.isStringNumber(numericString) || is.isNumber(numericString)) {
|
|
214
|
-
res = Number(numericString + decimalString).toFixed(digit);
|
|
215
|
-
}
|
|
216
|
-
if (thousands) {
|
|
217
|
-
res = formatThousands(res);
|
|
218
|
-
}
|
|
219
|
-
if (!unit) {
|
|
220
|
-
finalUnit = "";
|
|
221
|
-
}
|
|
222
|
-
return `${prefix}${res}${finalUnit}${suffix}`;
|
|
223
|
-
}
|
|
224
|
-
function formatTextToHtml(str) {
|
|
225
|
-
if (!str || typeof str !== "string") {
|
|
226
|
-
return str;
|
|
227
|
-
}
|
|
228
|
-
str = str.replace(/\n/g, "<br>");
|
|
229
|
-
str = str.replace(/\t/g, " ");
|
|
230
|
-
return str;
|
|
231
|
-
}
|
|
232
10
|
|
|
233
|
-
|
|
234
|
-
exports.
|
|
235
|
-
exports.
|
|
236
|
-
exports.
|
|
237
|
-
exports.
|
|
238
|
-
exports.
|
|
239
|
-
exports.
|
|
240
|
-
exports.
|
|
11
|
+
|
|
12
|
+
exports.formatBytes = format.formatBytes;
|
|
13
|
+
exports.formatBytesConvert = format.formatBytesConvert;
|
|
14
|
+
exports.formatDurationTime = format.formatDurationTime;
|
|
15
|
+
exports.formatImg = format.formatImg;
|
|
16
|
+
exports.formatTextToHtml = format.formatTextToHtml;
|
|
17
|
+
exports.formatThousands = format.formatThousands;
|
|
18
|
+
exports.formatTime = format.formatTime;
|
|
19
|
+
exports.formatToFixed = format.formatToFixed;
|
package/dist/format.d.cts
CHANGED
|
@@ -63,15 +63,17 @@ declare function formatBytesConvert<const T extends {
|
|
|
63
63
|
*/
|
|
64
64
|
declare function formatThousands(number: any): any;
|
|
65
65
|
type TimeType = Date | string | number;
|
|
66
|
+
type TimeFallback = string | ((time: TimeType) => string);
|
|
66
67
|
/**
|
|
67
68
|
* 时间格式化函数
|
|
68
69
|
* @param {TimeType} time - 可选时间参数,可以是 Date 对象、时间戳字符串或数字
|
|
69
70
|
* @param {FormatType} cFormat - '{y}-{m}-{d} {h}:{i}:{s}' - 格式化字符串,支持 {y}年 {m}月 {d}日 {h}时 {i}分 {s}秒 {a}星期
|
|
71
|
+
* @param {string | ((time: TimeType) => string)} fallback - 非法日期时返回的兜底值,默认返回原始值,也支持传固定文案或函数
|
|
70
72
|
* @returns {string} 格式化后的时间字符串
|
|
71
73
|
* @examples
|
|
72
74
|
* formatTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s} 星期{a}') // 示例输出: 2026-02-27 14:47:45 星期五 (实际结果取决于调用时的具体时间)
|
|
73
75
|
*/
|
|
74
|
-
declare function formatTime(time?: TimeType, cFormat?: string): string;
|
|
76
|
+
declare function formatTime(time?: TimeType, cFormat?: string, fallback?: TimeFallback): string;
|
|
75
77
|
/**
|
|
76
78
|
*
|
|
77
79
|
* @param timestamp 持续的时间戳
|
package/dist/format.d.mts
CHANGED
|
@@ -63,15 +63,17 @@ declare function formatBytesConvert<const T extends {
|
|
|
63
63
|
*/
|
|
64
64
|
declare function formatThousands(number: any): any;
|
|
65
65
|
type TimeType = Date | string | number;
|
|
66
|
+
type TimeFallback = string | ((time: TimeType) => string);
|
|
66
67
|
/**
|
|
67
68
|
* 时间格式化函数
|
|
68
69
|
* @param {TimeType} time - 可选时间参数,可以是 Date 对象、时间戳字符串或数字
|
|
69
70
|
* @param {FormatType} cFormat - '{y}-{m}-{d} {h}:{i}:{s}' - 格式化字符串,支持 {y}年 {m}月 {d}日 {h}时 {i}分 {s}秒 {a}星期
|
|
71
|
+
* @param {string | ((time: TimeType) => string)} fallback - 非法日期时返回的兜底值,默认返回原始值,也支持传固定文案或函数
|
|
70
72
|
* @returns {string} 格式化后的时间字符串
|
|
71
73
|
* @examples
|
|
72
74
|
* formatTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s} 星期{a}') // 示例输出: 2026-02-27 14:47:45 星期五 (实际结果取决于调用时的具体时间)
|
|
73
75
|
*/
|
|
74
|
-
declare function formatTime(time?: TimeType, cFormat?: string): string;
|
|
76
|
+
declare function formatTime(time?: TimeType, cFormat?: string, fallback?: TimeFallback): string;
|
|
75
77
|
/**
|
|
76
78
|
*
|
|
77
79
|
* @param timestamp 持续的时间戳
|
package/dist/format.d.ts
CHANGED
|
@@ -63,15 +63,17 @@ declare function formatBytesConvert<const T extends {
|
|
|
63
63
|
*/
|
|
64
64
|
declare function formatThousands(number: any): any;
|
|
65
65
|
type TimeType = Date | string | number;
|
|
66
|
+
type TimeFallback = string | ((time: TimeType) => string);
|
|
66
67
|
/**
|
|
67
68
|
* 时间格式化函数
|
|
68
69
|
* @param {TimeType} time - 可选时间参数,可以是 Date 对象、时间戳字符串或数字
|
|
69
70
|
* @param {FormatType} cFormat - '{y}-{m}-{d} {h}:{i}:{s}' - 格式化字符串,支持 {y}年 {m}月 {d}日 {h}时 {i}分 {s}秒 {a}星期
|
|
71
|
+
* @param {string | ((time: TimeType) => string)} fallback - 非法日期时返回的兜底值,默认返回原始值,也支持传固定文案或函数
|
|
70
72
|
* @returns {string} 格式化后的时间字符串
|
|
71
73
|
* @examples
|
|
72
74
|
* formatTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s} 星期{a}') // 示例输出: 2026-02-27 14:47:45 星期五 (实际结果取决于调用时的具体时间)
|
|
73
75
|
*/
|
|
74
|
-
declare function formatTime(time?: TimeType, cFormat?: string): string;
|
|
76
|
+
declare function formatTime(time?: TimeType, cFormat?: string, fallback?: TimeFallback): string;
|
|
75
77
|
/**
|
|
76
78
|
*
|
|
77
79
|
* @param timestamp 持续的时间戳
|
package/dist/format.mjs
CHANGED
|
@@ -1,230 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
1
|
+
import './is.mjs';
|
|
2
|
+
export { z as formatBytes, A as formatBytesConvert, B as formatDurationTime, C as formatImg, D as formatTextToHtml, E as formatThousands, F as formatTime, G as formatToFixed } from './shared/utils.DRGlWWeY.mjs';
|
|
3
3
|
import '@vue/reactivity';
|
|
4
4
|
import 'consola';
|
|
5
5
|
import 'es-toolkit';
|
|
6
6
|
import 'element-plus';
|
|
7
|
-
|
|
8
|
-
function formatBytes(bytes, options) {
|
|
9
|
-
let { digit = 2, thousands = false, prefix = "", suffix = "", roundType = "floor" } = options ?? {};
|
|
10
|
-
if (isStringNumber(bytes) || isNumber(bytes)) {
|
|
11
|
-
bytes = Number(bytes);
|
|
12
|
-
} else {
|
|
13
|
-
return bytes;
|
|
14
|
-
}
|
|
15
|
-
if (bytes <= 1) {
|
|
16
|
-
return Math[roundType](bytes * Math.pow(10, digit)) / Math.pow(10, digit) + " B";
|
|
17
|
-
}
|
|
18
|
-
const k = 1024;
|
|
19
|
-
const sizes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
|
20
|
-
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
21
|
-
const power = Math.pow(k, i);
|
|
22
|
-
let num = bytes / power;
|
|
23
|
-
num = Math[roundType](num * Math.pow(10, digit)) / Math.pow(10, digit);
|
|
24
|
-
let res = num.toFixed(digit) + " " + sizes[i];
|
|
25
|
-
if (thousands) {
|
|
26
|
-
res = formatThousands(res);
|
|
27
|
-
}
|
|
28
|
-
return `${prefix}${res}${suffix}`;
|
|
29
|
-
}
|
|
30
|
-
function formatBytesConvert(oBytes, options) {
|
|
31
|
-
let { thounsands = false, digit = 0 } = options ?? {};
|
|
32
|
-
let bytes = oBytes;
|
|
33
|
-
if (isStringNumber(oBytes) || isNumber(oBytes) || getType(oBytes) !== "string") {
|
|
34
|
-
return parseDigitThounsands(oBytes);
|
|
35
|
-
}
|
|
36
|
-
if (!oBytes) {
|
|
37
|
-
return parseDigitThounsands(oBytes);
|
|
38
|
-
}
|
|
39
|
-
const regex = /^\d{1,3}(,\d{3})*(\.\d+)?[a-zA-Z ]*$/;
|
|
40
|
-
if (regex.test(oBytes)) {
|
|
41
|
-
bytes = oBytes.replace(/,/g, "");
|
|
42
|
-
if (isStringNumber(bytes) || isNumber(bytes) || getType(bytes) !== "string") {
|
|
43
|
-
return parseDigitThounsands(bytes);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
const bytesRegex = /^(\d+(?:\.\d+)?)\s*([BKMGTPEZY]?B|Byte)$/i;
|
|
47
|
-
const units = {
|
|
48
|
-
B: 1,
|
|
49
|
-
BYTE: 1,
|
|
50
|
-
KB: 1024,
|
|
51
|
-
MB: 1024 ** 2,
|
|
52
|
-
GB: 1024 ** 3,
|
|
53
|
-
TB: 1024 ** 4,
|
|
54
|
-
PB: 1024 ** 5,
|
|
55
|
-
EB: 1024 ** 6,
|
|
56
|
-
ZB: 1024 ** 7,
|
|
57
|
-
YB: 1024 ** 8
|
|
58
|
-
};
|
|
59
|
-
const match = bytes.match(bytesRegex);
|
|
60
|
-
if (!match) {
|
|
61
|
-
console.warn("Invalid bytes format. Please provide a valid bytes string, like '100GB'.");
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
const size = parseFloat(match[1]);
|
|
65
|
-
const unit = match[2].toUpperCase();
|
|
66
|
-
if (!units.hasOwnProperty(unit)) {
|
|
67
|
-
console.warn(
|
|
68
|
-
"Invalid bytes unit. Please provide a valid unit, like 'B', 'BYTE', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', or 'YB'."
|
|
69
|
-
);
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
function parseDigitThounsands(val) {
|
|
73
|
-
let finalRes = val;
|
|
74
|
-
if (digit) {
|
|
75
|
-
finalRes = Number(finalRes).toFixed(digit);
|
|
76
|
-
}
|
|
77
|
-
if (thounsands) {
|
|
78
|
-
finalRes = formatThousands(finalRes);
|
|
79
|
-
}
|
|
80
|
-
return finalRes;
|
|
81
|
-
}
|
|
82
|
-
return parseDigitThounsands(size * units[unit]);
|
|
83
|
-
}
|
|
84
|
-
function formatThousands(number) {
|
|
85
|
-
let matches = ("" + number).match(/^([\d,]+)(\.?)(\d+)?(\D+)?$/);
|
|
86
|
-
if (!matches) {
|
|
87
|
-
return number;
|
|
88
|
-
}
|
|
89
|
-
let numericString = matches[1].replace(/\D/g, "");
|
|
90
|
-
let decimalString = matches[3] ? `.${matches[3]}` : "";
|
|
91
|
-
let unit = matches[4] || "";
|
|
92
|
-
let numberWithSeparator = numericString.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
93
|
-
return `${numberWithSeparator}${decimalString}${unit}`;
|
|
94
|
-
}
|
|
95
|
-
function formatTime(time = /* @__PURE__ */ new Date(), cFormat = "{y}-{m}-{d} {h}:{i}:{s}") {
|
|
96
|
-
let date;
|
|
97
|
-
const timeStr = String(time);
|
|
98
|
-
if (typeof time === "object" && time instanceof Date) {
|
|
99
|
-
date = time;
|
|
100
|
-
} else {
|
|
101
|
-
const isoRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d{1,3})?)?$/;
|
|
102
|
-
if (isoRegex.test(timeStr)) {
|
|
103
|
-
date = new Date(time);
|
|
104
|
-
} else {
|
|
105
|
-
if (timeStr.includes(".") && !isNaN(parseFloat(timeStr))) {
|
|
106
|
-
date = new Date(parseFloat(timeStr) * 1e3);
|
|
107
|
-
} else if (/^\d{10}$/.test(timeStr)) {
|
|
108
|
-
date = new Date(parseInt(timeStr) * 1e3);
|
|
109
|
-
} else {
|
|
110
|
-
date = new Date(time);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
if (isNaN(date.getTime())) {
|
|
115
|
-
throw new Error("Invalid Date");
|
|
116
|
-
}
|
|
117
|
-
const formatObj = {
|
|
118
|
-
y: date.getFullYear(),
|
|
119
|
-
m: date.getMonth() + 1,
|
|
120
|
-
d: date.getDate(),
|
|
121
|
-
h: date.getHours(),
|
|
122
|
-
i: date.getMinutes(),
|
|
123
|
-
s: date.getSeconds(),
|
|
124
|
-
a: date.getDay()
|
|
125
|
-
};
|
|
126
|
-
return cFormat.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
|
127
|
-
const value = formatObj[key];
|
|
128
|
-
if (key === "a") {
|
|
129
|
-
return ["\u65E5", "\u4E00", "\u4E8C", "\u4E09", "\u56DB", "\u4E94", "\u516D"][value];
|
|
130
|
-
}
|
|
131
|
-
if (result.length > 0 && value < 10) {
|
|
132
|
-
return "0" + value;
|
|
133
|
-
}
|
|
134
|
-
return value;
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
function formatDurationTime(timestamp, cFormat = "{d}\u5929{h}\u65F6{i}\u5206{s}\u79D2") {
|
|
138
|
-
const secondsPerMinute = 60;
|
|
139
|
-
const minutesPerHour = 60;
|
|
140
|
-
const hoursPerDay = 24;
|
|
141
|
-
let totalSeconds = Math.floor(timestamp / 1e3);
|
|
142
|
-
let days = 0;
|
|
143
|
-
if (cFormat.indexOf("d") !== -1) {
|
|
144
|
-
days = Math.floor(totalSeconds / (secondsPerMinute * minutesPerHour * hoursPerDay));
|
|
145
|
-
totalSeconds %= secondsPerMinute * minutesPerHour * hoursPerDay;
|
|
146
|
-
}
|
|
147
|
-
let hours = Math.floor(totalSeconds / (secondsPerMinute * minutesPerHour));
|
|
148
|
-
totalSeconds %= secondsPerMinute * minutesPerHour;
|
|
149
|
-
let minutes = Math.floor(totalSeconds / secondsPerMinute);
|
|
150
|
-
let seconds = totalSeconds % secondsPerMinute;
|
|
151
|
-
const formatObj = {
|
|
152
|
-
d: days,
|
|
153
|
-
h: hours,
|
|
154
|
-
i: minutes,
|
|
155
|
-
s: seconds
|
|
156
|
-
};
|
|
157
|
-
let parseFormat = cFormat;
|
|
158
|
-
if (days === 0) {
|
|
159
|
-
parseFormat = cFormat.match(/{h}.*/g)?.[0] ?? "";
|
|
160
|
-
if (hours === 0) {
|
|
161
|
-
parseFormat = cFormat.match(/{i}.*/g)?.[0] ?? "";
|
|
162
|
-
if (minutes === 0) {
|
|
163
|
-
parseFormat = cFormat.match(/{s}.*/g)?.[0] ?? "";
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
const time_str = parseFormat.replace(/{(y|m|d|h|i|s)+}/g, (result, key) => {
|
|
168
|
-
let value = formatObj[key];
|
|
169
|
-
if (result.length > 0 && value < 10 && value != 0) {
|
|
170
|
-
value = "0" + value;
|
|
171
|
-
}
|
|
172
|
-
return value || "00";
|
|
173
|
-
});
|
|
174
|
-
return time_str;
|
|
175
|
-
}
|
|
176
|
-
function formatImg(photoName, addPath = "", { basePath = "assets/images" } = {}) {
|
|
177
|
-
if (photoName.startsWith("http") || photoName.startsWith("https")) {
|
|
178
|
-
return photoName;
|
|
179
|
-
}
|
|
180
|
-
if (photoName.indexOf(".") === -1) {
|
|
181
|
-
photoName = photoName + ".png";
|
|
182
|
-
}
|
|
183
|
-
const addLastSlash = addPath.endsWith("/") || !addPath ? addPath : `${addPath}/`;
|
|
184
|
-
const addLastBasePathSlash = basePath.endsWith("/") || !basePath ? basePath : `${basePath}/`;
|
|
185
|
-
let mergeSrc = `${addLastSlash}${photoName}`;
|
|
186
|
-
let res = new URL(`../${addLastBasePathSlash}${mergeSrc}`, import.meta.url).href;
|
|
187
|
-
return res;
|
|
188
|
-
}
|
|
189
|
-
function formatToFixed(value, options) {
|
|
190
|
-
if (typeof options === "number") {
|
|
191
|
-
options = { digit: options };
|
|
192
|
-
}
|
|
193
|
-
const finalOptions = {
|
|
194
|
-
digit: 2,
|
|
195
|
-
prefix: "",
|
|
196
|
-
suffix: "",
|
|
197
|
-
unit: true,
|
|
198
|
-
thousands: false,
|
|
199
|
-
...options
|
|
200
|
-
};
|
|
201
|
-
const { digit, prefix, suffix, unit, thousands } = finalOptions;
|
|
202
|
-
let matches = ("" + value).match(/^([\d,]+)(\.?)(\d+)?(\D+)?$/);
|
|
203
|
-
if (!matches) {
|
|
204
|
-
return value;
|
|
205
|
-
}
|
|
206
|
-
let numericString = matches[1].replace(/\D/g, "");
|
|
207
|
-
let decimalString = matches[3] ? `.${matches[3]}` : "";
|
|
208
|
-
let finalUnit = matches[4] || "";
|
|
209
|
-
let res = numericString;
|
|
210
|
-
if (isStringNumber(numericString) || isNumber(numericString)) {
|
|
211
|
-
res = Number(numericString + decimalString).toFixed(digit);
|
|
212
|
-
}
|
|
213
|
-
if (thousands) {
|
|
214
|
-
res = formatThousands(res);
|
|
215
|
-
}
|
|
216
|
-
if (!unit) {
|
|
217
|
-
finalUnit = "";
|
|
218
|
-
}
|
|
219
|
-
return `${prefix}${res}${finalUnit}${suffix}`;
|
|
220
|
-
}
|
|
221
|
-
function formatTextToHtml(str) {
|
|
222
|
-
if (!str || typeof str !== "string") {
|
|
223
|
-
return str;
|
|
224
|
-
}
|
|
225
|
-
str = str.replace(/\n/g, "<br>");
|
|
226
|
-
str = str.replace(/\t/g, " ");
|
|
227
|
-
return str;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
export { formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatTextToHtml, formatThousands, formatTime, formatToFixed };
|
package/dist/index.cjs
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const format = require('./shared/utils.CPo2h8Qi.cjs');
|
|
4
4
|
const day = require('./day.cjs');
|
|
5
5
|
const is = require('./is.cjs');
|
|
6
6
|
const ws = require('./ws.cjs');
|
|
7
|
-
const format = require('./format.cjs');
|
|
8
7
|
const test = require('./test.cjs');
|
|
9
8
|
require('@vue/reactivity');
|
|
10
9
|
require('consola');
|
|
@@ -15,32 +14,40 @@ require('qs');
|
|
|
15
14
|
|
|
16
15
|
|
|
17
16
|
|
|
18
|
-
exports.$toast =
|
|
19
|
-
exports.asyncWrapper =
|
|
20
|
-
exports.clearStorage =
|
|
21
|
-
exports.clone =
|
|
22
|
-
exports.confirm =
|
|
23
|
-
exports.copy =
|
|
24
|
-
exports.debounce =
|
|
25
|
-
exports.
|
|
26
|
-
exports.
|
|
27
|
-
exports.
|
|
28
|
-
exports.
|
|
29
|
-
exports.
|
|
30
|
-
exports.
|
|
31
|
-
exports.
|
|
32
|
-
exports.
|
|
33
|
-
exports.
|
|
34
|
-
exports.
|
|
35
|
-
exports.
|
|
36
|
-
exports.
|
|
37
|
-
exports.
|
|
38
|
-
exports.
|
|
39
|
-
exports.
|
|
40
|
-
exports.
|
|
41
|
-
exports.
|
|
42
|
-
exports.
|
|
43
|
-
exports.
|
|
17
|
+
exports.$toast = format.$toast;
|
|
18
|
+
exports.asyncWrapper = format.asyncWrapper;
|
|
19
|
+
exports.clearStorage = format.clearStorage;
|
|
20
|
+
exports.clone = format.clone;
|
|
21
|
+
exports.confirm = format.confirm;
|
|
22
|
+
exports.copy = format.copy;
|
|
23
|
+
exports.debounce = format.debounce;
|
|
24
|
+
exports.formatBytes = format.formatBytes;
|
|
25
|
+
exports.formatBytesConvert = format.formatBytesConvert;
|
|
26
|
+
exports.formatDurationTime = format.formatDurationTime;
|
|
27
|
+
exports.formatImg = format.formatImg;
|
|
28
|
+
exports.formatTextToHtml = format.formatTextToHtml;
|
|
29
|
+
exports.formatThousands = format.formatThousands;
|
|
30
|
+
exports.formatTime = format.formatTime;
|
|
31
|
+
exports.formatToFixed = format.formatToFixed;
|
|
32
|
+
exports.getStorage = format.getStorage;
|
|
33
|
+
exports.getType = format.getType;
|
|
34
|
+
exports.getVariable = format.getVariable;
|
|
35
|
+
exports.isEmpty = format.isEmpty;
|
|
36
|
+
exports.log = format.log;
|
|
37
|
+
exports.merge = format.merge;
|
|
38
|
+
exports.notEmpty = format.notEmpty;
|
|
39
|
+
exports.processWidth = format.processWidth;
|
|
40
|
+
exports.random = format.random;
|
|
41
|
+
exports.setStorage = format.setStorage;
|
|
42
|
+
exports.sleep = format.sleep;
|
|
43
|
+
exports.test = format.test;
|
|
44
|
+
exports.throttle = format.throttle;
|
|
45
|
+
exports.toLine = format.toLine;
|
|
46
|
+
exports.tryCatch = format.tryCatch;
|
|
47
|
+
exports.uuid = format.uuid;
|
|
48
|
+
exports.validForm = format.validForm;
|
|
49
|
+
exports.validate = format.validate;
|
|
50
|
+
exports.validateTrigger = format.validateTrigger;
|
|
44
51
|
exports.diffDate = day.diffDate;
|
|
45
52
|
exports.diffDateFromCurrent = day.diffDateFromCurrent;
|
|
46
53
|
exports.formatDate = day.formatDate;
|
|
@@ -69,12 +76,4 @@ exports.objectToString = is.objectToString;
|
|
|
69
76
|
exports.toRawType = is.toRawType;
|
|
70
77
|
exports.toTypeString = is.toTypeString;
|
|
71
78
|
exports.WS = ws.WS;
|
|
72
|
-
exports.formatBytes = format.formatBytes;
|
|
73
|
-
exports.formatBytesConvert = format.formatBytesConvert;
|
|
74
|
-
exports.formatDurationTime = format.formatDurationTime;
|
|
75
|
-
exports.formatImg = format.formatImg;
|
|
76
|
-
exports.formatTextToHtml = format.formatTextToHtml;
|
|
77
|
-
exports.formatThousands = format.formatThousands;
|
|
78
|
-
exports.formatTime = format.formatTime;
|
|
79
|
-
exports.formatToFixed = format.formatToFixed;
|
|
80
79
|
exports.onlyTest = test.onlyTest;
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
export { $toast, asyncWrapper, clearStorage, clone, confirm, copy, debounce, getStorage, getType, getVariable, isEmpty, log, merge, notEmpty, processWidth, random, setStorage, sleep, test, throttle, toLine, tryCatch, uuid, validForm, validate, validateTrigger } from './
|
|
1
|
+
export { $ as $toast, a as asyncWrapper, c as clearStorage, b as clone, d as confirm, e as copy, f as debounce, z as formatBytes, A as formatBytesConvert, B as formatDurationTime, C as formatImg, D as formatTextToHtml, E as formatThousands, F as formatTime, G as formatToFixed, g as getStorage, h as getType, i as getVariable, j as isEmpty, l as log, m as merge, n as notEmpty, p as processWidth, r as random, s as setStorage, k as sleep, t as test, o as throttle, q as toLine, u as tryCatch, v as uuid, w as validForm, x as validate, y as validateTrigger } from './shared/utils.DRGlWWeY.mjs';
|
|
2
2
|
export { diffDate, diffDateFromCurrent, formatDate, formatDateToDay, formatDateToMinute } from './day.mjs';
|
|
3
3
|
export { isArray, isBoolean, isComponent, isDate, isEmptyObject, isFunction, isIOS, isMap, isNumber, isObject, isPlainObject, isPromise, isRegExp, isSVGElement, isSet, isString, isStringNumber, isSymbol, isUrl, objectToString, toRawType, toTypeString } from './is.mjs';
|
|
4
4
|
export { WS } from './ws.mjs';
|
|
5
|
-
export { formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatTextToHtml, formatThousands, formatTime, formatToFixed } from './format.mjs';
|
|
6
5
|
export { onlyTest } from './test.mjs';
|
|
7
6
|
import '@vue/reactivity';
|
|
8
7
|
import 'consola';
|