UNPKG

node-darts

Version:

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

207 lines 6.97 kB
/** * 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 * as fs from 'fs'; import Dictionary from './core/dictionary'; import Builder from './core/builder'; import { dartsNative } from './core/native'; import { FileNotFoundError } from './core/errors'; // FinalizationRegistry for automatic resource cleanup const registry = new FinalizationRegistry((handle) => { // Clean up native resources when object is garbage collected if (handle !== undefined) { dartsNative.destroyDictionary(handle); } }); /** * TextDarts class - A class-based interface similar to Perl's Text::Darts */ export default class TextDarts { /** * Private constructor - use static methods instead * @param dictionary Dictionary object * @param words Array of words */ constructor(dictionary, words) { this.dictionary = dictionary; this.words = words; this.isDisposed = false; // Register for automatic cleanup when garbage collected registry.register(this, dictionary.getHandle(), this); } /** * 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, values) { if (Array.isArray(source)) { return TextDarts.build(source, values); } return TextDarts.load(source); } /** * 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, values, options) { const builder = new Builder(); const dictionary = builder.build(keys, values, options); return new TextDarts(dictionary, keys); } /** * Creates a new TextDarts object from a dictionary file * @param filePath Path to the dictionary file * @returns A new TextDarts object */ static load(filePath) { // Check if file exists if (!fs.existsSync(filePath)) { throw new FileNotFoundError(filePath); } const dictionary = new Dictionary(); dictionary.loadSync(filePath); // Note: We don't have the original words in this case // This will limit the functionality of replaceWords return new TextDarts(dictionary, []); } /** * 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 async buildAndSave(keys, filePath, values, options) { const builder = new Builder(); return builder.buildAndSave(keys, filePath, values, options); } /** * 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, filePath, values, options) { const builder = new Builder(); return builder.buildAndSaveSync(keys, filePath, values, options); } /** * 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, replacer) { this.ensureNotDisposed(); return this.dictionary.replaceWords(text, replacer); } /** * Performs an exact match search * @param key The key to search for * @returns The value if found, -1 otherwise */ exactMatchSearch(key) { this.ensureNotDisposed(); return this.dictionary.exactMatchSearch(key); } /** * Performs a common prefix search * @param key The key to search for * @returns Array of values */ commonPrefixSearch(key) { this.ensureNotDisposed(); return this.dictionary.commonPrefixSearch(key); } /** * Traverses the trie * @param key The key to start traversal from * @param callback The callback function */ traverse(key, callback) { this.ensureNotDisposed(); this.dictionary.traverse(key, callback); } /** * Loads a dictionary file asynchronously * @param filePath Path to the dictionary file * @returns Promise that resolves to true if successful */ async load(filePath) { this.ensureNotDisposed(); const result = await this.dictionary.load(filePath); // Note: We don't have the original words in this case // This will limit the functionality of replaceWords return result; } /** * Loads a dictionary file synchronously * @param filePath Path to the dictionary file * @returns True if successful */ loadSync(filePath) { this.ensureNotDisposed(); const result = this.dictionary.loadSync(filePath); // Note: We don't have the original words in this case // This will limit the functionality of replaceWords return result; } /** * Gets the size of the dictionary * @returns The size of the dictionary */ size() { this.ensureNotDisposed(); // If we have words array, return its size if (this.words && this.words.length > 0) { return this.words.length; } // Otherwise fall back to dictionary size return this.dictionary.size(); } /** * Ensures the object is not disposed * @throws Error if the object is disposed */ ensureNotDisposed() { if (this.isDisposed) { throw new Error('TextDarts object has been disposed'); } } /** * Releases resources (optional, resources will be automatically released when the object is garbage collected) */ dispose() { if (this.isDisposed) { return; } // Explicitly release resources this.dictionary.dispose(); // Unregister from FinalizationRegistry registry.unregister(this); // Mark as disposed this.isDisposed = true; } /** * Gets the underlying Dictionary object * @returns The Dictionary object * @internal */ // eslint-disable-next-line no-underscore-dangle get _dictionary() { this.ensureNotDisposed(); return this.dictionary; } } //# sourceMappingURL=text-darts.js.map