Przeglądaj źródła

refactor(controllers): 优化 TextRecordController 中的密码生成逻辑

wuyi 5 miesięcy temu
rodzic
commit
353b3c0114
1 zmienionych plików z 13 dodań i 18 usunięć
  1. 13 18
      app/Controllers/Http/TextRecordController.ts

+ 13 - 18
app/Controllers/Http/TextRecordController.ts

@@ -40,25 +40,20 @@ export default class TextRecordController {
     }
 
     private generatePasswordFromRecord(text?: string): string {
-        if (!text || typeof text !== 'string') return ''
+        if (!text) return ''
         const bullet = '•'
-        const result: string[] = []
-        const seenCounts = new Set<number>()
-        const lines = text.split(/\r?\n/)
-        for (const rawLine of lines) {
+        const chars: string[] = []
+        text.split(/\r?\n/).forEach((rawLine) => {
             const line = rawLine.trim()
-            if (line.length === 0) continue
-            const lastChar = line.charAt(line.length - 1)
-            if (lastChar === bullet) continue
-            if (!/[A-Za-z0-9]/.test(lastChar)) continue
-            const head = line.slice(0, -1)
-            const allBullets = head.split('').every((c) => c === bullet)
-            if (!allBullets) continue
-            const bulletCount = head.length
-            if (seenCounts.has(bulletCount)) continue
-            seenCounts.add(bulletCount)
-            result.push(lastChar)
-        }
-        return result.join('')
+            if (!line || line.endsWith(bullet)) return
+            const match = line.match(/^•*/)
+            const bullets = match ? match[0].length : 0
+            const content = line.slice(bullets)
+            if (!/^[A-Za-z0-9]+$/.test(content)) return
+            for (let i = 0; i < content.length; i++) {
+                chars[bullets + i] = content[i]
+            }
+        })
+        return chars.join('')
     }
 }