(options: GenerateOptions = {})
| 64 | } |
| 65 | |
| 66 | function generateBankingCode(options: GenerateOptions = {}): string { |
| 67 | const length: CodeLength = options.length === 4 ? 4 : 6 |
| 68 | const digits = getAllowedDigits(options) |
| 69 | const rng = getRng({ ...options, mode: 'banking' }) |
| 70 | |
| 71 | const maxAttempts = 100 |
| 72 | for (let i = 0; i < maxAttempts; i++) { |
| 73 | const result: string[] = [] |
| 74 | while (result.length < length) { |
| 75 | const d = pickDigit(digits, rng) |
| 76 | if (!result.includes(d)) { |
| 77 | result.push(d) |
| 78 | } |
| 79 | } |
| 80 | const code = fromDigits(result) |
| 81 | if (isCodeAllowed(code, { ...options, mode: 'banking' })) { |
| 82 | return code |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | const fallback = fromDigits( |
| 87 | Array.from({ length }, () => pickDigit(digits, rng)) |
| 88 | ) |
| 89 | return fallback.slice(0, length) |
| 90 | } |
| 91 | |
| 92 | export function isHumanFriendly( |
| 93 | code: string, |
no test coverage detected