UNPKG

shoetest

Version:

Powerful string matching insensitive to diacritics, special characters, symbols and case

184 lines 8.12 kB
type TextInput = string | string[]; type SearchResult<T> = T | undefined; interface ShoetestOptions { strict?: boolean; diacritics?: boolean; charCase?: boolean; symbols?: boolean; whitespaces?: boolean; boundaries?: boolean; begin?: string; end?: string; } declare class Shoetest { private readonly basic; private basicExtra; private readonly special; private readonly symbols; constructor(); /** * Initialize character mappings for basic and extra characters * * This method sets up the basic and extra character indices used for * fuzzy matching, including special character mappings. */ private initializeCharacterIndices; /** * Build character mappings for the given character set * * Creates regex patterns for character variations and stores them in the index. * For basic mappings, creates simple alternation patterns. For extra mappings, * combines existing patterns with new variants using non-capturing groups. * * @param chars - Character mapping object where keys are base characters and values are their variants * @param index - The character index to populate with regex patterns * @param isExtra - Whether this is building extra character mappings (combines with existing patterns) */ private buildCharacterMappings; /** * Deep clone an object using the best available method * * Uses structuredClone if available (modern browsers/Node.js), * otherwise falls back to JSON serialization for deep cloning. * * @param obj - The object to clone * @returns A deep copy of the input object */ private cloneDeep; /** * Remove diacritics and accents from text * * Converts accented and special characters to their basic equivalents * using the character mapping table. Returns undefined for falsy inputs. * * @param str - The input string to simplify * @returns The simplified string without diacritics, or undefined if input is falsy */ simplify(str: string): SearchResult<string>; /** * Create a fuzzy matching regular expression * * Generates a regex pattern that matches the input string with various * character variations based on the provided options. Handles diacritics, * case sensitivity, symbols, and other fuzzy matching features. * * @param str - The input string to create a regex pattern for * @param options - Configuration options for the regex generation * @returns A RegExp object for fuzzy matching, or undefined if input is falsy */ getRegExp(str: string, options?: ShoetestOptions): SearchResult<RegExp>; /** * Build the actual regex pattern from processed string and options * * Constructs the final regular expression by preprocessing the input string * and building the pattern with appropriate flags based on configuration. * * @param str - The input string to build a pattern for * @param options - Complete configuration options for pattern building * @param index - Character index to use for pattern generation * @returns A RegExp object with the generated pattern and appropriate flags */ private buildRegexPattern; /** * Preprocess string based on symbol and boundary options * * Handles removal or transformation of symbols and boundaries based on * configuration options. Prepares separators for pattern building. * * @param str - The input string to preprocess * @param options - Complete configuration options for preprocessing * @returns Object containing processed string and boundary/symbol separators */ private preprocessString; /** * Build the core pattern from processed string * * Constructs the main regex pattern by iterating through characters * and handling multi-character sequences, boundaries, and separators. * * @param str - The preprocessed input string * @param options - Complete configuration options for pattern building * @param index - Character index to use for pattern generation * @param sb - Boundary separator pattern * @param sp - Symbol separator pattern * @returns The constructed regex pattern string */ private buildPattern; /** * Get regex pattern for a single character * * Creates a regex pattern for an individual character, handling * whitespace, symbols, and character variations based on options. * * @param char - The character to create a pattern for * @param options - Complete configuration options for pattern building * @param index - Character index to use for pattern generation * @returns The regex pattern string for the character */ private getCharacterRegex; /** * Get regex pattern for multi-character sequences * * Attempts to find regex patterns for character sequences of 2 or 3 characters * in the character index. Used for handling ligatures and multi-character mappings. * * @param char1 - First character of the sequence * @param char2 - Second character of the sequence * @param char3 - Optional third character of the sequence * @param index - Character index to search for multi-character patterns * @returns The regex pattern string for the sequence, or null if not found */ private getMultiCharRegex; /** * Check if pattern exists in target text(s) * * Tests whether the fuzzy pattern matches any of the provided texts. * Returns true if any match is found, false if no matches, undefined for invalid inputs. * * @param str - The pattern string to search for * @param texts - Single text string or array of strings to search in * @param options - Configuration options for fuzzy matching * @returns True if pattern matches any text, false if no matches, undefined if invalid input */ test(str: string, texts: TextInput, options?: ShoetestOptions): SearchResult<boolean>; /** * Extract all matching substrings from text(s) * * Searches for all occurrences of the fuzzy pattern in the provided text(s) * and returns an array of matching substrings. Filters out non-string inputs. * * @param str - The pattern string to search for * @param texts - Single text string or array of strings to search in * @param options - Configuration options for fuzzy matching * @returns Array of matching substrings, empty array if no matches, undefined for invalid input */ match(str: string, texts: TextInput, options?: ShoetestOptions): SearchResult<string[]>; /** * Replace pattern matches with new content * * Searches for all occurrences of the fuzzy pattern in the provided text(s) * and replaces them with the specified replacement string. Preserves original * input structure (string vs array). * * @param str - The pattern string to search for * @param newstr - The replacement string for matches * @param texts - Single text string or array of strings to search in * @param options - Configuration options for fuzzy matching * @returns Modified string/array with replacements, or undefined for invalid input */ replace(str: string, newstr: string, texts: TextInput, options?: ShoetestOptions): SearchResult<string | string[]>; /** * Add random diacritics and character variations to text * * Generates a random variation of the input string by creating a fuzzy * regex pattern and using it to produce a randomized version with * character substitutions and diacritics. * * @param str - The input string to complexify * @returns A randomized version of the string with character variations, or undefined if input is falsy */ complexify(str: string): SearchResult<string>; } declare const shoetestInstance: Shoetest; export default shoetestInstance; //# sourceMappingURL=shoetest.d.ts.map