node-darts
Version:
Node.js Native Addon for Darts (Double-ARray Trie System)
246 lines • 8.67 kB
JavaScript
"use strict";
/**
* 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.
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs = __importStar(require("fs"));
const dictionary_1 = __importDefault(require("./core/dictionary"));
const builder_1 = __importDefault(require("./core/builder"));
const native_1 = require("./core/native");
const errors_1 = require("./core/errors");
// FinalizationRegistry for automatic resource cleanup
const registry = new FinalizationRegistry((handle) => {
// Clean up native resources when object is garbage collected
if (handle !== undefined) {
native_1.dartsNative.destroyDictionary(handle);
}
});
/**
* TextDarts class - A class-based interface similar to Perl's Text::Darts
*/
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_1.default();
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 errors_1.FileNotFoundError(filePath);
}
const dictionary = new dictionary_1.default();
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_1.default();
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_1.default();
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;
}
}
exports.default = TextDarts;
//# sourceMappingURL=text-darts.js.map