Skip to content

extract()

Returns the top N candidates that score at or above the threshold. Combines filtering and limiting in one call.

Signature

ts
// Default — returns string[]
extract(input: string, candidates: string[], options?: RateOptions & { limit?: number; return?: 'values' }): string[]

// Detailed — returns ranked objects
extract(input: string, candidates: string[], options: RateOptions & { limit?: number; return: 'entries' }): RankedCandidate[]
extract(input: string, candidates: string[], options: RateOptions & { limit?: number; return: 'entries'; explain: true }): RankedCandidateExplain[]

Usage

js
import { extract } from 'guess-rater'

// Default: top 5, threshold 80, returns string[]
extract('apple', ['Apple Watch', 'Apple TV', 'Samsung TV', 'iPad'])
// ['Apple Watch', 'Apple TV']

// Custom limit and threshold
extract('apple', candidates, { limit: 2, threshold: 60 })
// ['Apple Watch', 'Apple TV']

// Detailed objects
extract('apple', candidates, { return: 'entries', limit: 3 })
// [{ value: 'Apple Watch', score: 91, index: 0 }, ...]

// With explain
extract('apple', candidates, { return: 'entries', explain: true })

explain is only applied when return: 'entries'.

Options

OptionDefaultDescription
limit5Maximum number of results
threshold80Minimum score to include
return'values''values'string[], 'entries'{value, score, index}[]
explainfalseInclude explain payload (requires return: 'entries')
+ all rate() options

Pipeline

rankCandidates()  →  filter(score >= threshold)  →  slice(0, limit)

See also