@loke/ui
Version:
60 lines (59 loc) • 2.44 kB
TypeScript
/**
* @fileoverview Command scoring algorithm for fuzzy string matching.
*
* This module implements a sophisticated fuzzy matching algorithm that scores
* the relevance of strings against search abbreviations. The algorithm considers
* various factors including character continuity, word boundaries, case sensitivity,
* and character transpositions to provide intuitive search results.
*
* The scoring system is designed so that a continuous match of characters
* results in a total score of 1, with various penalties and bonuses applied
* based on match quality and context.
*/
/**
* Calculates a fuzzy match score between a target string and a search abbreviation.
*
* This function implements a sophisticated scoring algorithm that considers:
* - Character sequence continuity (higher scores for consecutive matches)
* - Word boundary awareness (bonuses for matches at word starts)
* - Case sensitivity (slight preference for exact case matches)
* - Character transpositions (handling common typing errors)
* - Skip penalties (closer matches score higher)
* - Alias support (additional searchable terms)
*
* The algorithm is optimized for typical user search behavior, where users
* often type the first letters of words or use abbreviations that follow
* natural word boundaries.
*
* @returns A numeric score between 0 and 1, where:
* - 1.0 = Perfect match
* - >0.8 = Excellent match (typically word boundary matches)
* - >0.5 = Good match (some word boundaries or close characters)
* - >0.1 = Fair match (scattered character matches)
* - 0 = No match
*
* @example
* ```typescript
* // Perfect abbreviation match
* commandScore("Hello World", "HW") // ~0.9 (high score)
*
* // Partial word match
* commandScore("Hello World", "Hel") // ~0.95 (very high score)
*
* // Character jumps within words
* commandScore("example", "amp") // ~0.17 (lower score)
*
* // With aliases for enhanced matching
* commandScore("HTML", "markup", ["HyperText", "web"]) // Matches "markup" in aliases
*
* // Transposition tolerance
* commandScore("touch", "otuch") // >0 (handles letter swaps)
* ```
*/
export declare function commandScore(
/** The target string to score against (e.g., "Hello World") */
string: string,
/** The search query/abbreviation (e.g., "HW" or "helo") */
abbreviation: string,
/** Optional array of additional terms to include in matching */
aliases?: string[]): number;