node-darts
Version:
Node.js Native Addon for Darts (Double-ARray Trie System)
119 lines (118 loc) • 4.17 kB
TypeScript
/**
* TextDarts class - A class-based interface similar to Perl's Text::Darts
*
* This class provides a class-based interface similar to Perl's Text::Darts module.
* It internally uses the Dictionary and Builder classes.
*/
import Dictionary from './core/dictionary';
import { WordReplacer, TraverseCallback, BuildOptions } from './core/types';
/**
* TextDarts class - A class-based interface similar to Perl's Text::Darts
*/
export default class TextDarts {
private dictionary;
private words;
private isDisposed;
/**
* Private constructor - use static methods instead
* @param dictionary Dictionary object
* @param words Array of words
*/
private constructor();
/**
* Creates a new TextDarts object
* @param source Array of words or path to a dictionary file
* @param values Optional array of values
* @returns A new TextDarts object
*/
static new(source: string[] | string, values?: number[]): TextDarts;
/**
* Creates a new TextDarts object from a word list
* @param keys Array of words
* @param values Optional array of values
* @param options Optional build options
* @returns A new TextDarts object
*/
static build(keys: string[], values?: number[], options?: BuildOptions): TextDarts;
/**
* Creates a new TextDarts object from a dictionary file
* @param filePath Path to the dictionary file
* @returns A new TextDarts object
*/
static load(filePath: string): TextDarts;
/**
* Builds a dictionary and saves it to a file
* @param keys Array of words
* @param filePath Path to save the dictionary
* @param values Optional array of values
* @param options Optional build options
* @returns Promise that resolves to true if successful
*/
static buildAndSave(keys: string[], filePath: string, values?: number[], options?: BuildOptions): Promise<boolean>;
/**
* Builds a dictionary and saves it to a file synchronously
* @param keys Array of words
* @param filePath Path to save the dictionary
* @param values Optional array of values
* @param options Optional build options
* @returns True if successful
*/
static buildAndSaveSync(keys: string[], filePath: string, values?: number[], options?: BuildOptions): boolean;
/**
* Searches for dictionary words in a text and replaces them
* @param text The text to search in
* @param replacer The replacement method (function or object)
* @returns The text after replacement
*/
replaceWords(text: string, replacer: WordReplacer): string;
/**
* Performs an exact match search
* @param key The key to search for
* @returns The value if found, -1 otherwise
*/
exactMatchSearch(key: string): number;
/**
* Performs a common prefix search
* @param key The key to search for
* @returns Array of values
*/
commonPrefixSearch(key: string): number[];
/**
* Traverses the trie
* @param key The key to start traversal from
* @param callback The callback function
*/
traverse(key: string, callback: TraverseCallback): void;
/**
* Loads a dictionary file asynchronously
* @param filePath Path to the dictionary file
* @returns Promise that resolves to true if successful
*/
load(filePath: string): Promise<boolean>;
/**
* Loads a dictionary file synchronously
* @param filePath Path to the dictionary file
* @returns True if successful
*/
loadSync(filePath: string): boolean;
/**
* Gets the size of the dictionary
* @returns The size of the dictionary
*/
size(): number;
/**
* Ensures the object is not disposed
* @throws Error if the object is disposed
*/
private ensureNotDisposed;
/**
* Releases resources (optional, resources will be automatically released when the object is garbage collected)
*/
dispose(): void;
/**
* Gets the underlying Dictionary object
* @returns The Dictionary object
* @internal
*/
get _dictionary(): Dictionary;
}