Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/utils.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123import type { CsvRow } from '@okikio/csv/types'
import { DEFAULT_DOMAIN_CLASSIFIER } from './classify.ts'import { normalizeDomain, normalizeHostname } from './normalize.ts'import type { DomainColumnType, DomainSource, DomainSourceKind, EmailDomainClassifier, EmailRowDomain, RowDomainCandidate,} from './types.ts'
/** Ranking used only to choose among candidates within one source row. */const SOURCE_CONFIDENCE = Object.freeze({ domain: 100, website: 80, email: 60,} satisfies Readonly<Record<DomainSourceKind, number>>)
/** Splits cells using rules appropriate to their semantic source. */function splitCell(value: string, kind: DomainSourceKind): readonly string[] { if (kind === 'email') { return Object.freeze(Array.from(value.matchAll(/@([^\s>,;]+)/gu), (match) => match[1] ?? '')) } return Object.freeze(value.split(/[,;\n|]+/u).map((part) => part.trim()).filter(Boolean))}
/** Creates a stable key for deduplicating repeated source evidence within a row. */function sourceKey(source: DomainSource): string { return `${source.row}:${source.column}:${source.kind}:${source.candidate}:${source.value}`}
/** Product-neutral policy used by company-domain extraction. */function excludesCompanyCandidate( classifier: EmailDomainClassifier, domain: string,): boolean { return classifier.classify(domain).evidence.some(({ trait }) => trait === 'public-mailbox' || trait === 'privacy-relay' || trait === 'disposable' )}
/** * Snapshots candidate so later compilation cannot observe caller mutation. * * @internal */function freezeCandidate( domain: string, value: { readonly confidence: number; readonly sources: readonly DomainSource[] },): RowDomainCandidate { return Object.freeze({ domain, confidence: value.confidence, sources: Object.freeze([...value.sources]), })}
/** * Streams row-level company-domain candidates from already parsed CSV rows. * * CSV parsing and email-domain policy remain independent utilities. This * function is the explicit composition point where column roles, hostname * normalization, and mailbox classification are used together. */export async function* extractDomains( rows: AsyncIterable<CsvRow>, columns: readonly DomainColumnType[], classifier: EmailDomainClassifier = DEFAULT_DOMAIN_CLASSIFIER,): AsyncGenerator<EmailRowDomain> { const companyColumn = columns.find((column) => column.role === 'company')
for await (const row of rows) { const candidates = new Map<string, { confidence: number; sources: DomainSource[] }>() for (const column of columns) { if (column.role !== 'domain' && column.role !== 'website' && column.role !== 'email') continue const value = row.values[column.index]?.trim() if (!value) continue
for (const part of splitCell(value, column.role)) { const domain = column.role === 'email' ? normalizeDomain(part) : normalizeHostname(part) if (!domain || (column.role === 'email' && excludesCompanyCandidate(classifier, domain))) continue
const source = Object.freeze({ row: row.row, column: column.index + 1, header: column.name, value, candidate: part, kind: column.role, }) satisfies DomainSource const current = candidates.get(domain) ?? { confidence: SOURCE_CONFIDENCE[column.role], sources: [], } current.confidence = Math.max(current.confidence, SOURCE_CONFIDENCE[column.role]) if (!current.sources.some((item) => sourceKey(item) === sourceKey(source))) { current.sources.push(source) } candidates.set(domain, current) } }
const ranked = Object.freeze([...candidates.entries()] .map(([domain, value]) => freezeCandidate(domain, value)) .sort((left, right) => right.confidence - left.confidence || left.domain.localeCompare(right.domain) )) const primary = ranked[0] if (!primary) continue const company = companyColumn ? row.values[companyColumn.index]?.trim() || undefined : undefined yield Object.freeze({ row: row.row, ...(company ? { company } : {}), primary, candidates: ranked, }) }}