react-util-tools 1.0.21 → 1.0.24
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/index.cjs +111 -2
- package/dist/index.d.cts +17 -1
- package/dist/index.d.ts +17 -1
- package/dist/index.js +102 -1
- package/package.json +6 -6
- package/src/index.ts +12 -1
package/dist/index.cjs
CHANGED
|
@@ -38,6 +38,7 @@ __export(index_exports, {
|
|
|
38
38
|
addMonthsToDate: () => addMonthsToDate,
|
|
39
39
|
addMonthsUTC: () => addMonthsUTC,
|
|
40
40
|
ceil: () => ceil,
|
|
41
|
+
clearAllCookies: () => clearAllCookies,
|
|
41
42
|
debounce: () => debounceFn,
|
|
42
43
|
divide: () => divide,
|
|
43
44
|
equals: () => equals,
|
|
@@ -54,10 +55,12 @@ __export(index_exports, {
|
|
|
54
55
|
formatUTCDateOnly: () => formatUTCDateOnly,
|
|
55
56
|
formatUTCTimeOnly: () => formatUTCTimeOnly,
|
|
56
57
|
fromUTC: () => fromUTC,
|
|
58
|
+
getAllCookies: () => getAllCookies,
|
|
57
59
|
getAllQueryParams: () => getAllQueryParams,
|
|
58
60
|
getBrowser: () => getBrowser,
|
|
59
61
|
getBrowserEngine: () => getBrowserEngine,
|
|
60
62
|
getBrowserVersion: () => getBrowserVersion,
|
|
63
|
+
getCookie: () => getCookie,
|
|
61
64
|
getDaysDiff: () => getDaysDiff,
|
|
62
65
|
getDeviceInfo: () => getDeviceInfo,
|
|
63
66
|
getDevicePixelRatio: () => getDevicePixelRatio,
|
|
@@ -102,6 +105,7 @@ __export(index_exports, {
|
|
|
102
105
|
getViewportSize: () => getViewportSize,
|
|
103
106
|
greaterThan: () => greaterThan,
|
|
104
107
|
greaterThanOrEqual: () => greaterThanOrEqual,
|
|
108
|
+
hasCookie: () => hasCookie,
|
|
105
109
|
isAfterDate: () => isAfterDate,
|
|
106
110
|
isAndroid: () => isAndroid,
|
|
107
111
|
isBeforeDate: () => isBeforeDate,
|
|
@@ -115,11 +119,14 @@ __export(index_exports, {
|
|
|
115
119
|
isWeChat: () => isWeChat,
|
|
116
120
|
lessThan: () => lessThan,
|
|
117
121
|
lessThanOrEqual: () => lessThanOrEqual,
|
|
122
|
+
maskEmail: () => maskEmail,
|
|
118
123
|
multiply: () => multiply,
|
|
119
124
|
negate: () => negate,
|
|
120
125
|
parseDate: () => parseDate,
|
|
121
126
|
parseMoney: () => parseMoney,
|
|
127
|
+
removeCookie: () => removeCookie,
|
|
122
128
|
round: () => round,
|
|
129
|
+
setCookie: () => setCookie,
|
|
123
130
|
subDaysFromDate: () => subDaysFromDate,
|
|
124
131
|
subDaysUTC: () => subDaysUTC,
|
|
125
132
|
subMonthsFromDate: () => subMonthsFromDate,
|
|
@@ -127,7 +134,8 @@ __export(index_exports, {
|
|
|
127
134
|
subtract: () => subtract,
|
|
128
135
|
throttle: () => throttleFn,
|
|
129
136
|
toISOString: () => toISOString,
|
|
130
|
-
toUTC: () => toUTC
|
|
137
|
+
toUTC: () => toUTC,
|
|
138
|
+
unmaskEmail: () => unmaskEmail
|
|
131
139
|
});
|
|
132
140
|
module.exports = __toCommonJS(index_exports);
|
|
133
141
|
|
|
@@ -208,6 +216,66 @@ function getQueryParamAll(key, url) {
|
|
|
208
216
|
return searchParams.getAll(key);
|
|
209
217
|
}
|
|
210
218
|
|
|
219
|
+
// src/cookie/index.ts
|
|
220
|
+
function setCookie(name, value, options = {}) {
|
|
221
|
+
let cookieString = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
222
|
+
if (options.expires) {
|
|
223
|
+
const expires = options.expires instanceof Date ? options.expires : new Date(Date.now() + options.expires * 1e3);
|
|
224
|
+
cookieString += `; expires=${expires.toUTCString()}`;
|
|
225
|
+
}
|
|
226
|
+
if (options.path) {
|
|
227
|
+
cookieString += `; path=${options.path}`;
|
|
228
|
+
}
|
|
229
|
+
if (options.domain) {
|
|
230
|
+
cookieString += `; domain=${options.domain}`;
|
|
231
|
+
}
|
|
232
|
+
if (options.secure) {
|
|
233
|
+
cookieString += "; secure";
|
|
234
|
+
}
|
|
235
|
+
if (options.sameSite) {
|
|
236
|
+
cookieString += `; SameSite=${options.sameSite}`;
|
|
237
|
+
}
|
|
238
|
+
document.cookie = cookieString;
|
|
239
|
+
}
|
|
240
|
+
function getCookie(name) {
|
|
241
|
+
const nameEQ = encodeURIComponent(name) + "=";
|
|
242
|
+
const cookies = document.cookie.split(";");
|
|
243
|
+
for (let cookie of cookies) {
|
|
244
|
+
cookie = cookie.trim();
|
|
245
|
+
if (cookie.indexOf(nameEQ) === 0) {
|
|
246
|
+
return decodeURIComponent(cookie.substring(nameEQ.length));
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
function removeCookie(name, options = {}) {
|
|
252
|
+
setCookie(name, "", {
|
|
253
|
+
...options,
|
|
254
|
+
expires: /* @__PURE__ */ new Date(0)
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
function hasCookie(name) {
|
|
258
|
+
return getCookie(name) !== null;
|
|
259
|
+
}
|
|
260
|
+
function getAllCookies() {
|
|
261
|
+
const cookies = {};
|
|
262
|
+
const cookieArray = document.cookie.split(";");
|
|
263
|
+
for (let cookie of cookieArray) {
|
|
264
|
+
cookie = cookie.trim();
|
|
265
|
+
const [name, ...valueParts] = cookie.split("=");
|
|
266
|
+
if (name) {
|
|
267
|
+
cookies[decodeURIComponent(name)] = decodeURIComponent(valueParts.join("="));
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return cookies;
|
|
271
|
+
}
|
|
272
|
+
function clearAllCookies(options = {}) {
|
|
273
|
+
const cookies = getAllCookies();
|
|
274
|
+
for (const name in cookies) {
|
|
275
|
+
removeCookie(name, options);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
211
279
|
// src/device/index.ts
|
|
212
280
|
function getUserAgent() {
|
|
213
281
|
return navigator.userAgent.toLowerCase();
|
|
@@ -435,6 +503,39 @@ function formatPercent(value, options = {}) {
|
|
|
435
503
|
const num = multiply2 ? value * 100 : value;
|
|
436
504
|
return num.toFixed(decimals) + "%";
|
|
437
505
|
}
|
|
506
|
+
function maskEmail(email) {
|
|
507
|
+
if (!email || typeof email !== "string") {
|
|
508
|
+
return "";
|
|
509
|
+
}
|
|
510
|
+
const atIndex = email.indexOf("@");
|
|
511
|
+
if (atIndex <= 0) {
|
|
512
|
+
return email;
|
|
513
|
+
}
|
|
514
|
+
const localPart = email.substring(0, atIndex);
|
|
515
|
+
const domainPart = email.substring(atIndex);
|
|
516
|
+
const visiblePart = localPart.substring(0, 3);
|
|
517
|
+
return `${visiblePart}***${domainPart}`;
|
|
518
|
+
}
|
|
519
|
+
function unmaskEmail(maskedEmail, originalEmail) {
|
|
520
|
+
if (!maskedEmail || !originalEmail) {
|
|
521
|
+
return "";
|
|
522
|
+
}
|
|
523
|
+
if (!maskedEmail.includes("***@")) {
|
|
524
|
+
return maskedEmail;
|
|
525
|
+
}
|
|
526
|
+
const maskedParts = maskedEmail.split("***@");
|
|
527
|
+
const atIndex = originalEmail.indexOf("@");
|
|
528
|
+
if (atIndex <= 0) {
|
|
529
|
+
return maskedEmail;
|
|
530
|
+
}
|
|
531
|
+
const originalPrefix = originalEmail.substring(0, 3);
|
|
532
|
+
const originalDomain = originalEmail.substring(atIndex + 1);
|
|
533
|
+
const maskedDomain = maskedParts[1];
|
|
534
|
+
if (maskedParts[0] === originalPrefix && maskedDomain === originalDomain) {
|
|
535
|
+
return originalEmail;
|
|
536
|
+
}
|
|
537
|
+
return maskedEmail;
|
|
538
|
+
}
|
|
438
539
|
|
|
439
540
|
// src/date/index.ts
|
|
440
541
|
var import_date_fns = require("date-fns");
|
|
@@ -850,6 +951,7 @@ function negate(value) {
|
|
|
850
951
|
addMonthsToDate,
|
|
851
952
|
addMonthsUTC,
|
|
852
953
|
ceil,
|
|
954
|
+
clearAllCookies,
|
|
853
955
|
debounce,
|
|
854
956
|
divide,
|
|
855
957
|
equals,
|
|
@@ -866,10 +968,12 @@ function negate(value) {
|
|
|
866
968
|
formatUTCDateOnly,
|
|
867
969
|
formatUTCTimeOnly,
|
|
868
970
|
fromUTC,
|
|
971
|
+
getAllCookies,
|
|
869
972
|
getAllQueryParams,
|
|
870
973
|
getBrowser,
|
|
871
974
|
getBrowserEngine,
|
|
872
975
|
getBrowserVersion,
|
|
976
|
+
getCookie,
|
|
873
977
|
getDaysDiff,
|
|
874
978
|
getDeviceInfo,
|
|
875
979
|
getDevicePixelRatio,
|
|
@@ -914,6 +1018,7 @@ function negate(value) {
|
|
|
914
1018
|
getViewportSize,
|
|
915
1019
|
greaterThan,
|
|
916
1020
|
greaterThanOrEqual,
|
|
1021
|
+
hasCookie,
|
|
917
1022
|
isAfterDate,
|
|
918
1023
|
isAndroid,
|
|
919
1024
|
isBeforeDate,
|
|
@@ -927,11 +1032,14 @@ function negate(value) {
|
|
|
927
1032
|
isWeChat,
|
|
928
1033
|
lessThan,
|
|
929
1034
|
lessThanOrEqual,
|
|
1035
|
+
maskEmail,
|
|
930
1036
|
multiply,
|
|
931
1037
|
negate,
|
|
932
1038
|
parseDate,
|
|
933
1039
|
parseMoney,
|
|
1040
|
+
removeCookie,
|
|
934
1041
|
round,
|
|
1042
|
+
setCookie,
|
|
935
1043
|
subDaysFromDate,
|
|
936
1044
|
subDaysUTC,
|
|
937
1045
|
subMonthsFromDate,
|
|
@@ -939,5 +1047,6 @@ function negate(value) {
|
|
|
939
1047
|
subtract,
|
|
940
1048
|
throttle,
|
|
941
1049
|
toISOString,
|
|
942
|
-
toUTC
|
|
1050
|
+
toUTC,
|
|
1051
|
+
unmaskEmail
|
|
943
1052
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -11,6 +11,20 @@ declare function getQueryParam(key: string, url?: string): string | null;
|
|
|
11
11
|
declare function getAllQueryParams(url?: string): Record<string, string>;
|
|
12
12
|
declare function getQueryParamAll(key: string, url?: string): string[];
|
|
13
13
|
|
|
14
|
+
interface CookieOptions {
|
|
15
|
+
expires?: number | Date;
|
|
16
|
+
path?: string;
|
|
17
|
+
domain?: string;
|
|
18
|
+
secure?: boolean;
|
|
19
|
+
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
20
|
+
}
|
|
21
|
+
declare function setCookie(name: string, value: string, options?: CookieOptions): void;
|
|
22
|
+
declare function getCookie(name: string): string | null;
|
|
23
|
+
declare function removeCookie(name: string, options?: Pick<CookieOptions, 'path' | 'domain'>): void;
|
|
24
|
+
declare function hasCookie(name: string): boolean;
|
|
25
|
+
declare function getAllCookies(): Record<string, string>;
|
|
26
|
+
declare function clearAllCookies(options?: Pick<CookieOptions, 'path' | 'domain'>): void;
|
|
27
|
+
|
|
14
28
|
declare function getOS(): string;
|
|
15
29
|
declare function getBrowser(): string;
|
|
16
30
|
declare function getBrowserEngine(): string;
|
|
@@ -70,6 +84,8 @@ declare function formatPercent(value: number, options?: {
|
|
|
70
84
|
decimals?: number;
|
|
71
85
|
multiply?: boolean;
|
|
72
86
|
}): string;
|
|
87
|
+
declare function maskEmail(email: string): string;
|
|
88
|
+
declare function unmaskEmail(maskedEmail: string, originalEmail: string): string;
|
|
73
89
|
|
|
74
90
|
declare function formatDate(date: Date | number | string, formatStr?: string): string;
|
|
75
91
|
declare function formatDateOnly(date: Date | number | string): string;
|
|
@@ -151,4 +167,4 @@ declare function floor(value: number | string | Decimal, decimalPlaces?: number)
|
|
|
151
167
|
declare function abs(value: number | string | Decimal): number;
|
|
152
168
|
declare function negate(value: number | string | Decimal): number;
|
|
153
169
|
|
|
154
|
-
export { abs, add, addDaysToDate, addDaysUTC, addMonthsToDate, addMonthsUTC, ceil, debounceFn as debounce, divide, equals, floor, formatDate, formatDateOnly, formatMoney, formatMoneyToChinese, formatNumber, formatPercent, formatRelativeTime, formatTimeOnly, formatUTC, formatUTCDateOnly, formatUTCTimeOnly, fromUTC, getAllQueryParams, getBrowser, getBrowserEngine, getBrowserVersion, getDaysDiff, getDeviceInfo, getDevicePixelRatio, getDeviceType, getEndOfDay, getEndOfMonth, getEndOfWeek, getEndOfYear, getHoursDiff, getMinutesDiff, getOS, getQueryParam, getQueryParamAll, getScreenResolution, getStartOfDay, getStartOfMonth, getStartOfWeek, getStartOfYear, getTimestamp, getTimestampInSeconds, getTimezoneOffset, getTimezoneOffsetHours, getUTCAllWeeksInYear, getUTCDaysDiff, getUTCEndOfDay, getUTCEndOfMonth, getUTCHoursDiff, getUTCMinutesDiff, getUTCNow, getUTCStartOfDay, getUTCStartOfMonth, getUTCTimestamp, getUTCTimestampInSeconds, getUTCWeekEnd, getUTCWeekNumber, getUTCWeekStart, getUTCWeeksInYear, getUTCYearEnd, getUTCYearEndTimestamp, getUTCYearStart, getUTCYearStartTimestamp, getViewportSize, greaterThan, greaterThanOrEqual, isAfterDate, isAndroid, isBeforeDate, isDesktop, isIOS, isMobile, isSameDayDate, isTablet, isTouchDevice, isValidDate, isWeChat, lessThan, lessThanOrEqual, multiply, negate, parseDate, parseMoney, round, subDaysFromDate, subDaysUTC, subMonthsFromDate, subMonthsUTC, subtract, throttleFn as throttle, toISOString, toUTC };
|
|
170
|
+
export { type CookieOptions, abs, add, addDaysToDate, addDaysUTC, addMonthsToDate, addMonthsUTC, ceil, clearAllCookies, debounceFn as debounce, divide, equals, floor, formatDate, formatDateOnly, formatMoney, formatMoneyToChinese, formatNumber, formatPercent, formatRelativeTime, formatTimeOnly, formatUTC, formatUTCDateOnly, formatUTCTimeOnly, fromUTC, getAllCookies, getAllQueryParams, getBrowser, getBrowserEngine, getBrowserVersion, getCookie, getDaysDiff, getDeviceInfo, getDevicePixelRatio, getDeviceType, getEndOfDay, getEndOfMonth, getEndOfWeek, getEndOfYear, getHoursDiff, getMinutesDiff, getOS, getQueryParam, getQueryParamAll, getScreenResolution, getStartOfDay, getStartOfMonth, getStartOfWeek, getStartOfYear, getTimestamp, getTimestampInSeconds, getTimezoneOffset, getTimezoneOffsetHours, getUTCAllWeeksInYear, getUTCDaysDiff, getUTCEndOfDay, getUTCEndOfMonth, getUTCHoursDiff, getUTCMinutesDiff, getUTCNow, getUTCStartOfDay, getUTCStartOfMonth, getUTCTimestamp, getUTCTimestampInSeconds, getUTCWeekEnd, getUTCWeekNumber, getUTCWeekStart, getUTCWeeksInYear, getUTCYearEnd, getUTCYearEndTimestamp, getUTCYearStart, getUTCYearStartTimestamp, getViewportSize, greaterThan, greaterThanOrEqual, hasCookie, isAfterDate, isAndroid, isBeforeDate, isDesktop, isIOS, isMobile, isSameDayDate, isTablet, isTouchDevice, isValidDate, isWeChat, lessThan, lessThanOrEqual, maskEmail, multiply, negate, parseDate, parseMoney, removeCookie, round, setCookie, subDaysFromDate, subDaysUTC, subMonthsFromDate, subMonthsUTC, subtract, throttleFn as throttle, toISOString, toUTC, unmaskEmail };
|
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,20 @@ declare function getQueryParam(key: string, url?: string): string | null;
|
|
|
11
11
|
declare function getAllQueryParams(url?: string): Record<string, string>;
|
|
12
12
|
declare function getQueryParamAll(key: string, url?: string): string[];
|
|
13
13
|
|
|
14
|
+
interface CookieOptions {
|
|
15
|
+
expires?: number | Date;
|
|
16
|
+
path?: string;
|
|
17
|
+
domain?: string;
|
|
18
|
+
secure?: boolean;
|
|
19
|
+
sameSite?: 'Strict' | 'Lax' | 'None';
|
|
20
|
+
}
|
|
21
|
+
declare function setCookie(name: string, value: string, options?: CookieOptions): void;
|
|
22
|
+
declare function getCookie(name: string): string | null;
|
|
23
|
+
declare function removeCookie(name: string, options?: Pick<CookieOptions, 'path' | 'domain'>): void;
|
|
24
|
+
declare function hasCookie(name: string): boolean;
|
|
25
|
+
declare function getAllCookies(): Record<string, string>;
|
|
26
|
+
declare function clearAllCookies(options?: Pick<CookieOptions, 'path' | 'domain'>): void;
|
|
27
|
+
|
|
14
28
|
declare function getOS(): string;
|
|
15
29
|
declare function getBrowser(): string;
|
|
16
30
|
declare function getBrowserEngine(): string;
|
|
@@ -70,6 +84,8 @@ declare function formatPercent(value: number, options?: {
|
|
|
70
84
|
decimals?: number;
|
|
71
85
|
multiply?: boolean;
|
|
72
86
|
}): string;
|
|
87
|
+
declare function maskEmail(email: string): string;
|
|
88
|
+
declare function unmaskEmail(maskedEmail: string, originalEmail: string): string;
|
|
73
89
|
|
|
74
90
|
declare function formatDate(date: Date | number | string, formatStr?: string): string;
|
|
75
91
|
declare function formatDateOnly(date: Date | number | string): string;
|
|
@@ -151,4 +167,4 @@ declare function floor(value: number | string | Decimal, decimalPlaces?: number)
|
|
|
151
167
|
declare function abs(value: number | string | Decimal): number;
|
|
152
168
|
declare function negate(value: number | string | Decimal): number;
|
|
153
169
|
|
|
154
|
-
export { abs, add, addDaysToDate, addDaysUTC, addMonthsToDate, addMonthsUTC, ceil, debounceFn as debounce, divide, equals, floor, formatDate, formatDateOnly, formatMoney, formatMoneyToChinese, formatNumber, formatPercent, formatRelativeTime, formatTimeOnly, formatUTC, formatUTCDateOnly, formatUTCTimeOnly, fromUTC, getAllQueryParams, getBrowser, getBrowserEngine, getBrowserVersion, getDaysDiff, getDeviceInfo, getDevicePixelRatio, getDeviceType, getEndOfDay, getEndOfMonth, getEndOfWeek, getEndOfYear, getHoursDiff, getMinutesDiff, getOS, getQueryParam, getQueryParamAll, getScreenResolution, getStartOfDay, getStartOfMonth, getStartOfWeek, getStartOfYear, getTimestamp, getTimestampInSeconds, getTimezoneOffset, getTimezoneOffsetHours, getUTCAllWeeksInYear, getUTCDaysDiff, getUTCEndOfDay, getUTCEndOfMonth, getUTCHoursDiff, getUTCMinutesDiff, getUTCNow, getUTCStartOfDay, getUTCStartOfMonth, getUTCTimestamp, getUTCTimestampInSeconds, getUTCWeekEnd, getUTCWeekNumber, getUTCWeekStart, getUTCWeeksInYear, getUTCYearEnd, getUTCYearEndTimestamp, getUTCYearStart, getUTCYearStartTimestamp, getViewportSize, greaterThan, greaterThanOrEqual, isAfterDate, isAndroid, isBeforeDate, isDesktop, isIOS, isMobile, isSameDayDate, isTablet, isTouchDevice, isValidDate, isWeChat, lessThan, lessThanOrEqual, multiply, negate, parseDate, parseMoney, round, subDaysFromDate, subDaysUTC, subMonthsFromDate, subMonthsUTC, subtract, throttleFn as throttle, toISOString, toUTC };
|
|
170
|
+
export { type CookieOptions, abs, add, addDaysToDate, addDaysUTC, addMonthsToDate, addMonthsUTC, ceil, clearAllCookies, debounceFn as debounce, divide, equals, floor, formatDate, formatDateOnly, formatMoney, formatMoneyToChinese, formatNumber, formatPercent, formatRelativeTime, formatTimeOnly, formatUTC, formatUTCDateOnly, formatUTCTimeOnly, fromUTC, getAllCookies, getAllQueryParams, getBrowser, getBrowserEngine, getBrowserVersion, getCookie, getDaysDiff, getDeviceInfo, getDevicePixelRatio, getDeviceType, getEndOfDay, getEndOfMonth, getEndOfWeek, getEndOfYear, getHoursDiff, getMinutesDiff, getOS, getQueryParam, getQueryParamAll, getScreenResolution, getStartOfDay, getStartOfMonth, getStartOfWeek, getStartOfYear, getTimestamp, getTimestampInSeconds, getTimezoneOffset, getTimezoneOffsetHours, getUTCAllWeeksInYear, getUTCDaysDiff, getUTCEndOfDay, getUTCEndOfMonth, getUTCHoursDiff, getUTCMinutesDiff, getUTCNow, getUTCStartOfDay, getUTCStartOfMonth, getUTCTimestamp, getUTCTimestampInSeconds, getUTCWeekEnd, getUTCWeekNumber, getUTCWeekStart, getUTCWeeksInYear, getUTCYearEnd, getUTCYearEndTimestamp, getUTCYearStart, getUTCYearStartTimestamp, getViewportSize, greaterThan, greaterThanOrEqual, hasCookie, isAfterDate, isAndroid, isBeforeDate, isDesktop, isIOS, isMobile, isSameDayDate, isTablet, isTouchDevice, isValidDate, isWeChat, lessThan, lessThanOrEqual, maskEmail, multiply, negate, parseDate, parseMoney, removeCookie, round, setCookie, subDaysFromDate, subDaysUTC, subMonthsFromDate, subMonthsUTC, subtract, throttleFn as throttle, toISOString, toUTC, unmaskEmail };
|
package/dist/index.js
CHANGED
|
@@ -75,6 +75,66 @@ function getQueryParamAll(key, url) {
|
|
|
75
75
|
return searchParams.getAll(key);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
// src/cookie/index.ts
|
|
79
|
+
function setCookie(name, value, options = {}) {
|
|
80
|
+
let cookieString = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
|
|
81
|
+
if (options.expires) {
|
|
82
|
+
const expires = options.expires instanceof Date ? options.expires : new Date(Date.now() + options.expires * 1e3);
|
|
83
|
+
cookieString += `; expires=${expires.toUTCString()}`;
|
|
84
|
+
}
|
|
85
|
+
if (options.path) {
|
|
86
|
+
cookieString += `; path=${options.path}`;
|
|
87
|
+
}
|
|
88
|
+
if (options.domain) {
|
|
89
|
+
cookieString += `; domain=${options.domain}`;
|
|
90
|
+
}
|
|
91
|
+
if (options.secure) {
|
|
92
|
+
cookieString += "; secure";
|
|
93
|
+
}
|
|
94
|
+
if (options.sameSite) {
|
|
95
|
+
cookieString += `; SameSite=${options.sameSite}`;
|
|
96
|
+
}
|
|
97
|
+
document.cookie = cookieString;
|
|
98
|
+
}
|
|
99
|
+
function getCookie(name) {
|
|
100
|
+
const nameEQ = encodeURIComponent(name) + "=";
|
|
101
|
+
const cookies = document.cookie.split(";");
|
|
102
|
+
for (let cookie of cookies) {
|
|
103
|
+
cookie = cookie.trim();
|
|
104
|
+
if (cookie.indexOf(nameEQ) === 0) {
|
|
105
|
+
return decodeURIComponent(cookie.substring(nameEQ.length));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
function removeCookie(name, options = {}) {
|
|
111
|
+
setCookie(name, "", {
|
|
112
|
+
...options,
|
|
113
|
+
expires: /* @__PURE__ */ new Date(0)
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
function hasCookie(name) {
|
|
117
|
+
return getCookie(name) !== null;
|
|
118
|
+
}
|
|
119
|
+
function getAllCookies() {
|
|
120
|
+
const cookies = {};
|
|
121
|
+
const cookieArray = document.cookie.split(";");
|
|
122
|
+
for (let cookie of cookieArray) {
|
|
123
|
+
cookie = cookie.trim();
|
|
124
|
+
const [name, ...valueParts] = cookie.split("=");
|
|
125
|
+
if (name) {
|
|
126
|
+
cookies[decodeURIComponent(name)] = decodeURIComponent(valueParts.join("="));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return cookies;
|
|
130
|
+
}
|
|
131
|
+
function clearAllCookies(options = {}) {
|
|
132
|
+
const cookies = getAllCookies();
|
|
133
|
+
for (const name in cookies) {
|
|
134
|
+
removeCookie(name, options);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
78
138
|
// src/device/index.ts
|
|
79
139
|
function getUserAgent() {
|
|
80
140
|
return navigator.userAgent.toLowerCase();
|
|
@@ -302,6 +362,39 @@ function formatPercent(value, options = {}) {
|
|
|
302
362
|
const num = multiply2 ? value * 100 : value;
|
|
303
363
|
return num.toFixed(decimals) + "%";
|
|
304
364
|
}
|
|
365
|
+
function maskEmail(email) {
|
|
366
|
+
if (!email || typeof email !== "string") {
|
|
367
|
+
return "";
|
|
368
|
+
}
|
|
369
|
+
const atIndex = email.indexOf("@");
|
|
370
|
+
if (atIndex <= 0) {
|
|
371
|
+
return email;
|
|
372
|
+
}
|
|
373
|
+
const localPart = email.substring(0, atIndex);
|
|
374
|
+
const domainPart = email.substring(atIndex);
|
|
375
|
+
const visiblePart = localPart.substring(0, 3);
|
|
376
|
+
return `${visiblePart}***${domainPart}`;
|
|
377
|
+
}
|
|
378
|
+
function unmaskEmail(maskedEmail, originalEmail) {
|
|
379
|
+
if (!maskedEmail || !originalEmail) {
|
|
380
|
+
return "";
|
|
381
|
+
}
|
|
382
|
+
if (!maskedEmail.includes("***@")) {
|
|
383
|
+
return maskedEmail;
|
|
384
|
+
}
|
|
385
|
+
const maskedParts = maskedEmail.split("***@");
|
|
386
|
+
const atIndex = originalEmail.indexOf("@");
|
|
387
|
+
if (atIndex <= 0) {
|
|
388
|
+
return maskedEmail;
|
|
389
|
+
}
|
|
390
|
+
const originalPrefix = originalEmail.substring(0, 3);
|
|
391
|
+
const originalDomain = originalEmail.substring(atIndex + 1);
|
|
392
|
+
const maskedDomain = maskedParts[1];
|
|
393
|
+
if (maskedParts[0] === originalPrefix && maskedDomain === originalDomain) {
|
|
394
|
+
return originalEmail;
|
|
395
|
+
}
|
|
396
|
+
return maskedEmail;
|
|
397
|
+
}
|
|
305
398
|
|
|
306
399
|
// src/date/index.ts
|
|
307
400
|
import {
|
|
@@ -759,6 +852,7 @@ export {
|
|
|
759
852
|
addMonthsToDate,
|
|
760
853
|
addMonthsUTC,
|
|
761
854
|
ceil,
|
|
855
|
+
clearAllCookies,
|
|
762
856
|
debounceFn as debounce,
|
|
763
857
|
divide,
|
|
764
858
|
equals,
|
|
@@ -775,10 +869,12 @@ export {
|
|
|
775
869
|
formatUTCDateOnly,
|
|
776
870
|
formatUTCTimeOnly,
|
|
777
871
|
fromUTC,
|
|
872
|
+
getAllCookies,
|
|
778
873
|
getAllQueryParams,
|
|
779
874
|
getBrowser,
|
|
780
875
|
getBrowserEngine,
|
|
781
876
|
getBrowserVersion,
|
|
877
|
+
getCookie,
|
|
782
878
|
getDaysDiff,
|
|
783
879
|
getDeviceInfo,
|
|
784
880
|
getDevicePixelRatio,
|
|
@@ -823,6 +919,7 @@ export {
|
|
|
823
919
|
getViewportSize,
|
|
824
920
|
greaterThan,
|
|
825
921
|
greaterThanOrEqual,
|
|
922
|
+
hasCookie,
|
|
826
923
|
isAfterDate,
|
|
827
924
|
isAndroid,
|
|
828
925
|
isBeforeDate,
|
|
@@ -836,11 +933,14 @@ export {
|
|
|
836
933
|
isWeChat,
|
|
837
934
|
lessThan,
|
|
838
935
|
lessThanOrEqual,
|
|
936
|
+
maskEmail,
|
|
839
937
|
multiply,
|
|
840
938
|
negate,
|
|
841
939
|
parseDate,
|
|
842
940
|
parseMoney,
|
|
941
|
+
removeCookie,
|
|
843
942
|
round,
|
|
943
|
+
setCookie,
|
|
844
944
|
subDaysFromDate,
|
|
845
945
|
subDaysUTC,
|
|
846
946
|
subMonthsFromDate,
|
|
@@ -848,5 +948,6 @@ export {
|
|
|
848
948
|
subtract,
|
|
849
949
|
throttleFn as throttle,
|
|
850
950
|
toISOString,
|
|
851
|
-
toUTC
|
|
951
|
+
toUTC,
|
|
952
|
+
unmaskEmail
|
|
852
953
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-util-tools",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.24",
|
|
4
4
|
"description": "A collection of useful utilities: throttle, debounce, date formatting, device detection, money formatting, decimal calculations and more",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -17,11 +17,11 @@
|
|
|
17
17
|
"scripts": {
|
|
18
18
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
19
19
|
"dev": "tsup src/index.ts --watch",
|
|
20
|
-
"prepublishOnly": "
|
|
21
|
-
"release:patch": "
|
|
22
|
-
"release:minor": "
|
|
23
|
-
"release:major": "
|
|
24
|
-
"release": "
|
|
20
|
+
"prepublishOnly": "pnpm run build",
|
|
21
|
+
"release:patch": "pnpm version patch --no-git-tag-version && git add . && git commit -m 'chore: release v%s' && git tag v$(node -p \"require('./package.json').version\") && git push --follow-tags && pnpm publish --no-git-checks",
|
|
22
|
+
"release:minor": "pnpm version minor --no-git-tag-version && git add . && git commit -m 'chore: release v%s' && git tag v$(node -p \"require('./package.json').version\") && git push --follow-tags && pnpm publish --no-git-checks",
|
|
23
|
+
"release:major": "pnpm version major --no-git-tag-version && git add . && git commit -m 'chore: release v%s' && git tag v$(node -p \"require('./package.json').version\") && git push --follow-tags && pnpm publish --no-git-checks",
|
|
24
|
+
"release": "pnpm run release:patch"
|
|
25
25
|
},
|
|
26
26
|
"keywords": [
|
|
27
27
|
"react",
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
// 按需导出:支持 Tree Shaking
|
|
2
2
|
export { throttle, debounce } from './throttle/index'
|
|
3
3
|
export { getQueryParam, getAllQueryParams, getQueryParamAll } from './address/index'
|
|
4
|
+
export {
|
|
5
|
+
setCookie,
|
|
6
|
+
getCookie,
|
|
7
|
+
removeCookie,
|
|
8
|
+
hasCookie,
|
|
9
|
+
getAllCookies,
|
|
10
|
+
clearAllCookies
|
|
11
|
+
} from './cookie/index'
|
|
12
|
+
export type { CookieOptions } from './cookie/index'
|
|
4
13
|
export {
|
|
5
14
|
getOS,
|
|
6
15
|
getBrowser,
|
|
@@ -24,7 +33,9 @@ export {
|
|
|
24
33
|
parseMoney,
|
|
25
34
|
formatNumber,
|
|
26
35
|
formatMoneyToChinese,
|
|
27
|
-
formatPercent
|
|
36
|
+
formatPercent,
|
|
37
|
+
maskEmail,
|
|
38
|
+
unmaskEmail
|
|
28
39
|
} from './format/index'
|
|
29
40
|
export {
|
|
30
41
|
formatDate,
|