UNPKG

node-darts

Version:

Node.js Native Addon for Darts (Double-ARray Trie System)

88 lines (87 loc) 2.83 kB
import { TraverseCallback, WordReplacer } from './types'; /** * Darts Dictionary class * Provides dictionary search using Double-Array Trie */ export default class Dictionary { private handle; private isDisposed; private words; /** * Constructor * @param handle Dictionary handle (optional) * @param words Array of words (optional) */ constructor(handle?: number, words?: string[]); /** * Gets the native handle * @returns The native handle */ getHandle(): number; /** * Performs an exact match search * @param key search key * @returns the corresponding value if found, -1 otherwise * @throws {DartsError} if the search fails */ exactMatchSearch(key: string): number; /** * Performs a common prefix search * @param key search key * @returns array of found values * @throws {DartsError} if the search fails */ commonPrefixSearch(key: string): number[]; /** * Traverses the trie * @param key search key * @param callback callback function * @throws {DartsError} if the traversal fails */ traverse(key: string, callback: TraverseCallback): void; /** * Loads a dictionary file asynchronously * @param filePath path to the dictionary file * @returns true if successful, false otherwise * @throws {FileNotFoundError} if the file is not found * @throws {InvalidDictionaryError} if the dictionary file is invalid */ load(filePath: string): Promise<boolean>; /** * Loads a dictionary file synchronously * @param filePath path to the dictionary file * @returns true if successful, false otherwise * @throws {FileNotFoundError} if the file is not found * @throws {InvalidDictionaryError} if the dictionary file is invalid */ loadSync(filePath: string): boolean; /** * Gets the size of the dictionary * @returns size of the dictionary * @throws {DartsError} if getting the size fails */ size(): number; /** * 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; /** * Gets a word by its value * @param value The value to look up * @returns The corresponding word or undefined if not found */ private getWordByValue; /** * Releases resources * After calling this method, this object can no longer be used */ dispose(): void; /** * Ensures the object has not been disposed * @throws {DartsError} if the object has been disposed */ private ensureNotDisposed; }