Files
accounted/extensions/general/enable-banking/lib/bank-match.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

33 lines
1.2 KiB
TypeScript

/**
* Resolve a user-stated bank name ("swedbank", "SEB", "handelsbanken") to
* one ASPSP from the Enable Banking list, so a deep link can start that
* bank's consent directly instead of showing the picker.
*
* Matching is deliberately conservative: exact (case-insensitive) first,
* then a UNIQUE prefix match, then a UNIQUE substring match. An ambiguous
* or unknown name returns null and the caller falls back to the picker
* with the query prefilled: guessing between "Länsförsäkringar Bank" and a
* regional "Länsförsäkringar Skåne" would start a consent at the wrong
* institution.
*/
export function matchBankByName<T extends { name: string }>(
banks: T[],
query: string
): T | null {
const q = query.trim().toLowerCase()
if (!q) return null
const exact = banks.filter((b) => b.name.toLowerCase() === q)
if (exact.length === 1) return exact[0]
if (exact.length > 1) return null
const prefix = banks.filter((b) => b.name.toLowerCase().startsWith(q))
if (prefix.length === 1) return prefix[0]
if (prefix.length > 1) return null
const substring = banks.filter((b) => b.name.toLowerCase().includes(q))
if (substring.length === 1) return substring[0]
return null
}