MCPcopy Index your code
hub / github.com/TeaCoder52/patcode / generateBankingCode

Function generateBankingCode

src/index.ts:66–90  ·  view source on GitHub ↗
(options: GenerateOptions = {})

Source from the content-addressed store, hash-verified

64}
65
66function 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
92export function isHumanFriendly(
93 code: string,

Callers 1

generateCodeFunction · 0.85

Calls 5

getAllowedDigitsFunction · 0.90
getRngFunction · 0.90
pickDigitFunction · 0.90
fromDigitsFunction · 0.90
isCodeAllowedFunction · 0.90

Tested by

no test coverage detected