react-util-tools 1.0.0
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/README.md +254 -0
- package/dist/index.cjs +943 -0
- package/dist/index.d.cts +154 -0
- package/dist/index.d.ts +154 -0
- package/dist/index.js +852 -0
- package/package.json +50 -0
- package/src/address/README.md +196 -0
- package/src/address/index.ts +41 -0
- package/src/date/README.md +539 -0
- package/src/date/index.ts +330 -0
- package/src/date/utc/README.md +779 -0
- package/src/date/utc/index.ts +374 -0
- package/src/decimal/README.md +425 -0
- package/src/decimal/index.ts +9 -0
- package/src/decimal/utils/README.md +474 -0
- package/src/decimal/utils/index.ts +244 -0
- package/src/device/README.md +441 -0
- package/src/device/index.ts +214 -0
- package/src/format/README.md +335 -0
- package/src/format/index.ts +189 -0
- package/src/index.ts +107 -0
- package/src/throttle/README.md +152 -0
- package/src/throttle/index.ts +83 -0
- package/tsconfig.json +28 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,852 @@
|
|
|
1
|
+
// src/throttle/index.ts
|
|
2
|
+
function throttleFn(fn, wait = 3e3, options = {}) {
|
|
3
|
+
let timeout = null;
|
|
4
|
+
let lastCallTime = null;
|
|
5
|
+
let lastArgs = null;
|
|
6
|
+
const { leading = true, trailing = true } = options;
|
|
7
|
+
const invoke = (time) => {
|
|
8
|
+
fn(...lastArgs);
|
|
9
|
+
lastCallTime = time;
|
|
10
|
+
lastArgs = null;
|
|
11
|
+
};
|
|
12
|
+
return function(...args) {
|
|
13
|
+
const now = Date.now();
|
|
14
|
+
if (lastCallTime === null && !leading) {
|
|
15
|
+
lastCallTime = now;
|
|
16
|
+
}
|
|
17
|
+
const remaining = wait - (now - (lastCallTime ?? 0));
|
|
18
|
+
lastArgs = args;
|
|
19
|
+
if (remaining <= 0) {
|
|
20
|
+
if (timeout) {
|
|
21
|
+
clearTimeout(timeout);
|
|
22
|
+
timeout = null;
|
|
23
|
+
}
|
|
24
|
+
invoke(now);
|
|
25
|
+
} else if (!timeout && trailing) {
|
|
26
|
+
timeout = setTimeout(() => {
|
|
27
|
+
timeout = null;
|
|
28
|
+
if (trailing && lastArgs) {
|
|
29
|
+
invoke(Date.now());
|
|
30
|
+
}
|
|
31
|
+
}, remaining);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function debounceFn(fn, wait = 500, immediate = false) {
|
|
36
|
+
let timeout = null;
|
|
37
|
+
return function(...args) {
|
|
38
|
+
const callNow = immediate && !timeout;
|
|
39
|
+
if (timeout) {
|
|
40
|
+
clearTimeout(timeout);
|
|
41
|
+
}
|
|
42
|
+
timeout = setTimeout(() => {
|
|
43
|
+
timeout = null;
|
|
44
|
+
if (!immediate) {
|
|
45
|
+
fn(...args);
|
|
46
|
+
}
|
|
47
|
+
}, wait);
|
|
48
|
+
if (callNow) {
|
|
49
|
+
fn(...args);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/address/index.ts
|
|
55
|
+
function getQueryParam(key, url) {
|
|
56
|
+
const searchParams = new URLSearchParams(
|
|
57
|
+
url ? new URL(url).search : window.location.search
|
|
58
|
+
);
|
|
59
|
+
return searchParams.get(key);
|
|
60
|
+
}
|
|
61
|
+
function getAllQueryParams(url) {
|
|
62
|
+
const searchParams = new URLSearchParams(
|
|
63
|
+
url ? new URL(url).search : window.location.search
|
|
64
|
+
);
|
|
65
|
+
const params = {};
|
|
66
|
+
searchParams.forEach((value, key) => {
|
|
67
|
+
params[key] = value;
|
|
68
|
+
});
|
|
69
|
+
return params;
|
|
70
|
+
}
|
|
71
|
+
function getQueryParamAll(key, url) {
|
|
72
|
+
const searchParams = new URLSearchParams(
|
|
73
|
+
url ? new URL(url).search : window.location.search
|
|
74
|
+
);
|
|
75
|
+
return searchParams.getAll(key);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/device/index.ts
|
|
79
|
+
function getUserAgent() {
|
|
80
|
+
return navigator.userAgent.toLowerCase();
|
|
81
|
+
}
|
|
82
|
+
function getOS() {
|
|
83
|
+
const ua = getUserAgent();
|
|
84
|
+
if (ua.includes("win")) return "Windows";
|
|
85
|
+
if (ua.includes("mac")) return "MacOS";
|
|
86
|
+
if (ua.includes("linux")) return "Linux";
|
|
87
|
+
if (ua.includes("android")) return "Android";
|
|
88
|
+
if (ua.includes("iphone") || ua.includes("ipad") || ua.includes("ipod")) return "iOS";
|
|
89
|
+
return "Unknown";
|
|
90
|
+
}
|
|
91
|
+
function getBrowser() {
|
|
92
|
+
const ua = getUserAgent();
|
|
93
|
+
if (ua.includes("edg")) return "Edge";
|
|
94
|
+
if (ua.includes("chrome")) return "Chrome";
|
|
95
|
+
if (ua.includes("safari") && !ua.includes("chrome")) return "Safari";
|
|
96
|
+
if (ua.includes("firefox")) return "Firefox";
|
|
97
|
+
if (ua.includes("opera") || ua.includes("opr")) return "Opera";
|
|
98
|
+
if (ua.includes("trident") || ua.includes("msie")) return "IE";
|
|
99
|
+
return "Unknown";
|
|
100
|
+
}
|
|
101
|
+
function getBrowserEngine() {
|
|
102
|
+
const ua = getUserAgent();
|
|
103
|
+
if (ua.includes("trident")) return "Trident";
|
|
104
|
+
if (ua.includes("gecko") && !ua.includes("webkit")) return "Gecko";
|
|
105
|
+
if (ua.includes("webkit")) return "WebKit";
|
|
106
|
+
if (ua.includes("presto")) return "Presto";
|
|
107
|
+
return "Unknown";
|
|
108
|
+
}
|
|
109
|
+
function isMobile() {
|
|
110
|
+
const ua = getUserAgent();
|
|
111
|
+
return /mobile|android|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(ua);
|
|
112
|
+
}
|
|
113
|
+
function isTablet() {
|
|
114
|
+
const ua = getUserAgent();
|
|
115
|
+
return /ipad|android(?!.*mobile)|tablet/i.test(ua);
|
|
116
|
+
}
|
|
117
|
+
function isDesktop() {
|
|
118
|
+
return !isMobile() && !isTablet();
|
|
119
|
+
}
|
|
120
|
+
function getDeviceType() {
|
|
121
|
+
if (isMobile()) return "mobile";
|
|
122
|
+
if (isTablet()) return "tablet";
|
|
123
|
+
return "desktop";
|
|
124
|
+
}
|
|
125
|
+
function isWeChat() {
|
|
126
|
+
const ua = getUserAgent();
|
|
127
|
+
return /micromessenger/i.test(ua);
|
|
128
|
+
}
|
|
129
|
+
function isIOS() {
|
|
130
|
+
const ua = getUserAgent();
|
|
131
|
+
return /iphone|ipad|ipod/i.test(ua);
|
|
132
|
+
}
|
|
133
|
+
function isAndroid() {
|
|
134
|
+
const ua = getUserAgent();
|
|
135
|
+
return /android/i.test(ua);
|
|
136
|
+
}
|
|
137
|
+
function getBrowserVersion() {
|
|
138
|
+
const ua = navigator.userAgent;
|
|
139
|
+
const browser = getBrowser();
|
|
140
|
+
let match = null;
|
|
141
|
+
switch (browser) {
|
|
142
|
+
case "Chrome":
|
|
143
|
+
match = ua.match(/chrome\/([\d.]+)/i);
|
|
144
|
+
break;
|
|
145
|
+
case "Safari":
|
|
146
|
+
match = ua.match(/version\/([\d.]+)/i);
|
|
147
|
+
break;
|
|
148
|
+
case "Firefox":
|
|
149
|
+
match = ua.match(/firefox\/([\d.]+)/i);
|
|
150
|
+
break;
|
|
151
|
+
case "Edge":
|
|
152
|
+
match = ua.match(/edg\/([\d.]+)/i);
|
|
153
|
+
break;
|
|
154
|
+
case "Opera":
|
|
155
|
+
match = ua.match(/(?:opera|opr)\/([\d.]+)/i);
|
|
156
|
+
break;
|
|
157
|
+
case "IE":
|
|
158
|
+
match = ua.match(/(?:msie |rv:)([\d.]+)/i);
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
return match ? match[1] : "Unknown";
|
|
162
|
+
}
|
|
163
|
+
function getDevicePixelRatio() {
|
|
164
|
+
return window.devicePixelRatio || 1;
|
|
165
|
+
}
|
|
166
|
+
function getScreenResolution() {
|
|
167
|
+
return {
|
|
168
|
+
width: window.screen.width,
|
|
169
|
+
height: window.screen.height
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function getViewportSize() {
|
|
173
|
+
return {
|
|
174
|
+
width: window.innerWidth || document.documentElement.clientWidth,
|
|
175
|
+
height: window.innerHeight || document.documentElement.clientHeight
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
function isTouchDevice() {
|
|
179
|
+
return "ontouchstart" in window || navigator.maxTouchPoints > 0;
|
|
180
|
+
}
|
|
181
|
+
function getDeviceInfo() {
|
|
182
|
+
return {
|
|
183
|
+
os: getOS(),
|
|
184
|
+
browser: getBrowser(),
|
|
185
|
+
browserVersion: getBrowserVersion(),
|
|
186
|
+
browserEngine: getBrowserEngine(),
|
|
187
|
+
deviceType: getDeviceType(),
|
|
188
|
+
isMobile: isMobile(),
|
|
189
|
+
isTablet: isTablet(),
|
|
190
|
+
isDesktop: isDesktop(),
|
|
191
|
+
isIOS: isIOS(),
|
|
192
|
+
isAndroid: isAndroid(),
|
|
193
|
+
isWeChat: isWeChat(),
|
|
194
|
+
isTouchDevice: isTouchDevice(),
|
|
195
|
+
devicePixelRatio: getDevicePixelRatio(),
|
|
196
|
+
screenResolution: getScreenResolution(),
|
|
197
|
+
viewportSize: getViewportSize(),
|
|
198
|
+
userAgent: navigator.userAgent
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// src/format/index.ts
|
|
203
|
+
function formatMoney(amount, options = {}) {
|
|
204
|
+
const {
|
|
205
|
+
decimals = 2,
|
|
206
|
+
symbol = "\xA5",
|
|
207
|
+
separator = ",",
|
|
208
|
+
decimalPoint = "."
|
|
209
|
+
} = options;
|
|
210
|
+
const num = typeof amount === "string" ? parseFloat(amount) : amount;
|
|
211
|
+
if (isNaN(num)) {
|
|
212
|
+
return `${symbol}0${decimalPoint}${"0".repeat(decimals)}`;
|
|
213
|
+
}
|
|
214
|
+
const isNegative = num < 0;
|
|
215
|
+
const absNum = Math.abs(num);
|
|
216
|
+
const fixed = absNum.toFixed(decimals);
|
|
217
|
+
const [integerPart, decimalPart] = fixed.split(".");
|
|
218
|
+
const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, separator);
|
|
219
|
+
let result = symbol + formattedInteger;
|
|
220
|
+
if (decimals > 0 && decimalPart) {
|
|
221
|
+
result += decimalPoint + decimalPart;
|
|
222
|
+
}
|
|
223
|
+
return isNegative ? `-${result}` : result;
|
|
224
|
+
}
|
|
225
|
+
function parseMoney(formattedAmount) {
|
|
226
|
+
if (!formattedAmount || typeof formattedAmount !== "string") {
|
|
227
|
+
return 0;
|
|
228
|
+
}
|
|
229
|
+
const cleaned = formattedAmount.replace(/[^\d.-]/g, "");
|
|
230
|
+
const num = parseFloat(cleaned);
|
|
231
|
+
return isNaN(num) ? 0 : num;
|
|
232
|
+
}
|
|
233
|
+
function formatNumber(amount, decimals = 2) {
|
|
234
|
+
const num = typeof amount === "string" ? parseFloat(amount) : amount;
|
|
235
|
+
if (isNaN(num)) {
|
|
236
|
+
return "0." + "0".repeat(decimals);
|
|
237
|
+
}
|
|
238
|
+
const fixed = num.toFixed(decimals);
|
|
239
|
+
const [integerPart, decimalPart] = fixed.split(".");
|
|
240
|
+
const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
241
|
+
return decimals > 0 && decimalPart ? `${formattedInteger}.${decimalPart}` : formattedInteger;
|
|
242
|
+
}
|
|
243
|
+
function formatMoneyToChinese(amount) {
|
|
244
|
+
const num = typeof amount === "string" ? parseFloat(amount) : amount;
|
|
245
|
+
if (isNaN(num) || num < 0) {
|
|
246
|
+
return "\u96F6\u5143\u6574";
|
|
247
|
+
}
|
|
248
|
+
const digits = ["\u96F6", "\u58F9", "\u8D30", "\u53C1", "\u8086", "\u4F0D", "\u9646", "\u67D2", "\u634C", "\u7396"];
|
|
249
|
+
const units = ["", "\u62FE", "\u4F70", "\u4EDF"];
|
|
250
|
+
const bigUnits = ["", "\u4E07", "\u4EBF", "\u5146"];
|
|
251
|
+
const decimalUnits = ["\u89D2", "\u5206"];
|
|
252
|
+
const [integerPart, decimalPart] = num.toFixed(2).split(".");
|
|
253
|
+
let result = "";
|
|
254
|
+
if (integerPart === "0") {
|
|
255
|
+
result = "\u96F6\u5143";
|
|
256
|
+
} else {
|
|
257
|
+
const integerStr = integerPart;
|
|
258
|
+
const len = integerStr.length;
|
|
259
|
+
let zeroCount = 0;
|
|
260
|
+
for (let i = 0; i < len; i++) {
|
|
261
|
+
const digit = parseInt(integerStr[i]);
|
|
262
|
+
const unitIndex = (len - i - 1) % 4;
|
|
263
|
+
const bigUnitIndex = Math.floor((len - i - 1) / 4);
|
|
264
|
+
if (digit === 0) {
|
|
265
|
+
zeroCount++;
|
|
266
|
+
} else {
|
|
267
|
+
if (zeroCount > 0) {
|
|
268
|
+
result += "\u96F6";
|
|
269
|
+
}
|
|
270
|
+
result += digits[digit] + units[unitIndex];
|
|
271
|
+
zeroCount = 0;
|
|
272
|
+
}
|
|
273
|
+
if (unitIndex === 0 && bigUnitIndex > 0) {
|
|
274
|
+
if (result[result.length - 1] !== bigUnits[bigUnitIndex]) {
|
|
275
|
+
result += bigUnits[bigUnitIndex];
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
result += "\u5143";
|
|
280
|
+
}
|
|
281
|
+
if (decimalPart && decimalPart !== "00") {
|
|
282
|
+
const jiao = parseInt(decimalPart[0]);
|
|
283
|
+
const fen = parseInt(decimalPart[1]);
|
|
284
|
+
if (jiao > 0) {
|
|
285
|
+
result += digits[jiao] + decimalUnits[0];
|
|
286
|
+
} else if (fen > 0) {
|
|
287
|
+
result += "\u96F6";
|
|
288
|
+
}
|
|
289
|
+
if (fen > 0) {
|
|
290
|
+
result += digits[fen] + decimalUnits[1];
|
|
291
|
+
}
|
|
292
|
+
} else {
|
|
293
|
+
result += "\u6574";
|
|
294
|
+
}
|
|
295
|
+
return result;
|
|
296
|
+
}
|
|
297
|
+
function formatPercent(value, options = {}) {
|
|
298
|
+
const { decimals = 2, multiply: multiply2 = true } = options;
|
|
299
|
+
if (isNaN(value)) {
|
|
300
|
+
return "0%";
|
|
301
|
+
}
|
|
302
|
+
const num = multiply2 ? value * 100 : value;
|
|
303
|
+
return num.toFixed(decimals) + "%";
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// src/date/index.ts
|
|
307
|
+
import {
|
|
308
|
+
format as dateFnsFormat,
|
|
309
|
+
parse,
|
|
310
|
+
parseISO,
|
|
311
|
+
isValid,
|
|
312
|
+
differenceInDays,
|
|
313
|
+
differenceInHours,
|
|
314
|
+
differenceInMinutes,
|
|
315
|
+
addDays,
|
|
316
|
+
addMonths,
|
|
317
|
+
subDays,
|
|
318
|
+
subMonths,
|
|
319
|
+
startOfDay,
|
|
320
|
+
endOfDay,
|
|
321
|
+
startOfWeek,
|
|
322
|
+
endOfWeek,
|
|
323
|
+
startOfMonth,
|
|
324
|
+
endOfMonth,
|
|
325
|
+
startOfYear,
|
|
326
|
+
endOfYear,
|
|
327
|
+
isBefore,
|
|
328
|
+
isAfter,
|
|
329
|
+
isSameDay,
|
|
330
|
+
formatDistanceToNow
|
|
331
|
+
} from "date-fns";
|
|
332
|
+
import { zhCN } from "date-fns/locale";
|
|
333
|
+
function formatDate(date, formatStr = "yyyy-MM-dd HH:mm:ss") {
|
|
334
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
335
|
+
return isValid(dateObj) ? dateFnsFormat(dateObj, formatStr) : "";
|
|
336
|
+
}
|
|
337
|
+
function formatDateOnly(date) {
|
|
338
|
+
return formatDate(date, "yyyy-MM-dd");
|
|
339
|
+
}
|
|
340
|
+
function formatTimeOnly(date) {
|
|
341
|
+
return formatDate(date, "HH:mm:ss");
|
|
342
|
+
}
|
|
343
|
+
function formatRelativeTime(date, locale = "zh") {
|
|
344
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
345
|
+
if (!isValid(dateObj)) return "";
|
|
346
|
+
return formatDistanceToNow(dateObj, {
|
|
347
|
+
addSuffix: true,
|
|
348
|
+
locale: locale === "zh" ? zhCN : void 0
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
function parseDate(dateStr, formatStr = "yyyy-MM-dd HH:mm:ss") {
|
|
352
|
+
return parse(dateStr, formatStr, /* @__PURE__ */ new Date());
|
|
353
|
+
}
|
|
354
|
+
function isValidDate(date) {
|
|
355
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
356
|
+
return isValid(dateObj);
|
|
357
|
+
}
|
|
358
|
+
function getDaysDiff(dateLeft, dateRight) {
|
|
359
|
+
const left = typeof dateLeft === "string" ? parseISO(dateLeft) : new Date(dateLeft);
|
|
360
|
+
const right = typeof dateRight === "string" ? parseISO(dateRight) : new Date(dateRight);
|
|
361
|
+
return differenceInDays(left, right);
|
|
362
|
+
}
|
|
363
|
+
function getHoursDiff(dateLeft, dateRight) {
|
|
364
|
+
const left = typeof dateLeft === "string" ? parseISO(dateLeft) : new Date(dateLeft);
|
|
365
|
+
const right = typeof dateRight === "string" ? parseISO(dateRight) : new Date(dateRight);
|
|
366
|
+
return differenceInHours(left, right);
|
|
367
|
+
}
|
|
368
|
+
function getMinutesDiff(dateLeft, dateRight) {
|
|
369
|
+
const left = typeof dateLeft === "string" ? parseISO(dateLeft) : new Date(dateLeft);
|
|
370
|
+
const right = typeof dateRight === "string" ? parseISO(dateRight) : new Date(dateRight);
|
|
371
|
+
return differenceInMinutes(left, right);
|
|
372
|
+
}
|
|
373
|
+
function addDaysToDate(date, amount) {
|
|
374
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
375
|
+
return addDays(dateObj, amount);
|
|
376
|
+
}
|
|
377
|
+
function subDaysFromDate(date, amount) {
|
|
378
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
379
|
+
return subDays(dateObj, amount);
|
|
380
|
+
}
|
|
381
|
+
function addMonthsToDate(date, amount) {
|
|
382
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
383
|
+
return addMonths(dateObj, amount);
|
|
384
|
+
}
|
|
385
|
+
function subMonthsFromDate(date, amount) {
|
|
386
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
387
|
+
return subMonths(dateObj, amount);
|
|
388
|
+
}
|
|
389
|
+
function getStartOfDay(date) {
|
|
390
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
391
|
+
return startOfDay(dateObj);
|
|
392
|
+
}
|
|
393
|
+
function getEndOfDay(date) {
|
|
394
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
395
|
+
return endOfDay(dateObj);
|
|
396
|
+
}
|
|
397
|
+
function getStartOfWeek(date) {
|
|
398
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
399
|
+
return startOfWeek(dateObj, { weekStartsOn: 1 });
|
|
400
|
+
}
|
|
401
|
+
function getEndOfWeek(date) {
|
|
402
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
403
|
+
return endOfWeek(dateObj, { weekStartsOn: 1 });
|
|
404
|
+
}
|
|
405
|
+
function getStartOfMonth(date) {
|
|
406
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
407
|
+
return startOfMonth(dateObj);
|
|
408
|
+
}
|
|
409
|
+
function getEndOfMonth(date) {
|
|
410
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
411
|
+
return endOfMonth(dateObj);
|
|
412
|
+
}
|
|
413
|
+
function getStartOfYear(date) {
|
|
414
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
415
|
+
return startOfYear(dateObj);
|
|
416
|
+
}
|
|
417
|
+
function getEndOfYear(date) {
|
|
418
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
419
|
+
return endOfYear(dateObj);
|
|
420
|
+
}
|
|
421
|
+
function isBeforeDate(date, dateToCompare) {
|
|
422
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
423
|
+
const compareObj = typeof dateToCompare === "string" ? parseISO(dateToCompare) : new Date(dateToCompare);
|
|
424
|
+
return isBefore(dateObj, compareObj);
|
|
425
|
+
}
|
|
426
|
+
function isAfterDate(date, dateToCompare) {
|
|
427
|
+
const dateObj = typeof date === "string" ? parseISO(date) : new Date(date);
|
|
428
|
+
const compareObj = typeof dateToCompare === "string" ? parseISO(dateToCompare) : new Date(dateToCompare);
|
|
429
|
+
return isAfter(dateObj, compareObj);
|
|
430
|
+
}
|
|
431
|
+
function isSameDayDate(dateLeft, dateRight) {
|
|
432
|
+
const left = typeof dateLeft === "string" ? parseISO(dateLeft) : new Date(dateLeft);
|
|
433
|
+
const right = typeof dateRight === "string" ? parseISO(dateRight) : new Date(dateRight);
|
|
434
|
+
return isSameDay(left, right);
|
|
435
|
+
}
|
|
436
|
+
function getTimestamp() {
|
|
437
|
+
return Date.now();
|
|
438
|
+
}
|
|
439
|
+
function getTimestampInSeconds() {
|
|
440
|
+
return Math.floor(Date.now() / 1e3);
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
// src/date/utc/index.ts
|
|
444
|
+
import {
|
|
445
|
+
parseISO as parseISO2,
|
|
446
|
+
isValid as isValid2,
|
|
447
|
+
addDays as addDays2,
|
|
448
|
+
addMonths as addMonths2,
|
|
449
|
+
subDays as subDays2,
|
|
450
|
+
subMonths as subMonths2,
|
|
451
|
+
startOfDay as startOfDay2,
|
|
452
|
+
endOfDay as endOfDay2,
|
|
453
|
+
startOfMonth as startOfMonth2,
|
|
454
|
+
endOfMonth as endOfMonth2,
|
|
455
|
+
startOfWeek as startOfWeek2,
|
|
456
|
+
endOfWeek as endOfWeek2,
|
|
457
|
+
differenceInDays as differenceInDays2,
|
|
458
|
+
differenceInHours as differenceInHours2,
|
|
459
|
+
differenceInMinutes as differenceInMinutes2,
|
|
460
|
+
getISOWeek,
|
|
461
|
+
getISOWeeksInYear,
|
|
462
|
+
setISOWeek
|
|
463
|
+
} from "date-fns";
|
|
464
|
+
import { formatInTimeZone, toZonedTime, fromZonedTime } from "date-fns-tz";
|
|
465
|
+
var UTC_TIMEZONE = "UTC";
|
|
466
|
+
function toUTC(date) {
|
|
467
|
+
const dateObj = typeof date === "string" ? parseISO2(date) : new Date(date);
|
|
468
|
+
return fromZonedTime(dateObj, Intl.DateTimeFormat().resolvedOptions().timeZone);
|
|
469
|
+
}
|
|
470
|
+
function fromUTC(utcDate) {
|
|
471
|
+
const dateObj = typeof utcDate === "string" ? parseISO2(utcDate) : new Date(utcDate);
|
|
472
|
+
return toZonedTime(dateObj, Intl.DateTimeFormat().resolvedOptions().timeZone);
|
|
473
|
+
}
|
|
474
|
+
function formatUTC(date, formatStr = "yyyy-MM-dd HH:mm:ss") {
|
|
475
|
+
const dateObj = typeof date === "string" ? parseISO2(date) : new Date(date);
|
|
476
|
+
if (!isValid2(dateObj)) return "";
|
|
477
|
+
return formatInTimeZone(dateObj, UTC_TIMEZONE, formatStr);
|
|
478
|
+
}
|
|
479
|
+
function formatUTCDateOnly(date) {
|
|
480
|
+
return formatUTC(date, "yyyy-MM-dd");
|
|
481
|
+
}
|
|
482
|
+
function formatUTCTimeOnly(date) {
|
|
483
|
+
return formatUTC(date, "HH:mm:ss");
|
|
484
|
+
}
|
|
485
|
+
function toISOString(date) {
|
|
486
|
+
const dateObj = typeof date === "string" ? parseISO2(date) : new Date(date);
|
|
487
|
+
return dateObj.toISOString();
|
|
488
|
+
}
|
|
489
|
+
function getUTCNow() {
|
|
490
|
+
return /* @__PURE__ */ new Date();
|
|
491
|
+
}
|
|
492
|
+
function getUTCTimestamp() {
|
|
493
|
+
return Date.now();
|
|
494
|
+
}
|
|
495
|
+
function getUTCTimestampInSeconds() {
|
|
496
|
+
return Math.floor(Date.now() / 1e3);
|
|
497
|
+
}
|
|
498
|
+
function getUTCStartOfDay(date) {
|
|
499
|
+
const dateObj = typeof date === "string" ? parseISO2(date) : new Date(date);
|
|
500
|
+
const utcDate = toZonedTime(dateObj, UTC_TIMEZONE);
|
|
501
|
+
return startOfDay2(utcDate);
|
|
502
|
+
}
|
|
503
|
+
function getUTCEndOfDay(date) {
|
|
504
|
+
const dateObj = typeof date === "string" ? parseISO2(date) : new Date(date);
|
|
505
|
+
const utcDate = toZonedTime(dateObj, UTC_TIMEZONE);
|
|
506
|
+
return endOfDay2(utcDate);
|
|
507
|
+
}
|
|
508
|
+
function getUTCStartOfMonth(date) {
|
|
509
|
+
const dateObj = typeof date === "string" ? parseISO2(date) : new Date(date);
|
|
510
|
+
const utcDate = toZonedTime(dateObj, UTC_TIMEZONE);
|
|
511
|
+
return startOfMonth2(utcDate);
|
|
512
|
+
}
|
|
513
|
+
function getUTCEndOfMonth(date) {
|
|
514
|
+
const dateObj = typeof date === "string" ? parseISO2(date) : new Date(date);
|
|
515
|
+
const utcDate = toZonedTime(dateObj, UTC_TIMEZONE);
|
|
516
|
+
return endOfMonth2(utcDate);
|
|
517
|
+
}
|
|
518
|
+
function addDaysUTC(date, amount) {
|
|
519
|
+
const dateObj = typeof date === "string" ? parseISO2(date) : new Date(date);
|
|
520
|
+
return addDays2(dateObj, amount);
|
|
521
|
+
}
|
|
522
|
+
function subDaysUTC(date, amount) {
|
|
523
|
+
const dateObj = typeof date === "string" ? parseISO2(date) : new Date(date);
|
|
524
|
+
return subDays2(dateObj, amount);
|
|
525
|
+
}
|
|
526
|
+
function addMonthsUTC(date, amount) {
|
|
527
|
+
const dateObj = typeof date === "string" ? parseISO2(date) : new Date(date);
|
|
528
|
+
return addMonths2(dateObj, amount);
|
|
529
|
+
}
|
|
530
|
+
function subMonthsUTC(date, amount) {
|
|
531
|
+
const dateObj = typeof date === "string" ? parseISO2(date) : new Date(date);
|
|
532
|
+
return subMonths2(dateObj, amount);
|
|
533
|
+
}
|
|
534
|
+
function getUTCDaysDiff(dateLeft, dateRight) {
|
|
535
|
+
const left = typeof dateLeft === "string" ? parseISO2(dateLeft) : new Date(dateLeft);
|
|
536
|
+
const right = typeof dateRight === "string" ? parseISO2(dateRight) : new Date(dateRight);
|
|
537
|
+
return differenceInDays2(left, right);
|
|
538
|
+
}
|
|
539
|
+
function getUTCHoursDiff(dateLeft, dateRight) {
|
|
540
|
+
const left = typeof dateLeft === "string" ? parseISO2(dateLeft) : new Date(dateLeft);
|
|
541
|
+
const right = typeof dateRight === "string" ? parseISO2(dateRight) : new Date(dateRight);
|
|
542
|
+
return differenceInHours2(left, right);
|
|
543
|
+
}
|
|
544
|
+
function getUTCMinutesDiff(dateLeft, dateRight) {
|
|
545
|
+
const left = typeof dateLeft === "string" ? parseISO2(dateLeft) : new Date(dateLeft);
|
|
546
|
+
const right = typeof dateRight === "string" ? parseISO2(dateRight) : new Date(dateRight);
|
|
547
|
+
return differenceInMinutes2(left, right);
|
|
548
|
+
}
|
|
549
|
+
function getTimezoneOffset() {
|
|
550
|
+
return (/* @__PURE__ */ new Date()).getTimezoneOffset();
|
|
551
|
+
}
|
|
552
|
+
function getTimezoneOffsetHours() {
|
|
553
|
+
return -(/* @__PURE__ */ new Date()).getTimezoneOffset() / 60;
|
|
554
|
+
}
|
|
555
|
+
function getUTCYearStartTimestamp(year) {
|
|
556
|
+
const targetYear = year ?? (/* @__PURE__ */ new Date()).getUTCFullYear();
|
|
557
|
+
const date = new Date(Date.UTC(targetYear, 0, 1, 0, 0, 0, 0));
|
|
558
|
+
return date.getTime();
|
|
559
|
+
}
|
|
560
|
+
function getUTCYearEndTimestamp(year) {
|
|
561
|
+
const targetYear = year ?? (/* @__PURE__ */ new Date()).getUTCFullYear();
|
|
562
|
+
const date = new Date(Date.UTC(targetYear, 11, 31, 23, 59, 59, 999));
|
|
563
|
+
return date.getTime();
|
|
564
|
+
}
|
|
565
|
+
function getUTCYearStart(year) {
|
|
566
|
+
const targetYear = year ?? (/* @__PURE__ */ new Date()).getUTCFullYear();
|
|
567
|
+
return new Date(Date.UTC(targetYear, 0, 1, 0, 0, 0, 0));
|
|
568
|
+
}
|
|
569
|
+
function getUTCYearEnd(year) {
|
|
570
|
+
const targetYear = year ?? (/* @__PURE__ */ new Date()).getUTCFullYear();
|
|
571
|
+
return new Date(Date.UTC(targetYear, 11, 31, 23, 59, 59, 999));
|
|
572
|
+
}
|
|
573
|
+
function getUTCWeeksInYear(year) {
|
|
574
|
+
const targetYear = year ?? (/* @__PURE__ */ new Date()).getUTCFullYear();
|
|
575
|
+
const date = new Date(Date.UTC(targetYear, 0, 1));
|
|
576
|
+
return getISOWeeksInYear(date);
|
|
577
|
+
}
|
|
578
|
+
function getUTCWeekStart(year, week) {
|
|
579
|
+
const date = new Date(Date.UTC(year, 0, 4));
|
|
580
|
+
const weekDate = setISOWeek(date, week);
|
|
581
|
+
return startOfWeek2(weekDate, { weekStartsOn: 1 });
|
|
582
|
+
}
|
|
583
|
+
function getUTCWeekEnd(year, week) {
|
|
584
|
+
const date = new Date(Date.UTC(year, 0, 4));
|
|
585
|
+
const weekDate = setISOWeek(date, week);
|
|
586
|
+
return endOfWeek2(weekDate, { weekStartsOn: 1 });
|
|
587
|
+
}
|
|
588
|
+
function getUTCAllWeeksInYear(year) {
|
|
589
|
+
const targetYear = year ?? (/* @__PURE__ */ new Date()).getUTCFullYear();
|
|
590
|
+
const weeksCount = getUTCWeeksInYear(targetYear);
|
|
591
|
+
const weeks = [];
|
|
592
|
+
for (let week = 1; week <= weeksCount; week++) {
|
|
593
|
+
const start = getUTCWeekStart(targetYear, week);
|
|
594
|
+
const end = getUTCWeekEnd(targetYear, week);
|
|
595
|
+
weeks.push({
|
|
596
|
+
week,
|
|
597
|
+
start,
|
|
598
|
+
end,
|
|
599
|
+
startTimestamp: start.getTime(),
|
|
600
|
+
endTimestamp: end.getTime()
|
|
601
|
+
});
|
|
602
|
+
}
|
|
603
|
+
return weeks;
|
|
604
|
+
}
|
|
605
|
+
function getUTCWeekNumber(date) {
|
|
606
|
+
const dateObj = typeof date === "string" ? parseISO2(date) : new Date(date);
|
|
607
|
+
return getISOWeek(dateObj);
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// src/decimal/index.ts
|
|
611
|
+
import { default as default2 } from "decimal.js";
|
|
612
|
+
|
|
613
|
+
// src/decimal/utils/index.ts
|
|
614
|
+
import Decimal from "decimal.js";
|
|
615
|
+
function safeDecimal(value) {
|
|
616
|
+
try {
|
|
617
|
+
if (value instanceof Decimal) {
|
|
618
|
+
return value;
|
|
619
|
+
}
|
|
620
|
+
const decimal = new Decimal(value);
|
|
621
|
+
if (decimal.isNaN()) {
|
|
622
|
+
return new Decimal(0);
|
|
623
|
+
}
|
|
624
|
+
return decimal;
|
|
625
|
+
} catch {
|
|
626
|
+
return new Decimal(0);
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
function add(a, b) {
|
|
630
|
+
try {
|
|
631
|
+
const decimalA = safeDecimal(a);
|
|
632
|
+
const decimalB = safeDecimal(b);
|
|
633
|
+
return decimalA.plus(decimalB).toNumber();
|
|
634
|
+
} catch {
|
|
635
|
+
return 0;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
function subtract(a, b) {
|
|
639
|
+
try {
|
|
640
|
+
const decimalA = safeDecimal(a);
|
|
641
|
+
const decimalB = safeDecimal(b);
|
|
642
|
+
return decimalA.minus(decimalB).toNumber();
|
|
643
|
+
} catch {
|
|
644
|
+
return 0;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
function multiply(a, b) {
|
|
648
|
+
try {
|
|
649
|
+
const decimalA = safeDecimal(a);
|
|
650
|
+
const decimalB = safeDecimal(b);
|
|
651
|
+
return decimalA.times(decimalB).toNumber();
|
|
652
|
+
} catch {
|
|
653
|
+
return 0;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
function divide(a, b) {
|
|
657
|
+
try {
|
|
658
|
+
const decimalA = safeDecimal(a);
|
|
659
|
+
const decimalB = safeDecimal(b);
|
|
660
|
+
if (decimalB.isZero()) {
|
|
661
|
+
return 0;
|
|
662
|
+
}
|
|
663
|
+
return decimalA.dividedBy(decimalB).toNumber();
|
|
664
|
+
} catch {
|
|
665
|
+
return 0;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
function equals(a, b) {
|
|
669
|
+
try {
|
|
670
|
+
const decimalA = safeDecimal(a);
|
|
671
|
+
const decimalB = safeDecimal(b);
|
|
672
|
+
return decimalA.equals(decimalB);
|
|
673
|
+
} catch {
|
|
674
|
+
return false;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
function greaterThan(a, b) {
|
|
678
|
+
try {
|
|
679
|
+
const decimalA = safeDecimal(a);
|
|
680
|
+
const decimalB = safeDecimal(b);
|
|
681
|
+
return decimalA.greaterThan(decimalB);
|
|
682
|
+
} catch {
|
|
683
|
+
return false;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
function lessThan(a, b) {
|
|
687
|
+
try {
|
|
688
|
+
const decimalA = safeDecimal(a);
|
|
689
|
+
const decimalB = safeDecimal(b);
|
|
690
|
+
return decimalA.lessThan(decimalB);
|
|
691
|
+
} catch {
|
|
692
|
+
return false;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
function greaterThanOrEqual(a, b) {
|
|
696
|
+
try {
|
|
697
|
+
const decimalA = safeDecimal(a);
|
|
698
|
+
const decimalB = safeDecimal(b);
|
|
699
|
+
return decimalA.greaterThanOrEqualTo(decimalB);
|
|
700
|
+
} catch {
|
|
701
|
+
return false;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
function lessThanOrEqual(a, b) {
|
|
705
|
+
try {
|
|
706
|
+
const decimalA = safeDecimal(a);
|
|
707
|
+
const decimalB = safeDecimal(b);
|
|
708
|
+
return decimalA.lessThanOrEqualTo(decimalB);
|
|
709
|
+
} catch {
|
|
710
|
+
return false;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
function round(value, decimalPlaces = 2) {
|
|
714
|
+
try {
|
|
715
|
+
const decimal = safeDecimal(value);
|
|
716
|
+
return decimal.toDecimalPlaces(decimalPlaces).toNumber();
|
|
717
|
+
} catch {
|
|
718
|
+
return 0;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
function ceil(value, decimalPlaces = 2) {
|
|
722
|
+
try {
|
|
723
|
+
const decimal = safeDecimal(value);
|
|
724
|
+
return decimal.toDecimalPlaces(decimalPlaces, Decimal.ROUND_CEIL).toNumber();
|
|
725
|
+
} catch {
|
|
726
|
+
return 0;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
function floor(value, decimalPlaces = 2) {
|
|
730
|
+
try {
|
|
731
|
+
const decimal = safeDecimal(value);
|
|
732
|
+
return decimal.toDecimalPlaces(decimalPlaces, Decimal.ROUND_FLOOR).toNumber();
|
|
733
|
+
} catch {
|
|
734
|
+
return 0;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
function abs(value) {
|
|
738
|
+
try {
|
|
739
|
+
const decimal = safeDecimal(value);
|
|
740
|
+
return decimal.abs().toNumber();
|
|
741
|
+
} catch {
|
|
742
|
+
return 0;
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
function negate(value) {
|
|
746
|
+
try {
|
|
747
|
+
const decimal = safeDecimal(value);
|
|
748
|
+
return decimal.negated().toNumber();
|
|
749
|
+
} catch {
|
|
750
|
+
return 0;
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
export {
|
|
754
|
+
default2 as Decimal,
|
|
755
|
+
abs,
|
|
756
|
+
add,
|
|
757
|
+
addDaysToDate,
|
|
758
|
+
addDaysUTC,
|
|
759
|
+
addMonthsToDate,
|
|
760
|
+
addMonthsUTC,
|
|
761
|
+
ceil,
|
|
762
|
+
debounceFn as debounce,
|
|
763
|
+
divide,
|
|
764
|
+
equals,
|
|
765
|
+
floor,
|
|
766
|
+
formatDate,
|
|
767
|
+
formatDateOnly,
|
|
768
|
+
formatMoney,
|
|
769
|
+
formatMoneyToChinese,
|
|
770
|
+
formatNumber,
|
|
771
|
+
formatPercent,
|
|
772
|
+
formatRelativeTime,
|
|
773
|
+
formatTimeOnly,
|
|
774
|
+
formatUTC,
|
|
775
|
+
formatUTCDateOnly,
|
|
776
|
+
formatUTCTimeOnly,
|
|
777
|
+
fromUTC,
|
|
778
|
+
getAllQueryParams,
|
|
779
|
+
getBrowser,
|
|
780
|
+
getBrowserEngine,
|
|
781
|
+
getBrowserVersion,
|
|
782
|
+
getDaysDiff,
|
|
783
|
+
getDeviceInfo,
|
|
784
|
+
getDevicePixelRatio,
|
|
785
|
+
getDeviceType,
|
|
786
|
+
getEndOfDay,
|
|
787
|
+
getEndOfMonth,
|
|
788
|
+
getEndOfWeek,
|
|
789
|
+
getEndOfYear,
|
|
790
|
+
getHoursDiff,
|
|
791
|
+
getMinutesDiff,
|
|
792
|
+
getOS,
|
|
793
|
+
getQueryParam,
|
|
794
|
+
getQueryParamAll,
|
|
795
|
+
getScreenResolution,
|
|
796
|
+
getStartOfDay,
|
|
797
|
+
getStartOfMonth,
|
|
798
|
+
getStartOfWeek,
|
|
799
|
+
getStartOfYear,
|
|
800
|
+
getTimestamp,
|
|
801
|
+
getTimestampInSeconds,
|
|
802
|
+
getTimezoneOffset,
|
|
803
|
+
getTimezoneOffsetHours,
|
|
804
|
+
getUTCAllWeeksInYear,
|
|
805
|
+
getUTCDaysDiff,
|
|
806
|
+
getUTCEndOfDay,
|
|
807
|
+
getUTCEndOfMonth,
|
|
808
|
+
getUTCHoursDiff,
|
|
809
|
+
getUTCMinutesDiff,
|
|
810
|
+
getUTCNow,
|
|
811
|
+
getUTCStartOfDay,
|
|
812
|
+
getUTCStartOfMonth,
|
|
813
|
+
getUTCTimestamp,
|
|
814
|
+
getUTCTimestampInSeconds,
|
|
815
|
+
getUTCWeekEnd,
|
|
816
|
+
getUTCWeekNumber,
|
|
817
|
+
getUTCWeekStart,
|
|
818
|
+
getUTCWeeksInYear,
|
|
819
|
+
getUTCYearEnd,
|
|
820
|
+
getUTCYearEndTimestamp,
|
|
821
|
+
getUTCYearStart,
|
|
822
|
+
getUTCYearStartTimestamp,
|
|
823
|
+
getViewportSize,
|
|
824
|
+
greaterThan,
|
|
825
|
+
greaterThanOrEqual,
|
|
826
|
+
isAfterDate,
|
|
827
|
+
isAndroid,
|
|
828
|
+
isBeforeDate,
|
|
829
|
+
isDesktop,
|
|
830
|
+
isIOS,
|
|
831
|
+
isMobile,
|
|
832
|
+
isSameDayDate,
|
|
833
|
+
isTablet,
|
|
834
|
+
isTouchDevice,
|
|
835
|
+
isValidDate,
|
|
836
|
+
isWeChat,
|
|
837
|
+
lessThan,
|
|
838
|
+
lessThanOrEqual,
|
|
839
|
+
multiply,
|
|
840
|
+
negate,
|
|
841
|
+
parseDate,
|
|
842
|
+
parseMoney,
|
|
843
|
+
round,
|
|
844
|
+
subDaysFromDate,
|
|
845
|
+
subDaysUTC,
|
|
846
|
+
subMonthsFromDate,
|
|
847
|
+
subMonthsUTC,
|
|
848
|
+
subtract,
|
|
849
|
+
throttleFn as throttle,
|
|
850
|
+
toISOString,
|
|
851
|
+
toUTC
|
|
852
|
+
};
|