@oeos-components/utils 0.0.19 → 0.0.22
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 +682 -0
- package/dist/base.d.cts +342 -0
- package/dist/base.d.mts +342 -0
- package/dist/base.d.ts +342 -0
- package/dist/base.mjs +655 -0
- package/dist/day.cjs +41 -0
- package/dist/day.d.cts +37 -0
- package/dist/day.d.mts +37 -0
- package/dist/day.d.ts +37 -0
- package/dist/day.mjs +31 -0
- package/dist/format.cjs +240 -0
- package/dist/format.d.cts +134 -0
- package/dist/format.d.mts +134 -0
- package/dist/format.d.ts +134 -0
- package/dist/format.mjs +230 -0
- package/dist/index.cjs +76 -858
- package/dist/index.d.cts +10 -404
- package/dist/index.d.mts +10 -404
- package/dist/index.d.ts +10 -404
- package/dist/index.mjs +12 -823
- package/dist/is.cjs +66 -0
- package/dist/is.d.cts +124 -0
- package/dist/is.d.mts +124 -0
- package/dist/is.d.ts +124 -0
- package/dist/is.mjs +43 -0
- package/dist/test.cjs +7 -0
- package/dist/test.d.cts +3 -0
- package/dist/test.d.mts +3 -0
- package/dist/test.d.ts +3 -0
- package/dist/test.mjs +5 -0
- package/dist/ws.cjs +120 -0
- package/dist/ws.d.cts +86 -0
- package/dist/ws.d.mts +86 -0
- package/dist/ws.d.ts +86 -0
- package/dist/ws.mjs +114 -0
- package/package.json +3 -1
package/dist/day.d.cts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ConfigType, QUnitType, OpUnitType } from 'dayjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param {string | number | Date | Dayjs | null | undefined} date
|
|
6
|
+
* @param format 默认 YYYY-MM-DD HH:mm:ss
|
|
7
|
+
* @returns YYYY-MM-DD HH:mm:ss
|
|
8
|
+
*/
|
|
9
|
+
declare function formatDate(date?: ConfigType, format?: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* 格式化时间到天
|
|
12
|
+
* @param {string | number | Date | Dayjs | null | undefined} date
|
|
13
|
+
* @returns YYYY-MM-DD
|
|
14
|
+
*/
|
|
15
|
+
declare const formatDateToDay: (date: ConfigType) => string;
|
|
16
|
+
/**
|
|
17
|
+
* 格式化时间到分钟
|
|
18
|
+
* @param {string | number | Date | Dayjs | null | undefined} date
|
|
19
|
+
* @returns YYYY-MM-DD HH:mm
|
|
20
|
+
*/
|
|
21
|
+
declare const formatDateToMinute: (date: ConfigType) => string;
|
|
22
|
+
/**
|
|
23
|
+
* 获取两个 Dayjs 对象的时间差,默认秒。
|
|
24
|
+
* @param date1
|
|
25
|
+
* @param date2
|
|
26
|
+
* @param format
|
|
27
|
+
* @returns {*}
|
|
28
|
+
*/
|
|
29
|
+
declare function diffDate(date1: ConfigType, date2?: ConfigType, format?: QUnitType | OpUnitType): number | string | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* @desc 获取距离当前时间差,返回多少年(月/天/小时/分钟/秒)前
|
|
32
|
+
* @param date 秒数
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
declare function diffDateFromCurrent(second: number): string;
|
|
36
|
+
|
|
37
|
+
export { diffDate, diffDateFromCurrent, formatDate, formatDateToDay, formatDateToMinute };
|
package/dist/day.d.mts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ConfigType, QUnitType, OpUnitType } from 'dayjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param {string | number | Date | Dayjs | null | undefined} date
|
|
6
|
+
* @param format 默认 YYYY-MM-DD HH:mm:ss
|
|
7
|
+
* @returns YYYY-MM-DD HH:mm:ss
|
|
8
|
+
*/
|
|
9
|
+
declare function formatDate(date?: ConfigType, format?: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* 格式化时间到天
|
|
12
|
+
* @param {string | number | Date | Dayjs | null | undefined} date
|
|
13
|
+
* @returns YYYY-MM-DD
|
|
14
|
+
*/
|
|
15
|
+
declare const formatDateToDay: (date: ConfigType) => string;
|
|
16
|
+
/**
|
|
17
|
+
* 格式化时间到分钟
|
|
18
|
+
* @param {string | number | Date | Dayjs | null | undefined} date
|
|
19
|
+
* @returns YYYY-MM-DD HH:mm
|
|
20
|
+
*/
|
|
21
|
+
declare const formatDateToMinute: (date: ConfigType) => string;
|
|
22
|
+
/**
|
|
23
|
+
* 获取两个 Dayjs 对象的时间差,默认秒。
|
|
24
|
+
* @param date1
|
|
25
|
+
* @param date2
|
|
26
|
+
* @param format
|
|
27
|
+
* @returns {*}
|
|
28
|
+
*/
|
|
29
|
+
declare function diffDate(date1: ConfigType, date2?: ConfigType, format?: QUnitType | OpUnitType): number | string | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* @desc 获取距离当前时间差,返回多少年(月/天/小时/分钟/秒)前
|
|
32
|
+
* @param date 秒数
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
declare function diffDateFromCurrent(second: number): string;
|
|
36
|
+
|
|
37
|
+
export { diffDate, diffDateFromCurrent, formatDate, formatDateToDay, formatDateToMinute };
|
package/dist/day.d.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ConfigType, QUnitType, OpUnitType } from 'dayjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param {string | number | Date | Dayjs | null | undefined} date
|
|
6
|
+
* @param format 默认 YYYY-MM-DD HH:mm:ss
|
|
7
|
+
* @returns YYYY-MM-DD HH:mm:ss
|
|
8
|
+
*/
|
|
9
|
+
declare function formatDate(date?: ConfigType, format?: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* 格式化时间到天
|
|
12
|
+
* @param {string | number | Date | Dayjs | null | undefined} date
|
|
13
|
+
* @returns YYYY-MM-DD
|
|
14
|
+
*/
|
|
15
|
+
declare const formatDateToDay: (date: ConfigType) => string;
|
|
16
|
+
/**
|
|
17
|
+
* 格式化时间到分钟
|
|
18
|
+
* @param {string | number | Date | Dayjs | null | undefined} date
|
|
19
|
+
* @returns YYYY-MM-DD HH:mm
|
|
20
|
+
*/
|
|
21
|
+
declare const formatDateToMinute: (date: ConfigType) => string;
|
|
22
|
+
/**
|
|
23
|
+
* 获取两个 Dayjs 对象的时间差,默认秒。
|
|
24
|
+
* @param date1
|
|
25
|
+
* @param date2
|
|
26
|
+
* @param format
|
|
27
|
+
* @returns {*}
|
|
28
|
+
*/
|
|
29
|
+
declare function diffDate(date1: ConfigType, date2?: ConfigType, format?: QUnitType | OpUnitType): number | string | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* @desc 获取距离当前时间差,返回多少年(月/天/小时/分钟/秒)前
|
|
32
|
+
* @param date 秒数
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
declare function diffDateFromCurrent(second: number): string;
|
|
36
|
+
|
|
37
|
+
export { diffDate, diffDateFromCurrent, formatDate, formatDateToDay, formatDateToMinute };
|
package/dist/day.mjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import dayjs from 'dayjs';
|
|
2
|
+
|
|
3
|
+
function formatDate(date, format = "YYYY-MM-DD HH:mm:ss") {
|
|
4
|
+
return dayjs(date || /* @__PURE__ */ new Date()).format(format);
|
|
5
|
+
}
|
|
6
|
+
const formatDateToDay = (date) => formatDate(date, "YYYY-MM-DD");
|
|
7
|
+
const formatDateToMinute = (date) => formatDate(date, "YYYY-MM-DD HH:mm");
|
|
8
|
+
function diffDate(date1, date2 = dayjs(), format = "second") {
|
|
9
|
+
if (!date1) return;
|
|
10
|
+
return dayjs(date1).diff(dayjs(date2), format);
|
|
11
|
+
}
|
|
12
|
+
function diffDateFromCurrent(second) {
|
|
13
|
+
if (second >= 60 * 60 * 24 * 365) {
|
|
14
|
+
return `${parseInt(String(second / (60 * 60 * 24 * 365)))}\u5E74\u524D`;
|
|
15
|
+
}
|
|
16
|
+
if (second >= 60 * 60 * 24 * 30) {
|
|
17
|
+
return `${parseInt(String(second / (60 * 60 * 24 * 30)))}\u6708\u524D`;
|
|
18
|
+
}
|
|
19
|
+
if (second >= 60 * 60 * 24) {
|
|
20
|
+
return `${parseInt(String(second / (60 * 60 * 24)))}\u5929\u524D`;
|
|
21
|
+
}
|
|
22
|
+
if (second >= 60 * 60) {
|
|
23
|
+
return `${parseInt(String(second / (60 * 60)))}\u5C0F\u65F6\u524D`;
|
|
24
|
+
}
|
|
25
|
+
if (second >= 60) {
|
|
26
|
+
return `${parseInt(String(second / 60))}\u5206\u949F\u524D`;
|
|
27
|
+
}
|
|
28
|
+
return `${parseInt(String(second))}\u79D2\u524D`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { diffDate, diffDateFromCurrent, formatDate, formatDateToDay, formatDateToMinute };
|
package/dist/format.cjs
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const is = require('./is.cjs');
|
|
4
|
+
const base = require('./base.cjs');
|
|
5
|
+
require('@vue/reactivity');
|
|
6
|
+
require('consola');
|
|
7
|
+
require('es-toolkit');
|
|
8
|
+
require('element-plus');
|
|
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
|
+
|
|
233
|
+
exports.formatBytes = formatBytes;
|
|
234
|
+
exports.formatBytesConvert = formatBytesConvert;
|
|
235
|
+
exports.formatDurationTime = formatDurationTime;
|
|
236
|
+
exports.formatImg = formatImg;
|
|
237
|
+
exports.formatTextToHtml = formatTextToHtml;
|
|
238
|
+
exports.formatThousands = formatThousands;
|
|
239
|
+
exports.formatTime = formatTime;
|
|
240
|
+
exports.formatToFixed = formatToFixed;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 现有方法如下
|
|
3
|
+
* formatTime(time, cFormat = '{y}-{m}-{d} {h}:{i}:{s}')
|
|
4
|
+
* formatDurationTime(timestamp, cFormat = '{d} 天 {h} 时 {i} 分 {s} 秒')
|
|
5
|
+
* formatImg(photoName, addPath = '', { basePath = 'assets/images' } = {})
|
|
6
|
+
* formatThousands(number)
|
|
7
|
+
* formatBytes(bytes)
|
|
8
|
+
* formatBytesConvert(bytes)
|
|
9
|
+
* formatNewLines(str)
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* 格式化字节单位
|
|
13
|
+
* @param bytes - 字节数
|
|
14
|
+
* @param options - 配置项
|
|
15
|
+
* @param options.digit - 小数位数(默认2)
|
|
16
|
+
* @param options.thousands - 是否千分位分隔(默认true)
|
|
17
|
+
* @param options.prefix - 前缀(默认空)
|
|
18
|
+
* @param options.suffix - 后缀(默认空)
|
|
19
|
+
* @param options.roundType - 取整方式:'floor'(向下, 默认) | 'ceil'(向上) | 'round'(四舍五入)
|
|
20
|
+
* @example
|
|
21
|
+
* formatBytes(0.999) => 0.99B
|
|
22
|
+
* formatBytes(1040000, { digit: 3, prefix: "$", suffiex: "/s", roundType: "round", thousands: true }) => $1,015.625 KB
|
|
23
|
+
*/
|
|
24
|
+
declare function formatBytes<const T extends {
|
|
25
|
+
digit?: number;
|
|
26
|
+
prefix?: string;
|
|
27
|
+
suffix?: string;
|
|
28
|
+
roundType?: 'floor' | 'ceil' | 'round';
|
|
29
|
+
thousands?: boolean;
|
|
30
|
+
} = {
|
|
31
|
+
digit: 2;
|
|
32
|
+
prefix: '';
|
|
33
|
+
suffix: '';
|
|
34
|
+
roundType: 'floor';
|
|
35
|
+
thousands: false;
|
|
36
|
+
}>(bytes: number | string, options?: T): string;
|
|
37
|
+
/**
|
|
38
|
+
* 字节转数字
|
|
39
|
+
* @param oBytes
|
|
40
|
+
* @param param1
|
|
41
|
+
* @returns number
|
|
42
|
+
* formatBytesConvert('0.5GB') 536870912
|
|
43
|
+
*
|
|
44
|
+
* formatBytesConvert('1,234 GB') 1324997410816
|
|
45
|
+
*
|
|
46
|
+
* formatBytesConvert('1,234 GB', {thousands: true}) 1,324,997,410,816
|
|
47
|
+
*
|
|
48
|
+
* formatBytesConvert('1,234 GB', {digit: 2}) 1324997410816.00
|
|
49
|
+
*/
|
|
50
|
+
declare function formatBytesConvert<const T extends {
|
|
51
|
+
thounsands?: boolean;
|
|
52
|
+
digit?: number;
|
|
53
|
+
} = {
|
|
54
|
+
thounsands: false;
|
|
55
|
+
digit: 0;
|
|
56
|
+
}>(oBytes: any, options?: T): any;
|
|
57
|
+
/**
|
|
58
|
+
* 1234 => 1,234
|
|
59
|
+
* 1234b => 1,234b
|
|
60
|
+
* 1234.12b => 1,234.12b
|
|
61
|
+
* @param number 加千分位
|
|
62
|
+
* @returns
|
|
63
|
+
*/
|
|
64
|
+
declare function formatThousands(number: any): any;
|
|
65
|
+
type TimeType = Date | string | number;
|
|
66
|
+
/**
|
|
67
|
+
* 时间格式化函数
|
|
68
|
+
* @param {TimeType} time - 可选时间参数,可以是 Date 对象、时间戳字符串或数字
|
|
69
|
+
* @param {FormatType} cFormat - '{y}-{m}-{d} {h}:{i}:{s}' - 格式化字符串,支持 {y}年 {m}月 {d}日 {h}时 {i}分 {s}秒 {a}星期
|
|
70
|
+
* @returns {string} 格式化后的时间字符串
|
|
71
|
+
* @examples
|
|
72
|
+
* formatTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s} 星期{a}') // 示例输出: 2026-02-27 14:47:45 星期五 (实际结果取决于调用时的具体时间)
|
|
73
|
+
*/
|
|
74
|
+
declare function formatTime(time?: TimeType, cFormat?: string): string;
|
|
75
|
+
/**
|
|
76
|
+
*
|
|
77
|
+
* @param timestamp 持续的时间戳
|
|
78
|
+
* @param cFormat 格式化的规则 {d}天{h}时{i}分{s}秒
|
|
79
|
+
* @returns 天时分秒的字符串
|
|
80
|
+
* @example
|
|
81
|
+
* formatDurationTime(1162821) => 19分24秒
|
|
82
|
+
*/
|
|
83
|
+
declare function formatDurationTime(timestamp: any, cFormat?: string): string;
|
|
84
|
+
/** 获取assets静态资源
|
|
85
|
+
* @example
|
|
86
|
+
* proxy.formatImg('1.png')
|
|
87
|
+
* proxy.formatImg('1.png', 'menu')
|
|
88
|
+
* */
|
|
89
|
+
declare function formatImg(photoName: any, addPath?: string, { basePath }?: {
|
|
90
|
+
basePath?: string | undefined;
|
|
91
|
+
}): any;
|
|
92
|
+
/**
|
|
93
|
+
* 增加小数点
|
|
94
|
+
* formatToFixed(22) -> '22.00'
|
|
95
|
+
*
|
|
96
|
+
* formatToFixed('22') -> '22.00'
|
|
97
|
+
*
|
|
98
|
+
* formatToFixed('22', 4) -> '22.0000'
|
|
99
|
+
*
|
|
100
|
+
* formatToFixed('22', 2) -> 22
|
|
101
|
+
*
|
|
102
|
+
* formatToFixed('22 TB', {prefix: '$', suffix: '%', unit: false}) -> $22.00%
|
|
103
|
+
*/
|
|
104
|
+
declare function formatToFixed<const T extends {
|
|
105
|
+
digit?: number;
|
|
106
|
+
prefix?: string;
|
|
107
|
+
suffix?: string;
|
|
108
|
+
unit?: boolean;
|
|
109
|
+
thousands?: boolean;
|
|
110
|
+
} = {
|
|
111
|
+
digit: 2;
|
|
112
|
+
prefix: '';
|
|
113
|
+
suffix: '';
|
|
114
|
+
unit: true;
|
|
115
|
+
thousands: false;
|
|
116
|
+
}>(value: any, options?: T | number | any): string;
|
|
117
|
+
/**
|
|
118
|
+
* 格式化字符串中的换行符和制表符为HTML标签
|
|
119
|
+
* @param str 待格式化的字符串
|
|
120
|
+
* @returns 格式化后的字符串,如果输入的不是字符串或为空,则返回原字符串
|
|
121
|
+
* @example
|
|
122
|
+
$toast(
|
|
123
|
+
formatTextToHtml(
|
|
124
|
+
'Example file\n File : 111.jpeg\n CreateTime : 1721011155921 2024-07-15 10:39:15\n LastUpdateTime : 1721011155921 2024-07-15 10:39:15\n------------------------------------------------------------------------\nExtract:\n aa=231\n------------------------------------------------------------------------\n',
|
|
125
|
+
),
|
|
126
|
+
{
|
|
127
|
+
duration: 5000,
|
|
128
|
+
dangerouslyUseHTMLString: true,
|
|
129
|
+
},
|
|
130
|
+
)
|
|
131
|
+
*/
|
|
132
|
+
declare function formatTextToHtml(str: any): string;
|
|
133
|
+
|
|
134
|
+
export { formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatTextToHtml, formatThousands, formatTime, formatToFixed };
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 现有方法如下
|
|
3
|
+
* formatTime(time, cFormat = '{y}-{m}-{d} {h}:{i}:{s}')
|
|
4
|
+
* formatDurationTime(timestamp, cFormat = '{d} 天 {h} 时 {i} 分 {s} 秒')
|
|
5
|
+
* formatImg(photoName, addPath = '', { basePath = 'assets/images' } = {})
|
|
6
|
+
* formatThousands(number)
|
|
7
|
+
* formatBytes(bytes)
|
|
8
|
+
* formatBytesConvert(bytes)
|
|
9
|
+
* formatNewLines(str)
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* 格式化字节单位
|
|
13
|
+
* @param bytes - 字节数
|
|
14
|
+
* @param options - 配置项
|
|
15
|
+
* @param options.digit - 小数位数(默认2)
|
|
16
|
+
* @param options.thousands - 是否千分位分隔(默认true)
|
|
17
|
+
* @param options.prefix - 前缀(默认空)
|
|
18
|
+
* @param options.suffix - 后缀(默认空)
|
|
19
|
+
* @param options.roundType - 取整方式:'floor'(向下, 默认) | 'ceil'(向上) | 'round'(四舍五入)
|
|
20
|
+
* @example
|
|
21
|
+
* formatBytes(0.999) => 0.99B
|
|
22
|
+
* formatBytes(1040000, { digit: 3, prefix: "$", suffiex: "/s", roundType: "round", thousands: true }) => $1,015.625 KB
|
|
23
|
+
*/
|
|
24
|
+
declare function formatBytes<const T extends {
|
|
25
|
+
digit?: number;
|
|
26
|
+
prefix?: string;
|
|
27
|
+
suffix?: string;
|
|
28
|
+
roundType?: 'floor' | 'ceil' | 'round';
|
|
29
|
+
thousands?: boolean;
|
|
30
|
+
} = {
|
|
31
|
+
digit: 2;
|
|
32
|
+
prefix: '';
|
|
33
|
+
suffix: '';
|
|
34
|
+
roundType: 'floor';
|
|
35
|
+
thousands: false;
|
|
36
|
+
}>(bytes: number | string, options?: T): string;
|
|
37
|
+
/**
|
|
38
|
+
* 字节转数字
|
|
39
|
+
* @param oBytes
|
|
40
|
+
* @param param1
|
|
41
|
+
* @returns number
|
|
42
|
+
* formatBytesConvert('0.5GB') 536870912
|
|
43
|
+
*
|
|
44
|
+
* formatBytesConvert('1,234 GB') 1324997410816
|
|
45
|
+
*
|
|
46
|
+
* formatBytesConvert('1,234 GB', {thousands: true}) 1,324,997,410,816
|
|
47
|
+
*
|
|
48
|
+
* formatBytesConvert('1,234 GB', {digit: 2}) 1324997410816.00
|
|
49
|
+
*/
|
|
50
|
+
declare function formatBytesConvert<const T extends {
|
|
51
|
+
thounsands?: boolean;
|
|
52
|
+
digit?: number;
|
|
53
|
+
} = {
|
|
54
|
+
thounsands: false;
|
|
55
|
+
digit: 0;
|
|
56
|
+
}>(oBytes: any, options?: T): any;
|
|
57
|
+
/**
|
|
58
|
+
* 1234 => 1,234
|
|
59
|
+
* 1234b => 1,234b
|
|
60
|
+
* 1234.12b => 1,234.12b
|
|
61
|
+
* @param number 加千分位
|
|
62
|
+
* @returns
|
|
63
|
+
*/
|
|
64
|
+
declare function formatThousands(number: any): any;
|
|
65
|
+
type TimeType = Date | string | number;
|
|
66
|
+
/**
|
|
67
|
+
* 时间格式化函数
|
|
68
|
+
* @param {TimeType} time - 可选时间参数,可以是 Date 对象、时间戳字符串或数字
|
|
69
|
+
* @param {FormatType} cFormat - '{y}-{m}-{d} {h}:{i}:{s}' - 格式化字符串,支持 {y}年 {m}月 {d}日 {h}时 {i}分 {s}秒 {a}星期
|
|
70
|
+
* @returns {string} 格式化后的时间字符串
|
|
71
|
+
* @examples
|
|
72
|
+
* formatTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s} 星期{a}') // 示例输出: 2026-02-27 14:47:45 星期五 (实际结果取决于调用时的具体时间)
|
|
73
|
+
*/
|
|
74
|
+
declare function formatTime(time?: TimeType, cFormat?: string): string;
|
|
75
|
+
/**
|
|
76
|
+
*
|
|
77
|
+
* @param timestamp 持续的时间戳
|
|
78
|
+
* @param cFormat 格式化的规则 {d}天{h}时{i}分{s}秒
|
|
79
|
+
* @returns 天时分秒的字符串
|
|
80
|
+
* @example
|
|
81
|
+
* formatDurationTime(1162821) => 19分24秒
|
|
82
|
+
*/
|
|
83
|
+
declare function formatDurationTime(timestamp: any, cFormat?: string): string;
|
|
84
|
+
/** 获取assets静态资源
|
|
85
|
+
* @example
|
|
86
|
+
* proxy.formatImg('1.png')
|
|
87
|
+
* proxy.formatImg('1.png', 'menu')
|
|
88
|
+
* */
|
|
89
|
+
declare function formatImg(photoName: any, addPath?: string, { basePath }?: {
|
|
90
|
+
basePath?: string | undefined;
|
|
91
|
+
}): any;
|
|
92
|
+
/**
|
|
93
|
+
* 增加小数点
|
|
94
|
+
* formatToFixed(22) -> '22.00'
|
|
95
|
+
*
|
|
96
|
+
* formatToFixed('22') -> '22.00'
|
|
97
|
+
*
|
|
98
|
+
* formatToFixed('22', 4) -> '22.0000'
|
|
99
|
+
*
|
|
100
|
+
* formatToFixed('22', 2) -> 22
|
|
101
|
+
*
|
|
102
|
+
* formatToFixed('22 TB', {prefix: '$', suffix: '%', unit: false}) -> $22.00%
|
|
103
|
+
*/
|
|
104
|
+
declare function formatToFixed<const T extends {
|
|
105
|
+
digit?: number;
|
|
106
|
+
prefix?: string;
|
|
107
|
+
suffix?: string;
|
|
108
|
+
unit?: boolean;
|
|
109
|
+
thousands?: boolean;
|
|
110
|
+
} = {
|
|
111
|
+
digit: 2;
|
|
112
|
+
prefix: '';
|
|
113
|
+
suffix: '';
|
|
114
|
+
unit: true;
|
|
115
|
+
thousands: false;
|
|
116
|
+
}>(value: any, options?: T | number | any): string;
|
|
117
|
+
/**
|
|
118
|
+
* 格式化字符串中的换行符和制表符为HTML标签
|
|
119
|
+
* @param str 待格式化的字符串
|
|
120
|
+
* @returns 格式化后的字符串,如果输入的不是字符串或为空,则返回原字符串
|
|
121
|
+
* @example
|
|
122
|
+
$toast(
|
|
123
|
+
formatTextToHtml(
|
|
124
|
+
'Example file\n File : 111.jpeg\n CreateTime : 1721011155921 2024-07-15 10:39:15\n LastUpdateTime : 1721011155921 2024-07-15 10:39:15\n------------------------------------------------------------------------\nExtract:\n aa=231\n------------------------------------------------------------------------\n',
|
|
125
|
+
),
|
|
126
|
+
{
|
|
127
|
+
duration: 5000,
|
|
128
|
+
dangerouslyUseHTMLString: true,
|
|
129
|
+
},
|
|
130
|
+
)
|
|
131
|
+
*/
|
|
132
|
+
declare function formatTextToHtml(str: any): string;
|
|
133
|
+
|
|
134
|
+
export { formatBytes, formatBytesConvert, formatDurationTime, formatImg, formatTextToHtml, formatThousands, formatTime, formatToFixed };
|