(options: GenerateOptions = {})
| 38 | export { Strategy, analyze, predictCodeQuality, registerRng } |
| 39 | |
| 40 | export function generateCode(options: GenerateOptions = {}): string { |
| 41 | const length: CodeLength = options.length === 4 ? 4 : 6 |
| 42 | const strategy: Strategy = options.strategy ?? Strategy.Mixed |
| 43 | |
| 44 | if (options.mode === 'banking') { |
| 45 | return generateBankingCode({ ...options, length }) |
| 46 | } |
| 47 | |
| 48 | const impl = getStrategyImpl(length, strategy, options) |
| 49 | |
| 50 | const maxAttempts = 100 |
| 51 | let code = '' |
| 52 | |
| 53 | for (let i = 0; i < maxAttempts; i++) { |
| 54 | code = impl() |
| 55 | if (code.length !== length) { |
| 56 | code = code.slice(0, length) |
| 57 | } |
| 58 | if (isCodeAllowed(code, options)) { |
| 59 | return code |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return code.slice(0, length) |
| 64 | } |
| 65 | |
| 66 | function generateBankingCode(options: GenerateOptions = {}): string { |
| 67 | const length: CodeLength = options.length === 4 ? 4 : 6 |
no test coverage detected