react-util-tools 1.0.14 → 1.0.21
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/package.json +1 -1
- package/src/format/index.ts +65 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-util-tools",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.21",
|
|
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",
|
package/src/format/index.ts
CHANGED
|
@@ -187,3 +187,68 @@ export function formatPercent(
|
|
|
187
187
|
const num = multiply ? value * 100 : value
|
|
188
188
|
return num.toFixed(decimals) + '%'
|
|
189
189
|
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 邮箱脱敏:只展示前3个字符+@和后面的字符串,中间用***表示
|
|
193
|
+
* @param email 邮箱地址
|
|
194
|
+
* @returns 脱敏后的邮箱地址
|
|
195
|
+
* @example
|
|
196
|
+
* maskEmail('dj49846917@proton.me') // 'dj4***@proton.me'
|
|
197
|
+
* maskEmail('abc@example.com') // 'abc***@example.com'
|
|
198
|
+
*/
|
|
199
|
+
export function maskEmail(email: string): string {
|
|
200
|
+
if (!email || typeof email !== 'string') {
|
|
201
|
+
return ''
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const atIndex = email.indexOf('@')
|
|
205
|
+
if (atIndex <= 0) {
|
|
206
|
+
return email // 无效邮箱,返回原值
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const localPart = email.substring(0, atIndex)
|
|
210
|
+
const domainPart = email.substring(atIndex)
|
|
211
|
+
|
|
212
|
+
// 只展示前3个字符
|
|
213
|
+
const visiblePart = localPart.substring(0, 3)
|
|
214
|
+
|
|
215
|
+
return `${visiblePart}***${domainPart}`
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 邮箱解脱敏:将脱敏的邮箱还原(需要提供原始邮箱)
|
|
220
|
+
* @param maskedEmail 脱敏后的邮箱地址
|
|
221
|
+
* @param originalEmail 原始邮箱地址
|
|
222
|
+
* @returns 解脱敏后的邮箱地址
|
|
223
|
+
* @example
|
|
224
|
+
* unmaskEmail('dj4***@proton.me', 'dj49846917@proton.me') // 'dj49846917@proton.me'
|
|
225
|
+
*/
|
|
226
|
+
export function unmaskEmail(maskedEmail: string, originalEmail: string): string {
|
|
227
|
+
if (!maskedEmail || !originalEmail) {
|
|
228
|
+
return ''
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// 验证脱敏邮箱格式
|
|
232
|
+
if (!maskedEmail.includes('***@')) {
|
|
233
|
+
return maskedEmail // 不是脱敏格式,直接返回
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// 验证原始邮箱和脱敏邮箱是否匹配
|
|
237
|
+
const maskedParts = maskedEmail.split('***@')
|
|
238
|
+
const atIndex = originalEmail.indexOf('@')
|
|
239
|
+
|
|
240
|
+
if (atIndex <= 0) {
|
|
241
|
+
return maskedEmail // 原始邮箱无效
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const originalPrefix = originalEmail.substring(0, 3)
|
|
245
|
+
const originalDomain = originalEmail.substring(atIndex + 1)
|
|
246
|
+
const maskedDomain = maskedParts[1]
|
|
247
|
+
|
|
248
|
+
// 验证前缀和域名是否匹配
|
|
249
|
+
if (maskedParts[0] === originalPrefix && maskedDomain === originalDomain) {
|
|
250
|
+
return originalEmail
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return maskedEmail // 不匹配,返回脱敏邮箱
|
|
254
|
+
}
|