react-util-tools 1.0.14 → 1.0.23

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.
Files changed (2) hide show
  1. package/package.json +10 -11
  2. 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.14",
3
+ "version": "1.0.23",
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",
@@ -14,15 +14,6 @@
14
14
  "require": "./dist/index.cjs"
15
15
  }
16
16
  },
17
- "scripts": {
18
- "build": "tsup src/index.ts --format esm,cjs --dts",
19
- "dev": "tsup src/index.ts --watch",
20
- "prepublishOnly": "npm run build",
21
- "release:patch": "npm 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 && npm publish",
22
- "release:minor": "npm 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 && npm publish",
23
- "release:major": "npm 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 && npm publish",
24
- "release": "npm run release:patch"
25
- },
26
17
  "keywords": [
27
18
  "react",
28
19
  "utils",
@@ -63,5 +54,13 @@
63
54
  "date-fns": "^4.1.0",
64
55
  "date-fns-tz": "^3.2.0",
65
56
  "decimal.js": "^10.6.0"
57
+ },
58
+ "scripts": {
59
+ "build": "tsup src/index.ts --format esm,cjs --dts",
60
+ "dev": "tsup src/index.ts --watch",
61
+ "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",
62
+ "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",
63
+ "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",
64
+ "release": "pnpm run release:patch"
66
65
  }
67
- }
66
+ }
@@ -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
+ }