|
| 1 | +import { isAllSame, isTooSimpleSequence } from './utils' |
| 2 | +import type { CodeLength, CodeAnalysis } from './types' |
| 3 | + |
| 4 | +function isSequentialAsc(code: string): boolean { |
| 5 | + for (let i = 1; i < code.length; i++) { |
| 6 | + const prev = Number(code[i - 1]) |
| 7 | + const curr = Number(code[i]) |
| 8 | + if (curr !== (prev + 1) % 10) return false |
| 9 | + } |
| 10 | + return true |
| 11 | +} |
| 12 | + |
| 13 | +function isSequentialDesc(code: string): boolean { |
| 14 | + for (let i = 1; i < code.length; i++) { |
| 15 | + const prev = Number(code[i - 1]) |
| 16 | + const curr = Number(code[i]) |
| 17 | + if (curr !== (prev + 9) % 10) return false |
| 18 | + } |
| 19 | + return true |
| 20 | +} |
| 21 | + |
| 22 | +function countGroups(code: string): number { |
| 23 | + let groups = 1 |
| 24 | + for (let i = 1; i < code.length; i++) { |
| 25 | + if (code[i] !== code[i - 1]) groups++ |
| 26 | + } |
| 27 | + return groups |
| 28 | +} |
| 29 | + |
| 30 | +function estimateEntropy(code: string): number { |
| 31 | + const freq: Record<string, number> = {} |
| 32 | + |
| 33 | + for (const d of code) freq[d] = (freq[d] || 0) + 1 |
| 34 | + |
| 35 | + let entropy = 0 |
| 36 | + for (const k in freq) { |
| 37 | + const p = freq[k] / code.length |
| 38 | + entropy -= p * Math.log2(p) |
| 39 | + } |
| 40 | + |
| 41 | + return entropy * code.length |
| 42 | +} |
| 43 | + |
| 44 | +function collisionRisk( |
| 45 | + code: string, |
| 46 | + entropy: number |
| 47 | +): 'low' | 'medium' | 'high' { |
| 48 | + if (code.length === 4) { |
| 49 | + // Max entropy ~8 bits |
| 50 | + if (entropy > 6.5) return 'low' |
| 51 | + if (entropy > 3.5) return 'medium' |
| 52 | + return 'high' |
| 53 | + } |
| 54 | + |
| 55 | + if (code.length === 6) { |
| 56 | + // Max entropy ~12 bits |
| 57 | + if (entropy > 10) return 'low' |
| 58 | + if (entropy > 6) return 'medium' |
| 59 | + return 'high' |
| 60 | + } |
| 61 | + |
| 62 | + return 'high' |
| 63 | +} |
| 64 | + |
| 65 | +function memorabilityScore( |
| 66 | + code: string, |
| 67 | + groups: number |
| 68 | +): 'low' | 'medium' | 'high' { |
| 69 | + if (isAllSame(code)) return 'low' |
| 70 | + if (isTooSimpleSequence(code)) return 'low' |
| 71 | + |
| 72 | + if (groups <= 2) return 'high' |
| 73 | + |
| 74 | + if (groups === 3) return 'medium' |
| 75 | + |
| 76 | + return 'low' |
| 77 | +} |
| 78 | + |
| 79 | +export function analyze(code: string): CodeAnalysis { |
| 80 | + const length = code.length as CodeLength |
| 81 | + |
| 82 | + const allSame = isAllSame(code) |
| 83 | + const tooSimple = isTooSimpleSequence(code) |
| 84 | + const asc = isSequentialAsc(code) |
| 85 | + const desc = isSequentialDesc(code) |
| 86 | + const groups = countGroups(code) |
| 87 | + |
| 88 | + const entropy = estimateEntropy(code) |
| 89 | + const collision = collisionRisk(code, entropy) |
| 90 | + const memory = memorabilityScore(code, groups) |
| 91 | + |
| 92 | + return { |
| 93 | + length, |
| 94 | + isAllSame: allSame, |
| 95 | + isTooSimple: tooSimple, |
| 96 | + isSequentialAsc: asc, |
| 97 | + isSequentialDesc: desc, |
| 98 | + repeatedGroups: groups, |
| 99 | + |
| 100 | + entropy, |
| 101 | + collisionRisk: collision, |
| 102 | + memorability: memory, |
| 103 | + } |
| 104 | +} |
0 commit comments