Skip to main content
extractCardNumber (singular) finds Iranian card numbers inside free-form text — chat logs, OCR output, payment confirmations — with eight type-narrowing overloads based on option combinations.
The exported function is extractCardNumber (singular). There is no extractCardNumbers plural export, and no extractCardNumberWithMetrics.

Basic usage

Result shape

Every match has at minimum:
Options add fields:
  • checkValidation: trueisValid: boolean
  • detectBankNumber: truebankName: string | null
  • includeContext: truecontext: { before, after }

Multi-format input

Persian (۰-۹) and Arabic-Indic (٠-٩) digits, plus dashes / underscores / spaces, are recognized and normalized into pure:

Fuzzy matching for masked cards

Uses fuzzyCardNumberRegex to tolerate *, ?, and similar masking. Slower; gate by shouldUseFuzzyMatching(text, config) on large inputs.

Large-text optimization

Internally chunks the text via splitTextIntoChunks (with chunk size from getOptimalChunkConfig) to avoid regex backtracking across the whole document.

Context capture

Useful for UI snippets (”…در پیامک از ۶۰۳۷۷۰۱۶۸۹۰۹۵۴۴۳ خرید شد…”).

Type narrowing overloads

The function exposes 8 overloads:
All option/result interfaces are exported (ExtractCardNumberOptions, ExtractCardNumberComplete, ExtractCardNumberWithBank, etc.).

Re-exported helpers and constants

Pitfalls

  • No metrics function exists. Time the call yourself if you need throughput data.
  • null is not in the TS signature, though the runtime guard returns [] for falsy. Pass a string.
  • Use pure (not base) when piping into verifyCardNumber / getBankNameFromCardNumber.

Source

src/modules/extractCardNumbers/index.ts, types.ts, utils.ts, constants.ts · Tests: test/extractCardNumber.spec.ts